Java 使用 Spring 连接数据库时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6520284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error at connecting to database using Spring
提问by akshay
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
我正在尝试运行一个 springexample。我已按如下方式配置了我的 .xml 文件。我使用 mysql 作为我的数据库,但我收到下面提到的错误
<?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)
线程“main” org.springframework.jdbc.CannotGetJdbcConnectionException 中的异常:无法获得 JDBC 连接;嵌套异常是 org.apache.commons.dbcp.SQLNestedException:无法在 org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82) 处创建 PoolableConnectionFactory(套接字创建错误)在 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) 在 com.vaannila.dao.ForumDAOImpl.insertForum(ForumDAOImpl.java:29)
采纳答案by nfechner
Your configuration file is setup for a HSQL database instead of a MySQL database. Use:
您的配置文件是为 HSQL 数据库而不是 MySQL 数据库设置的。用:
<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.
您还应该检查您的类路径中是否有正确的 JDBC 驱动程序。
回答by NimChimpsky
double check your connection url, try
仔细检查您的连接网址,尝试
localhost:9001
本地主机:9001
instead of
代替
127.0.0.1
127.0.0.1
... and you have not specified a database in yr connection string
...并且您没有在 yr 连接字符串中指定数据库
回答by craftsman
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"/>
回答by Humayun
I am using this bean for my connection to mysql
and its working:
我正在使用这个 bean 来连接mysql
和工作:
<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>
回答by seshumani s
Download mysql-connector-java-5.1.45put in the lib, then it works.
下载mysql-connector-java-5.1.45放到lib里,就可以了。
回答by Ojonugwa Jude Ochalifu
In my case, the mysql service
wasn't running. Check and make sure it is running.
就我而言,mysql service
没有运行。检查并确保它正在运行。