Error at connecting to database using Spring - java

I am trying to run a springexample. I have configured my .xml file as follows. I am using mysql as my DB, but I'm getting the error mentioned below
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://127.0.0.1:3306"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="forumDAO" class="com.vaannila.dao.ForumDAOImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
error
EDIT
now changed to
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
Exception in thread "main"
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not
get JDBC Connection; nested exception is
org.apache.commons.dbcp.SQLNestedException: Cannot create
PoolableConnectionFactory (socket creation error)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:572)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:786)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:842)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:850)
at com.vaannila.dao.ForumDAOImpl.insertForum(ForumDAOImpl.java:29)

Your configuration file is setup for a HSQL database instead of a MySQL database.
Use:
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/DATABASE_NAME"/>
You should also check, that you have the correct JDBC driver in your classpath.

double check your connection url, try
localhost:9001
instead of
127.0.0.1
... and you have not specified a database in yr connection string

Your URL value is incorrect:
<property name="url" value="jdbc:hsqldb:hsql://127.0.0.1"/>
Use this instead:
<property name="url" value="jdbc:mysql://127.0.0.1:3306/yourdbname"/>

I am using this bean for my connection to mysql and its working:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test?user=root&password=root" />
<property name="initialSize" value="2" />
<property name="maxActive" value="5" />
</bean>

Download mysql-connector-java-5.1.45 put in the lib, then it works.

In my case, the mysql service wasn't running. Check and make sure it is running.

Related

Using JDBC connection with property config Throwing outofmemoryerror

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://xxx.xx.xxx.xxx:3306/vod6?autoReconnect=true" />
<!-- <property name="url" value="jdbc:mysql://xxx.xx.xxx.xxx:3306/vod2?autoReconnect=true" /> -->
<property name="username" value="voddb" />
<property name="password" value="vod#123" />
</bean>
above config xml working fine .
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${mysql.driverClassName}" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
</bean>
When I am trying to configure jdbc.property and configure them throwing error out of memory
Can you please explain what i am missing?
mysql.database=MYSQL
mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://xxx.xx.xxx.xxx:3306/vod?autoReconnect=true
mysql.username=voddb
mysql.password=vod#123
mysql.initialSize=4
mysql.maxActive=30
property file
Please confirm whether the placeholder is mentioned correctly in applicationContext.xml file
<context:property-placeholder location="classpath*:catalina.properties" ignore-unresolvable="true"/>
If this is configured correctly please try removing mysql.initialSize=4 mysql.maxActive=30 from properties file and test, this may be thr root cause. Because this is the additional property you have mentioned in properties file
You can try configuring these maxActive,initialSize values in xml itself as property.
<property name="removeAbandonedTimeout" value="180"/>
<property name="removeAbandoned" value="true"/>
<property name="logAbandoned" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="5000"/>
<property name="minEvictableIdleTimeMillis" value="900000"/>
<property name="maxActive" value="20"/>
<property name="maxIdle" value="4"/>

Searchpath with Postgres does not work with Spring/DBCP datasource

Postgres extensions are installed in public schema.
Set the search path for the app-specific schema on the DBCP datasource the following way:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" primary="true">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://myhost:myport/${db.gos_app.database}?searchpath=mySchema,public;?ApplicationName=${app.name}"/>
<property name="connectionProperties" value="currentSchema=mySchema;"/>
<property name="username" value="user"/>
<property name="password" value="pw"/>
<property name="defaultAutoCommit" value="false"/>
<property name="maxActive" value="6" />
</bean>
But somehow I cannot use extensions installed in this public schema without qualifying them like "public.hstore".
Found the solution - JDBC driver does not know the searchpath property in the URL. But this has been nowhere reported :-(
Instead currentSchema is given to driver which is then mapped to native Postgres searchpath (thus probably overwriting the default one there with public included). Not intuitive!
So the solution looks like this:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" primary="true">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://myhost:myport/${db.gos_app.database}?ApplicationName=${app.name}"/>
<property name="connectionProperties" value="currentSchema=mySchema,public;"/>
<property name="username" value="user"/>
<property name="password" value="pw"/>
<property name="defaultAutoCommit" value="false"/>
<property name="maxActive" value="6" />

Connecting Java Web Application to Oracle Database

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="????????????????????" />
<property name="url"
value="???????????????????????"/>
<property name="username" value="root" />
<property name="password" value="" />
</bean>
I am not able to figure out what should go in driver class name value and url vale. I have downloaded Oracle sql developer and oracle 11 g.But i am not sure how to configure it to my java application
Driver class name:- oracle.jdbc.driver.OracleDriver
URL :- jdbc:oracle:thin:#(hostname):(port number):(database name)
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:#(hostname):(port number):(database name)"/>
<property name="username" value="root" />
<property name="password" value="" />
</bean>
Your qustion is not clear if you are using Spring framework there are two type of
Data source handling technique
Method 1 you need to configure data source in your application server
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jboss/datasources/OracleDS" />
<property name="cache" value="false"/>
<property name="lookupOnStartup" value="false"/>
<property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>
Method 2
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#127.0.0.1:1521:OMQA" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>

JDBC beans configuration

I was following the tutorial from
http://www.mkyong.com/spring/maven-spring-jdbc-example/
I have not worked with beans before and one thing from this tutorial puzzled me
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerDAO" class="com.mkyong.customer.dao.impl.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
This is a bean file which contains a bean which sets the dataSource variable from JDBCCustomerDao to be dataSource which is yet another bean contained in this file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mkyongjava" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
I understand so far that the dataSource variable from JdbcCustomerDao is set to have the properties
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mkyongjava" />
<property name="username" value="root" />
<property name="password" value="password" />
However I'm not sure what the url points to. Is is the url where my database can be found? Is it the directory which I can create dbs in?
Probably this question has a pretty simple answer but I'm not that sure and google searches didn't really help.
Thank you
JDBC urls are driver-specific. In this case, it points to a MySQL server at localhost, port 3306, to the database named mkyongjava.

FileNotFoundException while running Spring and Ibatis based application on Tomcat

I am using Spring, ibatis for ORM. My app-config.xml look like
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.10.50/lmexdb_v1" />
<property name="username" value="lmexdba" />
<property name="password" value="lmexdba123#" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:com/platysgroup/lmex/server/mobile/dao/ibatis/SqlMapConfig.xml" />
</bean>
<bean id="mobileController" class="com.platysgroup.lmex.server.controller.MobileController">
<property name="announcementService" ref="announcementService"></property>
<property name="courseService" ref="courseService"></property>
<property name="userService" ref="userService"></property>
</bean>
and I have my sqlmapconfig.xml file in src/webapp/spring.
But when I run my application on tomcat it show me a exception:
java.io.FileNotFoundException: class path resource [com/platysgroup/lmex/server/mobile/dao/ibatis/SqlMapConfig.xml] cannot be opened because it does not exist
put it in src , and it will be available
if you are using maven project then add it to resource

Categories

Resources