0
package com.practise;

import java.sql.*;

public class connectSQL {

    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "Root!123321");

            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery("select * from testTable");

            while(rs.next()){
                System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));
                con.close();
            }

        }catch(Exception e){

            System.out.println("We got an exception...");
            System.out.println(e.getMessage());
        }
    }
}

Output:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
We got an exception...
The server time zone value 'EEST' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Have you tried loading ‘com.mysql.cj.jdbc.Driver’ or remove that line where you are loading a driver? That what it seems like it wants you to do because the driver you choose is not longer supported. – Matthew Sep 28 '19 at 17:02
  • If that warning is not the exception that happens when you try to connect, then please post the exception. – Matthew Sep 28 '19 at 17:03
  • What's the msql jdbc driver version? – Martin'sRun Sep 28 '19 at 17:07

2 Answers2

0

Looks like a warning message resulting from a

Class.forName("com.mysql.jdbc.Driver")

call. Your code continues to run since it is just a warning.

It is mainly telling you that the name of the driver class has changed to com.mysql.cj.jdbc.Driver. So, instead use:

Class.forName("com.mysql.cj.jdbc.Driver")

It is also letting you know that since Java 6 (JDBC 4.0) it is usually not necessary to manually load the driver class using Class.forName anyway, because JDBC is now able to load the correct driver itself (provided that the driver jar file is available on the class path).

Sujay Mohan
  • 933
  • 7
  • 14
0

As the others already answer you. You must use the new driver syntax.

Class.forName("com.mysql.cj.jdbc.Driver");

The MySQL driver .jar file must be in your CLASSPATH.

Also you need to configure your mysql server in order to avoid the timezone exception. You could just set the UTC timezone in your server manually or just use this in your jdbc url.

jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC