通过 jdbc 连接 mysql 的异常 [英] Exception in connection with mysql through jdbc

查看:28
本文介绍了通过 jdbc 连接 mysql 的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试通过 jdbc 与 mysql 连接时出现异常.已经问过这种类型的问题,但我没有得到解决我的问题的方法,这是我的代码...

I got exception when i have tried to connect with mysql through jdbc.This type of question is already asked but i didn't get solution for my problem, here is my code...

    public static void main(String[] args) {
    String url="jdbc:mysql://localhost:3306/student?
    autoReconnect=true&useSSL=false";
    String user="root";
    String pass="system";
    try {
        Class.forName("com.mysql.jdbc.Driver");

        System.out.println("Now connecting to databse...
");
        Connection con=DriverManager.getConnection(url,user,pass);
        System.out.println("Connected !!!
");
        con.close();
        System.out.println("connection close !!!
");

    } catch (ClassNotFoundException | SQLException e) {System.out.println(e);
   e.printStackTrace();
    }
}

}

这些是我得到的例外...

these are exception which i got...

run:
Now connecting to databse...

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
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:404)
    at com.mysql.jdbc.Util.getInstance(Util.java:387)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
    at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2165)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2090)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    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:404)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at dbms_basic.Dbms_Basic.main(Dbms_Basic.java:28)
Caused by: java.lang.NullPointerException
    at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3005)
    at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1916)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1845)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1215)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2255)
    at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2106)
    ... 13 more
BUILD SUCCESSFUL (total time: 4 seconds)

有人帮我解决这个错误..

someone help me solve this error..

推荐答案

当您尝试在 Mysql 8 或更高版本的数据库上使用 Mysql 5 JDBC 驱动程序时,会发生此错误.您可以使用以下 JAR 文件

This error happens when you try to use a Mysql 5 JDBC driver on a Mysql 8 or higher DB. You can use the following JAR file

mysql-connector-java 8.0.11

或者使用这个Maven依赖

Or use this Maven dependency

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
    </dependency>

如果您需要在同一个应用程序中同时连接到 MySQL 5 和 MySQL 8,您将需要隔离类加载器,如下所示可能会起作用

If you need to have connection both to MySQL 5 and MySQL 8 in the same app you will need to isolate classloaders, something like the following may work

如何在运行时动态加载 JAR 文件?

这篇关于通过 jdbc 连接 mysql 的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