11

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

kumar-kunal
  • 126
  • 1
  • 1
  • 5
  • 1
    Read the stacktrace carefully and you will find a **NULLPOINTEREXCEPTION** – Mehraj Malik Sep 10 '17 at 04:08
  • so how to get rid of it... i am just a beginner.. – kumar-kunal Sep 10 '17 at 04:10
  • There is an NPE is your ConnectionImpl at line no 3005... – Mehraj Malik Sep 10 '17 at 04:11
  • 2
    @MehrajMalik *"is **your** ConnectionImpl"* What are you talking about? That code doesn't belong to OP. That is in the MySQL JDBC driver code, i.e. a third-party library. – Andreas Sep 10 '17 at 04:25
  • @kumar-kunal What version of the MySQL JDBC driver are you using, and what is the version of the MySQL server you're trying to connect to? And for sake of completeness, what Java version are you using? It could be a bug in that particular JDBC driver. – Andreas Sep 10 '17 at 04:26
  • @Andreas mysql-connector-java-5.1.38 and mysql ver 8.0.2 and java 1.8 – kumar-kunal Sep 10 '17 at 04:38
  • 4
    Why are you using an unreleased (development/test) version of MySQL? Try downgrading to MySQL 5.7.19 (the latest release), or otherwise upgrade your Connector/J to the development version 8.0.7-dmr. Trying to upgrade to the latest released Connector/J (5.1.44) might also be an option. – Mark Rotteveel Sep 10 '17 at 09:00
  • 2
    @MarkRotteveel thanks , it's working after using MySql 5.7.19 – kumar-kunal Sep 10 '17 at 14:21

3 Answers3

13

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?

user2114253
  • 179
  • 1
  • 4
  • 1
    this allows me to connect to mysql8 but some installations still use 5 and this can't connect – alibttb Sep 15 '19 at 19:44
9

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.

Keith Oakes
  • 91
  • 1
  • 3
1

I would try to play around the char encoding sets, for instance you can try to use the following url

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