Could not get JDBC Connection

来源:互联网 发布:uitableview优化方案 编辑:程序博客网 时间:2024/05/16 08:05
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 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)  

jdbc.properties如下:

driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=gbk  username=root  password=rootspring

配置文件:

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" >          <property name="driverClassName" value="${driver}"/>          <property name="url" value="${url}" />          <property name="username" value="${username}" />          <property name="password" value="${password}" />  </beans>


当出现上面的Exception,而且spring的配置文件如上面两个文件所示时,无法连接数据库的原因在于

<property name="username" value="${username}" /> 这一行因为此时${username}的值并不是jdbc.properties文件中的username值,而是JVM系统环境变量的username。spring容器在管理PropertySource时,不光读取自己写的properties文件,spring也会把JVM system properties和JVM system env properties都读取到容器中,所以请不要使用和JVM properties相同的key。

0 0