简体   繁体   中英

Exception in connection with mysql through jdbc

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...\n");
        Connection con=DriverManager.getConnection(url,user,pass);
        System.out.println("Connected !!!\n");
        con.close();
        System.out.println("connection close !!!\n");

    } 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..

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

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>

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

How to load JAR files dynamically at Runtime?

I was using a latest version of mysql server with an older version of mysql-connector. Once I updated to the latest connector(8.0.11) this issue cleared up for me.

我会尝试使用字符编码集,例如您可以尝试使用以下网址

String url="jdbc:mysql://localhost:3306/student?autoReconnect=true&useSSL=false&characterEncoding=utf8&useUnicode=true";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM