0
votes

I build an application in java using intelliJ and MySQL. I take an error like the following

"The server time zone value '×åéìåñéíÞ þñá GTB' 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."

I tried to use jdbc:mysql://localhost:3306/schooldb?useLegacyDatetimeCode=false&serverTimezone=UTC instead of jdbc:mysql://localhost:3306/schooldb but the problem remain.

I use mysql-connector-java-8.0.15

My code is

public class Database {
    private MysqlDataSource dataSource;
    private Connection con;


    public Database(){
        dataSource = new MysqlDataSource();
        dataSource.setServerName("localhost");
        dataSource.setDatabaseName("schooldb");

        try{
            con = dataSource.getConnection("tei", "cangetin");
        }catch(SQLException e){
            e.printStackTrace();
        }
    }

    protected void finalize() throws Throwable{
        super.finalize();
        con.close();
    }
}

and

Database db = new Database();

Can anyone help me please

1
Try to use "useJDBCCompliantTimezoneShift=true" too.user1234SI.
@SebastianCăşvean Starting with jdbc driver version 8, this property have been removed as well as useLegacyDatetimeCode dev.mysql.com/doc/connector-j/8.0/en/…Maharramoff
just try jdbc:mysql://localhost:3306/schooldb?serverTimezone=UTCdassum
@SebastianCăşvean not workingAndreas Soularidis
Also, share the timezone of the database server timezone?dassum

1 Answers

0
votes

You can use MysqlDataSource.setServerTimezone as in the code below. I reproduced the exception with mysql-connector-java:8.0.18 by setting the system locale (on Windows) to something different than English (and restarting MySQL), and the edit solves the connection problem.

public Database() throws SQLException {
  dataSource = new MysqlDataSource();
  dataSource.setServerName("localhost");
  dataSource.setDatabaseName("schooldb");
  dataSource.setServerTimezone("UTC");
  try {
    con = dataSource.getConnection("username","pass");
    CallableStatement call = con.prepareCall("show databases;");

    ResultSet res = call.executeQuery();
    while(res.next()) {
      System.out.println(res.getString(1));
    }
  } catch (SQLException e) {
    e.printStackTrace();
  }
}

The strange timezone comes from the MySQL server configuration. If you execute the SQL:

SHOW VARIABLES LIKE '%zone%';

Then you'll find a strange value for variable system_time_zone. Eventually, you can edit the settings in the my.ini file instead:

[mysqld]
...
default-time-zone='+00:00'