Alvaro Vila Legua
Alvaro Vila Legua

Reputation: 33

How to fix error with MySQL because of timezone change?

Currently in Europe the timezone was changed and now its one hour more. For some reason this broke my script, and I cant connect to my mysql database.

I tried restarting, stopping and starting mysql server, even restarting eclipse. None of this works.

    public static Connection getConnection() throws Exception{
        try {
            String driver = "com.mysql.cj.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/db";
            String username = "root";
            String password = "***";
            Class.forName(driver);

            Connection DB = DriverManager.getConnection(url, username, password);
            System.out.println("Connected to Database");        
            return DB;
        } catch(Exception e) {System.out.println(e);        
        }

        return null;
    }

This worked before the time change, now I get this error

java.sql.SQLException: The server time zone value 'CEST' 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.

Upvotes: 2

Views: 4354

Answers (2)

Ari
Ari

Reputation: 46

This can be fixed on the server-side with an explicit timezone setting in my.cnf. If your timezone is for instance, Europe/Berlin:

[mysqld]
default-time-zone = Europe/Berlin

Upvotes: 1

Georges Khater
Georges Khater

Reputation: 74

try this : String url = "jdbc:mysql://localhost:3306/db?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";

Upvotes: 0

Related Questions