【数据库连接异常】MySQLNonTransientConnectionException: Could not create connection to database server.

在这里插入图片描述

在这里插入图片描述

个人简介:Java领域新星创作者;阿里云技术博主、星级博主、专家博主;正在Java学习的路上摸爬滚打,记录学习的过程~
个人主页:.29.的博客
学习社区:进去逛一逛~

在这里插入图片描述

Maven项目管理时,连接数据库报错


一、报错内容

在JDBC连接数据库时,代码没有错,运行却出现报错信息:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at org.junit.runners.ParentRunner 2. e v a l u a t e ( P a r e n t R u n n e r . j a v a : 268 ) a t j a v a . l a n g . r e f l e c t . C o n s t r u c t o r . n e w I n s t a n c e ( C o n s t r u c t o r . j a v a : 423 ) a t o r g . j u n i t . r u n n e r s . P a r e n t R u n n e r . r u n ( P a r e n t R u n n e r . j a v a : 363 ) a t c o m . m y s q l . j d b c . U t i l . h a n d l e N e w I n s t a n c e ( U t i l . j a v a : 404 ) a t o r g . j u n i t . r u n n e r . J U n i t C o r e . r u n ( J U n i t C o r e . j a v a : 137 ) a t c o m . m y s q l . j d b c . U t i l . g e t I n s t a n c e ( U t i l . j a v a : 387 ) a t c o m . i n t e l l i j . j u n i t 4. J U n i t 4 I d e a T e s t R u n n e r . s t a r t R u n n e r W i t h A r g s ( J U n i t 4 I d e a T e s t R u n n e r . j a v a : 69 ) a t c o m . m y s q l . j d b c . S Q L E r r o r . c r e a t e S Q L E x c e p t i o n ( S Q L E r r o r . j a v a : 919 ) a t c o m . i n t e l l i j . r t . j u n i t . I d e a T e s t R u n n e r 2.evaluate(ParentRunner.java:268) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.mysql.jdbc.Util.getInstance(Util.java:387) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) at com.intellij.rt.junit.IdeaTestRunner 2.evaluate(ParentRunner.java:268)atjava.lang.reflect.Constructor.newInstance(Constructor.java:423)atorg.junit.runners.ParentRunner.run(ParentRunner.java:363)atcom.mysql.jdbc.Util.handleNewInstance(Util.java:404)atorg.junit.runner.JUnitCore.run(JUnitCore.java:137)atcom.mysql.jdbc.Util.getInstance(Util.java:387)atcom.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)atcom.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)atcom.intellij.rt.junit.IdeaTestRunnerRepeater 1. e x e c u t e ( I d e a T e s t R u n n e r . j a v a : 38 ) a t c o m . m y s q l . j d b c . S Q L E r r o r . c r e a t e S Q L E x c e p t i o n ( S Q L E r r o r . j a v a : 898 ) a t c o m . i n t e l l i j . r t . e x e c u t i o n . j u n i t . T e s t s R e p e a t e r . r e p e a t ( T e s t s R e p e a t e r . j a v a : 11 ) a t c o m . m y s q l . j d b c . S Q L E r r o r . c r e a t e S Q L E x c e p t i o n ( S Q L E r r o r . j a v a : 887 ) a t c o m . i n t e l l i j . r t . j u n i t . I d e a T e s t R u n n e r 1.execute(IdeaTestRunner.java:38) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) at com.intellij.rt.junit.IdeaTestRunner 1.execute(IdeaTestRunner.java:38)atcom.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)atcom.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)atcom.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)atcom.intellij.rt.junit.IdeaTestRunnerRepeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:862)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2331)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084)
Caused by: java.lang.NullPointerException: null


二、分析与解决

我出现这个问题后,找到的原因是:Maven添加管理的mysql-connector-java依赖版本与下载使用的MySQL版本不一致导致的无法获取数据库连接对象。

  • Maven添加的版本:5.1.37
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
</dependency>

  • 实际下载使用的MySQL版本:8.0.31

在这里插入图片描述


解决方案

  • 将IDEA项目中Maven的pom.xml配置文件中添加的依赖版本改为自己正在使用的MySQL版本即可:
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>
**改完后记得重启IDEA**

测试:

    @Test
    public void testGetConnection(){
        Connection connection = JDBCUtils.getConnection();
        System.out.println("connection = " + connection);
    }
}

成功获取连接对象,问题解决:
在这里插入图片描述


在这里插入图片描述

  • 54
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 81
    评论
SQLNonTransientConnectionException是一个Java编程语言中的异常类,用于表示在与数据库建立连接或执行数据库操作时发生的非永久性连接异常。这个异常通常是由于连接被意外关闭或服务器端关闭了连接引起的。 在引用中的原因分析中提到了几种可能导致SQLNonTransientConnectionException异常的原因,包括数据库连接池配置问题、业务逻辑中的大事务操作以及数据库参数配置等。其中,特别提到了innodb_lock_wait_timeout参数的配置,默认为50秒,一旦数据库锁超过这个时间,就会触发异常。 另外,在引用中提到了Public Key Retrieval is not allowed错误,这个错误可能出现在数据库连接过程中,可能需要在项目的SDK和语言配置中进行相应的调整。 综上所述,SQLNonTransientConnectionException是一个表示非永久性连接异常Java异常类,可能是由于连接被意外关闭、服务器关闭连接、数据库连接池配置问题、业务逻辑中的大事务操作或数据库参数配置等原因引起的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mysql SQLNonTransientConnectionException 异常分析处理](https://blog.csdn.net/cwh056056/article/details/117330206)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [基于JavaWeb学生宿舍管理系统毕业设计项目源码(完美注释+高分必看).zip](https://download.csdn.net/download/weixin_55305220/85624213)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 81
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.29.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值