D
D
Domus2017-05-12 17:07:12
Java
Domus, 2017-05-12 17:07:12

How to fix error when connecting to MySql via JDBC?

Good afternoon!
I'm trying to connect to MySql but I'm getting an error. Please help me fix it. mysql:mysql-connector-java:6.0.6.

package com.company;
import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class BdApp {
    public static void main(String[] args) {
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("JDBC подключен.");
            try{
                Connection con = DriverManager.getConnection("jdbc:mysql://[удаленный_хост]:3306/straider_exam","045682268_exam", "4848");
            }catch(SQLException ex){
                System.out.println("Ошибка подключения");
                ex.printStackTrace();
            }
        }catch(ClassNotFoundException ex){
            System.out.println("Не нашёл драйвер.");
            ex.printStackTrace();
        }

    }
}

I get:
JDBC подключен.
Ошибка подключения
java.sql.SQLException: The server time zone value 'MSK' 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.
  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:545)
  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:505)
  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:479)
  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:489)
  at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:69)
  at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1606)
  at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:633)
  at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:347)
  at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:219)
  at java.sql.DriverManager.getConnection(DriverManager.java:664)
  at java.sql.DriverManager.getConnection(DriverManager.java:247)
  at com.company.BdApp.main(BdApp.java:17)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value 'MSK' 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.
  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.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
  at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:73)
  at com.mysql.cj.jdbc.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:118)
  at com.mysql.cj.mysqla.MysqlaSession.configureTimezone(MysqlaSession.java:293)
  at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:2399)
  at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1739)
  at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1596)
  ... 11 more

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Shockoway, 2017-05-13
@Shockoway

Most likely there is some more correct way, but I treated it like this:

"jdbc:mysql://[хост]:[порт]/[бд]?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"

I
IceJOKER, 2017-05-12
@IceJOKER

It also says what the problem is, there is no MSK timezone or more than one timezone is installed.
Configure JDBC via setTimezone or mysql

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question