首页 > 代码库 > org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection

这个问题困扰许久,许久。原来只是data source的properties引入文件,el表达式前必须加jdbc

原文解决参考:http://blog.csdn.net/liwuyang2013/article/details/25986597

 

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 root @localhost (using password: YES))### The error may exist in mapper/CustomerMapper.xml### The error may involve com.zhexiang.mybatis_springmvc.model.selectCustomerByID### 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 root @localhost (using password: YES))  at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)  at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:111)  at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)  at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:66)  at com.zhexiang.mybatis_springmvc.dao.impl.CustomerDaoImpl.selectCustomerByID(CustomerDaoImpl.java:18)  at com.zhexiang.mybatis_springmvc.dao.impl.CustomerDaoImplTest.testSelectCustomerByID(CustomerDaoImplTest.java:37)  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  at java.lang.reflect.Method.invoke(Method.java:597)  at junit.framework.TestCase.runTest(TestCase.java:176)  at junit.framework.TestCase.runBare(TestCase.java:141)  at junit.framework.TestResult$1.protect(TestResult.java:122)  at junit.framework.TestResult.runProtected(TestResult.java:142)  at junit.framework.TestResult.run(TestResult.java:125)  at junit.framework.TestCase.run(TestCase.java:129)  at junit.framework.TestSuite.runTest(TestSuite.java:255)  at junit.framework.TestSuite.run(TestSuite.java:250)  at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user root @localhost (using password: YES))  at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)  at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:81)  at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:67)  at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:279)  at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:72)  at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:59)  at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)  at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:137)  at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:96)  at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:77)  at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108)  ... 23 moreCaused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user root @localhost (using password: YES))  at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)  at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)  at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)  at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)  at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)  ... 33 moreCaused by: java.sql.SQLException: Access denied for user root @localhost (using password: YES)  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)  at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)  at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)  at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)  at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)  at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)  at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)  at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)  at java.lang.reflect.Constructor.newInstance(Constructor.java:513)  at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)  at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)  at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)  at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)  at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)  at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)  at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)  ... 37 more

出现以上错误原因是:数据源的错误配置:

错误的jdbc.properties

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=gbkusername=rootpassword=root

错误的spring配置文件:

<?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">  <!--  config db property file location  -->  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="locations" value=http://www.mamicode.com/"classpath:jdbc.properties"/>  </bean>    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" >        <property name="driverClassName" value=http://www.mamicode.com/"${driver}"/>        <property name="url" value=http://www.mamicode.com/"${url}" />        <property name="username" value=http://www.mamicode.com/"${username}" />        <property name="password" value=http://www.mamicode.com/"${password}" /> </beans>

正确的jdbc.properties配置:

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=gbkjdbc.username=rootjdbc.password=root

Spring4.0.4 官方文档数据源配置:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:context="http://www.springframework.org/schema/context"  xsi:schemaLocation="    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <!-- Scans within the base package of the application for @Components to configure as beans -->  <context:component-scan base-package="org.springframework.docs.test" />    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    <property name="driverClassName" value=http://www.mamicode.com/"${jdbc.driverClassName}"/>    <property name="url" value=http://www.mamicode.com/"${jdbc.url}"/>    <property name="username" value=http://www.mamicode.com/"${jdbc.username}"/>    <property name="password" value=http://www.mamicode.com/"${jdbc.password}"/>  </bean>  <context:property-placeholder location="jdbc.properties"/></beans>
麦田里的Cracker12-25 10:49
叼叼叼,楼主我都想请你吃饭了,这个问题困扰了我将近三天时间。jdbc什么我都配好了,就是出不来,网上的例子也大都千律一篇。赞

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection