Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException
错误信息展示:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:### Error querying database.Cause: org.springframework.jdbc.CannotGetJdbcCo...
·
错误信息展示:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user ''@'localhost' to database 'stu')
### The error may exist in file [E:\IDEAspace\Spring\SpringMybaties\target\classes\com\baizhi\mapper\StudentDaoMapper.xml]
### The error may involve com.baizhi.dao.StudentDao.query
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user ''@'localhost' to database 'stu')
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy9.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:119)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
at com.sun.proxy.$Proxy10.query(Unknown Source)
at com.baizhi.test.TestDAO.testSelectAll(TestDAO.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
解决办法一(详):
错误的小配置文件:
driver:com.mysql.jdbc.Driver
url:jdbc:mysql://localhost:3306/stu?characterEncoding=UTF-8
username:root
password:
注:配置信息根据自己的配置
错误的spring配置:
<!--读取小配置文件 获取连接数据库的参数-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!--配置数据源 使用DBCP连接池-->
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--连接数据库的相关参数-->
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
正确的小配置文件:
jdbc.driver:com.mysql.jdbc.Driver
jdbc.url:jdbc:mysql://localhost:3306/stu?characterEncoding=UTF-8
jdbc.username:root
jdbc.password:
正确的spring配置:
<!--读取小配置文件 获取连接数据库的参数-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!--配置数据源 使用DBCP连接池-->
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--连接数据库的相关参数-->
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
解决办法二(略):
如果上述方法未能解决你的异常,你可以尝试着用IDEA或者MyEclipse集成工具连接一下数据库,具体方法可以搜索比如IDEA集成mysql数据库等,这样做的目的是先排除一下你书写的配置信息是否有误。
解决办法三(略):
检查数据库的服务是否已经打开,具体方法百度。
解决办法四(略):
更换相关jar包,可能是版本不兼容所导致的。
笔者今天遇到这个问题,主要想到的解决办法就是后三种,对第一种解决办法不是很了解,最终查阅了相关API后问题才得到解决,记录一下。
更多推荐


所有评论(0)