1

I wrote some code to insert a table in a local database and I am getting the following error per the stack trace:

java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:108)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
    at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:932)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:857)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at CreateTable.main(CreateTable.java:24)
Caused by: com.mysql.cj.exceptions.WrongArgumentException: No timezone mapping entry for 'PDT'
    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.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
    at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2215)
    at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)
    at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)
    at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:895)
    ... 7 more

Apparently, I could be wrong, but it has something to do with my timezone or the way the timezone is set in MySQL or the JDBC connector.

Here is my original source code:

import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTable {
    public static void main(String[] args) throws SQLException {
        Connection myConnection = null;
        Statement myStmt = null;
        //ResultSet rs = null;
        String sql = "create table student(student_id varchar(20) primary key, name varchar(30), class varchar(15), marks varchar(10);";

        try {
        /*
        Get Connection to jdbc
        */
        myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/edureka?autoReconnect=true&useSSL=false", "userOne", "password");

        /*Create an object for passing SQL queries to DB */
        myStmt = myConnection.createStatement();

        /*Execute a resulting statement to create a table in the DB */
        myStmt.executeQuery(sql);

        System.out.println("DB Table created in Database Edureka!");

        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Al-geBra
  • 169
  • 3
  • 13

1 Answers1

1

Try modifying the connection url by adding timezone info:

myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/edureka?autoReconnect=true&useSSL=false&serverTimezone=PDT", "userOne", "password");
NiVeR
  • 9,644
  • 4
  • 30
  • 35