Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException已解决!

问题描述:

使用Java连接数据库出现错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at 。。。。。
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2989)
at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1873)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1802)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1206)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2239)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270)
… 13 more

查询资料后发现是mysql数据库连接的包导入版本过低,我使用的是mysql8.0版本数据库,jar包版本是5.1.32

Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException已解决!

此时我们需要下载8.0版本的jar包

下载链接: https://downloads.mysql.com/archives/c-j/.
Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException已解决!

下载完成后重新引入lib文件下

Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException已解决!

此时若按照5.0版本的代码又会出现:

Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException已解决!


Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException已解决!

现在按照最新官方提示支持将com.mysql.jdbc.Driver 改为 com.mysql.cj.jdbc.Driver

发现改后还有报错,报错变了
Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException已解决!
serverTimezone=UTC”不然会因为时区问题报错

在url后面加
String url=“jdbc:mysql://localhost/db_data?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true”;
Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException已解决!
再次运行发现不再报错

接着向数据库保存数据
Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException已解决!
打开数据库刷新查看多了添加的元素:
Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException已解决!
附上代码:

package day8.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author:zzy
 * @email:847464807@qq.com
 * @version:v1.1.0
 * @time:
 * @description:
 */

public class TestJDBC {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.实现数据库的驱动
        Class.forName("com.mysql.cj.jdbc.Driver");//8.0以上版本
        //Class.forName("com.mysql.jdbc.Driver");

        //2.获取数据库连接
        String url="jdbc:mysql://localhost/db_data?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true";//
        String user="root";//数据库的用户名
        String password="zzy0720*";//数据库的密码
        Connection con=DriverManager.getConnection(url,user,password);
        System.out.println(con);


        //3.获取传输对象
        Statement stat = con.createStatement();

        //4.发送SQL
        String sql="insert into data values('山西省',123,15,2,0)";
        int rows=stat.executeUpdate(sql);
        System.out.println("影响了"+rows+"行");
    }
}

上一篇:MyBatis程序中遇到的若干异常(java.lang.ExceptionInInitializerError和com.mysql.jdbc.exceptions.jdbc4.Communicatio


下一篇:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not al