今天在学习SpingBoot2数据访问,在整合JDBC访问mysql数据库测试时出现了以下异常。

项目能够正常启动,但就是测试的时候出现无法与JDBC连接。

org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82)at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:376)at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:465)at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:475)at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:508)at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:515)at com.atguigu.admin.BootDay03WebAdminApplicationTests.contextLoads(BootDay03WebAdminApplicationTests.java:22)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.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)at java.util.ArrayList.forEach(ArrayList.java:1249)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)at java.util.ArrayList.forEach(ArrayList.java:1249)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)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)
Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3933)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3869)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:864)at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1707)at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1217)at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2189)at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2220)at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2015)at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:768)at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)at java.lang.reflect.Constructor.newInstance(Constructor.java:423)at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:385)at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:323)at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560)at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158)at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116)at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)... 71 more

以下是测试类

@Slf4j
@SpringBootTest
class BootDay03WebAdminApplicationTests {@AutowiredJdbcTemplate jdbcTemplate;@Testvoid contextLoads() {//        jdbcTemplate.queryForObject("select * from acount_tbl")Long aLong = jdbcTemplate.queryForObject("select count(*) from acount_tbl", Long.class);log.info("记录总数:{}"+aLong);}
}

以下是application.yml配置文件

spring:datasource:url: jdbc:mysql://localhost:3306/db_acount?useUnicode=true&characterEncoding=utf-8username: rootpassword: 0123driver-class-name: com.mysql.jdbc.Driverjdbc:template:query-timeout: 3type: com.zaxxer.hikari.HikariDataSource

按照以前使用Spring或者Mybatis的配置方式来说,我觉得配置应该是没有问题的,但不知为啥老是报错,浪费了一个多小时候,发现密码应该要写成字符串的形式才行,查了一下博客,发现也有大佬遇到过这种情况。真实服了,以此记录一下,免得以后又出这种问题。

spring:datasource:url: jdbc:mysql://localhost:3306/db_acount?useUnicode=true&characterEncoding=utf-8username: rootpassword: "0123"driver-class-name: com.mysql.jdbc.Driverjdbc:template:query-timeout: 3type: com.zaxxer.hikari.HikariDataSource
  .   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.4.0)2021-08-16 22:58:24.835  INFO 14448 --- [           main] c.a.a.BootDay03WebAdminApplicationTests  : Starting BootDay03WebAdminApplicationTests using Java 1.8.0_131 on LAPTOP-0FGNQH25 with PID 14448 (started by XLJ-PC in D:\java\workspace_idea\bootday01_helloworld\boot-day03-web-admin)
2021-08-16 22:58:24.837  INFO 14448 --- [           main] c.a.a.BootDay03WebAdminApplicationTests  : No active profile set, falling back to default profiles: default
2021-08-16 22:58:26.206  INFO 14448 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-08-16 22:58:26.946  INFO 14448 --- [           main] c.a.a.BootDay03WebAdminApplicationTests  : Started BootDay03WebAdminApplicationTests in 2.57 seconds (JVM running for 3.724)
2021-08-16 22:58:27.268  INFO 14448 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-08-16 22:58:27.501  INFO 14448 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-08-16 22:58:27.521  INFO 14448 --- [           main] c.a.a.BootDay03WebAdminApplicationTests  : 记录总数:{}2
2021-08-16 22:58:27.540  INFO 14448 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2021-08-16 22:58:27.546  INFO 14448 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2021-08-16 22:58:27.546  INFO 14448 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

