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 exist in mapper/customermapper.xml### the error may I Nvolve 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 the 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 the 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) ... Morecaused By:org.apache.commons.dbcp.SQLNestedExcePtion:cannot Create Poolableconnectionfactory (Access denied for user ' root ' @ ' localhost ' (using Password:yes)) at Org.ap Ache.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) ... Morecaused By:java.sql.SQLException:Access denied for the 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) ... Panax Notoginseng More
The above error occurs because of an incorrect configuration of the data source:
Wrong jdbc.properties.
driver=com.mysql.jdbc.driverurl=jdbc:mysql://localhost:3306/mybatis?useunicode=true&characterencoding= Gbkusername=rootpassword=root
Wrong Spring configuration file:
<?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= "classpath:jdbc.properties"/></bean><bean id= "DataSource" Destroy-method= "Close" class= "Org.apache.commons.dbcp.BasicDataSource" > <property name= "Driverclassname" Val Ue= "${driver}"/> <property name= "url" value= "${url}"/> <property name= "username" value= "${username}"/> <property name=" password "value=" ${password "/> </beans>
The correct jdbc.properties configuration:
jdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost:3306/mybatis?useunicode=true& Characterencoding=gbkjdbc.username=rootjdbc.password=root
Spring4.0.4 Official document Data source configuration:
<?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/co Ntext/spring-context-3.0.xsd "> <!--Scans within the base package of the application for @Components to Confi Gure 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=" DRIVERCL Assname "value=" ${jdbc.driverclassname} "/> <property name=" url "value=" ${jdbc.url} "/> <property Name= "username" value= "${jdbc.username} "/> <property name=" password "value=" ${jdbc.password} "/> </bean> <context:property-placeho Lder location= "Jdbc.properties"/></beans>