个人简介:Java领域新星创作者;阿里云技术博主、星级博主、专家博主;正在Java学习的路上摸爬滚打,记录学习的过程~ 个人主页:.29.的博客 学习社区:进去逛一逛~ Maven项目管理时,连接数据库报错 一、报错内容
个人简介:Java领域新星创作者;阿里云技术博主、星级博主、专家博主;正在Java学习的路上摸爬滚打,记录学习的过程~
个人主页:.29.的博客
学习社区:进去逛一逛~
在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.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.IdeaTestRunner 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.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.IdeaTestRunner 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版本不一致导致的无法获取数据库连接对象。
mysql mysql-connector-java 5.1.37
…
解决方案
:
mysql mysql-connector-java 8.0.31
**改完后记得重启IDEA**
测试:
@Test public void testGetConnection(){ Connection connection = JDBCUtils.getConnection(); System.out.println("connection = " + connection); }}
成功获取连接对象,问题解决:
来源地址:https://blog.csdn.net/ebb29bbe/article/details/128166718
--结束END--
本文标题: 【数据库连接异常】MySQLNonTransientConnectionException: Could not create connection to database server.
本文链接: https://www.lsjlt.com/news/372566.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0