SpringBoot数据访问CannotGetJdbcConnectionException: Failed to obtain JDBC Connection异常的解决方式。相关推荐

  1. org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested

    org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested ...

  2. org.springframework.jdbc.CannotGetJdbcConnectionException Failed to obtain JDBC Connection

    org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection:nested e ...

  3. 【环境问题】基础Web环境搭建问题记录2——Failed to obtain JDBC Connection;

    环境: MacBook Pro 15 jdk8 IntelliJ IDEA 基础Web框架:Spring+SpringMVC+MyBatis+MySQL+Maven 问题: Error queryin ...

  4. Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsEx

    我的是再宿主机连接虚拟机的时候报的这个错 The last packet sent successfully to the server was 0 milliseconds ago. The dri ...

  5. Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: com.mysql.cj.jdbc.Driv

    解答 这种情况常见于直接打开jsp页面时ide报错,主要原因是不同意html这个静态页面,jsp它本身的特性导致其需要在运行的服务端下才可以打开. 所以打开前需要先启动tomcat等一类服务器,在服务 ...

  6. IDEA连接数据库时报错org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection

    IDEA连接数据库的记录 各种看不懂的错 org.springframework.test.context.support.DefaultTestContextBootstrapper getTest ...

  7. springboot数据访问基本操作步骤

    springboot数据访问基本操作步骤 步骤一:创建一个springboot项目 配置pom.xml依赖(此阶段我没有配置web场景启动器) <?xml version="1.0&q ...

  8. SpringBoot数据访问Mybatis注解版,配置版,注解与配置一体版

    SpringBoot数据访问Mybatis注解版,配置版,注解与配置一体版 注解版: 1.改druid 连接池,不改可以跳过这步 添加依赖 <dependency><groupId& ...

  9. Springboot数据访问

    Springboot数据访问 Springboot配置Druid pom.xml配置 <dependency><groupId>mysql</groupId>< ...

  10. SpringBoot数据访问配置

    文章目录 一.简介 二.特性 三.主要模块 四.SpringBoot整合基本JDBC与数据源 SpringBoot整合JDBC SpringBoot整合Druid数据源 一.简介 对于数据访问层而言, ...

最新文章

  1. 看到数学就打怵不是你的错:百万人调查发现上黑板做题、作业太多、家长陪写都会引发焦虑|PNAS...
  2. wso2_使用WSO2 ESB进行邮件内容过滤
  3. hbase 单机 java api,HBase学习(一)hbase安装(单机模式)和javaapi客户端访问hbase例子...
  4. 几种文件查找命令,whereis ,find ,locate.
  5. WSL(windows subsystem for linux)安装错误:安装过程中遇到错误,但可以继续安装。组件: ‘WSL 内核‘ 错误代码: 0x80072f78解决方法
  6. Result Maps collection already contains value for ***的问题
  7. C#2.0新特性探究之模拟泛型和内置算法
  8. auto_cmdb--01之models.py建表
  9. OSPF Packet Details
  10. DataList绑定xml数据,并实现删除和修改
  11. mqtt判断设备是否在线_物联网的基石-mqtt 协议初识
  12. Python-C语言语法解析:pycparser模块
  13. 怎么找网页源文件位置_html网页源代码是什么 如何查看网页源代码经验篇
  14. Jacoco 实现 Android 端手工测试覆盖率统计
  15. socks v5 协议解析
  16. 充电桩通过WiFi付费和管理方案
  17. 第6天:分割处理与中断处理
  18. python3还会有中文输出乱码问题?!!
  19. Docker 入坑指南
  20. 神经性疼痛的表现是什么,神经性疼痛常见部位是

热门文章

  1. 超声成像发射声场仿真(Ultrasound Emit Field Simulation)
  2. POJ 3709 K-Anonymous Sequence 斜率DP
  3. OSChina 周六乱弹 ——巴叔说他一直擅长硬来,弱弱的问……
  4. Segger和Micrium之间的区别和联系
  5. STM32下移植UCOSIII
  6. C语言52单片机串口中断,52单片机的中断及其使用方法
  7. Matlab自动保存图像时使用saveas函数运行出错
  8. win10运行python没有硬编码器_windows下关于python的编解码问题
  9. week15(字符串集合:Hash、字典树、KMP)
  10. MCS9865串口卡并口卡驱动