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

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 more
Caused 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 more
Caused 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.Driver
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=gbk
username=root
password=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="classpath:jdbc.properties"/>
	</bean>

	<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>
   

正确的jdbc.properties配置:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=gbk
jdbc.username=root
jdbc.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="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <context:property-placeholder location="jdbc.properties"/>

</beans>

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection,布布扣,bubuko.com

时间: 2024-12-26 21:05:14

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection的相关文章

jdbc连接oracle时报错 Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableC

错误: Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure Last packet sent

Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection;

解决报错 This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue Jan 07 21:06:43 CST 2020 There was an unexpected error (type=Internal Server Error, status=500). nested exception is org.apache.ibatis.exceptions.Pers

Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection

问题:维护团队原有java项目,spring + mybatis.所在机器重启后,重启项目,tomcat已启动,报以下错 解决: 根据错误信息推测与数据库有关,在Linux上执行mysql,报错 which mysql sh xx/bin/mysqld_safe # xx为安装地址 执行mysql无问题,项目仍报错.mysql语句链接项目使用数据库,报错:链接另一个一直数据库,可链接 进入项目所在目录下使用的mysql文件夹,执行sh xx/bin/mysqld_safe 梳理: mysql作为

Could not resolve placeholder &#39;jdbc.username&#39; in string value &quot;${jdbc.username}&quot;

1.错误描述 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource'

错误:“Cannot load JDBC driver class &#39;com.mysql.jdbc.Driver”的解决方法

"Cannot load JDBC driver class 'com.mysql.jdbc.Driver " 表示没有JDBC连接MySql的驱动包,因此需要手动添加驱动包到WEB-INF目录下的lib目录中. 解决方法: 从网上下载mysql-connector-java.jar,将其放到"D:\workspace\my-web\src\main\webapp\WEB-INF\lib"目录下,即可解决上述问题. 错误:"Cannot load JDBC

java攻城狮之路--复习JDBC(利用BeanUtils、以及JDBC元数据编写通用的查询方法)

1.利用BeanUtils的前提得要加入以下两个jar包: commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar package com.shellway.jdbcDAO; import java.util.List; import org.junit.Test; public class TestDAO { DAO dao = new DAO(); @Test public void testUpdate() throws Exceptio

完整java开发中JDBC连接数据库代码和步骤 JDBC连接数据库

JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.Class类的静态方法forName(String  className)实现. 例如: try{ //加载MySql的驱动类 Class.forName("com.mysql.jdbc.Driver") ; }catch(ClassNotFoundException e){ Sy

如何启动JDBC Debug模式,打印JDBC诊断日志

1.下载Debug版本jar包 首先要下载一个Debug版本的JDBC jar包,Debug版本的jar包命名形式为jdbcX_g.jar(如下图所示),如Oracle11g的Debug版本的jar包为jdbc6_g.jar.点击这里下载:http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html 2.启动Logging模式 第一种方式:设置运行环境变量java -Doracle.jd

JDBC Request :Cannot load JDBC driver class &#39;com.mysql.jdbc.Driver&#39;解决办法

在Jmeter中run JDBC Request时,收到了Cannot load JDBC driver class 'com.mysql.jdbc.Driver',在网上搜了一些办法,结合自己的实际情况,找到了以下解决办法: 1.点击这里下载mysql-connector-java-5.1.47.zip(或者其他版本) 2.解压之后,将mysql-connector-java-5.1.47文件夹下的mysql-connector-java-5.1.47-bin.jar文件放到Jmeter的安装