参见英文答案 Can’t connect to MySQL from Java: NullPointerException inside MySQL driver connection logic 2个我是数据库中的新手,刚开始学习它们.我有MySQL Server 8.0...
参见英文答案 > Can’t connect to MySQL from Java: NullPointerException inside MySQL driver connection logic 2个
我是数据库中的新手,刚开始学习它们.我有MySQL Server 8.0.,Workbench 8.0,Java连接器5.1.31,Java 1.8本身.为新手如何开始遵循多个指南.
有这样的情况.我在localhost上有数据库,并通过workbench和windows提示符成功连接到它.但是在我执行java代码之后:
Driver sqlDriver = new FabricMySQLDriver();
DriverManager.registerDriver(sqlDriver);
Connection sqlConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=root", "root", "root");
我得到异常com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法创建与数据库服务器的连接.
为什么会发生?我试图重新安装MySQL,清理操作系统变量,到处寻找并找不到解决方案.很高兴在这里找到它.
UPD:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
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:408)
at com.mysql.jdbc.Util.getInstance(Util.java:383)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1023)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:997)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:983)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:928)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2407)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2328)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
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:408)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.company.Main.main(Main.java:11)
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.ConnectionImpl.getServerCharacterEncoding(ConnectionImpl.java:3309)
at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1985)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1911)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2508)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2346)
... 13 more
解决方法:
问题是旧版MySQL Connector / J与MySQL 8的兼容性.您需要升级到MySQL Connector / J 5.1.46或 – 更好 – 8.0.11.
本文标题为:java – com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法创建与数据库服务器的连接
基础教程推荐
- ubuntu 14.04 java开发环境搭建 jdk 以及 inteliJ IDEA安装 2023-08-31
- Mybatis示例讲解注解开发中的单表操作 2023-02-19
- Java压缩文件操作详解 2023-04-07
- Java中classpath讲解及使用方式 2023-08-11
- SpringCloud降级规则使用介绍 2023-05-08
- JSP实现登录功能之添加验证码 2023-08-01
- Java图书管理系统课程设计 2022-11-04
- Java 多线程并发AbstractQueuedSynchronizer详情 2023-01-13
- SpringBoot利用validation实现优雅的校验参数 2023-01-08
- 一文带你你搞懂Java的3种IO模型 2023-07-15