akshay
akshay

Reputation: 5285

Error at connecting to database using Spring

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)

Upvotes: 2

Views: 66521

Answers (6)

Ojonugwa Jude Ochalifu
Ojonugwa Jude Ochalifu

Reputation: 27237

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

Upvotes: 0

seshumani s
seshumani s

Reputation: 1

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

Upvotes: 0

Humayun
Humayun

Reputation: 35

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>

Upvotes: 0

craftsman
craftsman

Reputation: 15623

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"/>

Upvotes: 0

NimChimpsky
NimChimpsky

Reputation: 47280

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

Upvotes: 0

nfechner
nfechner

Reputation: 17525

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.

Upvotes: 2

Related Questions