467

Some background:

I have a Java 1.6 webapp running on Tomcat 7. The database is MySQL 5.5. Previously, I was using Mysql JDBC driver 5.1.23 to connect to the DB. Everything worked. I recently upgraded to Mysql JDBC driver 5.1.33. After the upgrade, Tomcat would throw this error when starting the app.

WARNING: Unexpected exception resolving reference
java.sql.SQLException: The server timezone value 'UTC' is unrecognized or represents
  more than one timezone. You must configure either the server or JDBC driver (via
  the serverTimezone configuration property) to use a more specifc timezone value if
  you want to utilize timezone support.

Why is this happening?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
bluecollarcoder
  • 14,339
  • 4
  • 22
  • 18

35 Answers35

829

Apparently, to get version 5.1.33 of MySQL JDBC driver to work with UTC time zone, one has to specify the serverTimezone explicitly in the connection string.

jdbc:mysql://localhost/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
Ivar
  • 6,138
  • 12
  • 49
  • 61
bluecollarcoder
  • 14,339
  • 4
  • 22
  • 18
  • 5
    According to [docs](https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html) useJDBCCompliantTimezoneShift has no effect when using useLegacyDatetimeCode=false. Therefore it isn't needed there... – matof Nov 27 '15 at 08:15
  • 29
    This solves my error. Additional note, escape the & with & in persistence.xml file : – pdem Apr 18 '16 at 16:14
  • 5
    That's not correct. The point of useLegacyDatetimeCode=false is not having to specify serverTimezone so the client corrects timezone diferences. It's a bug in that version of MySQL client. – Aníbal Oct 31 '16 at 11:14
  • I had to use & instead of & while configuring this in my tomcats server.xml – user2478236 Nov 08 '17 at 08:45
  • 5
    This solution ruins the time zone except for GMT. I think the right solution is [underrated one below](https://stackoverflow.com/a/44720416/1601909) – DuncanSungWKim Dec 13 '17 at 20:40
  • Seems I have that situation: https://stackoverflow.com/questions/50066795/mysql-connector-java-upgrade-to-8-0-11-changes-all-the-date-values-retrieved-fro – Manuel Jordan Apr 27 '18 at 17:30
  • This works in Glassfish or Payara in the specification of a JDBC data source as well when putting each URL parameter and value above in a connection property. – Kalle Richter Jul 30 '18 at 08:46
  • I got this error in PhpStorm when trying to connect to database, fixed by adding my timezone url encoded: ?serverTimezone=Europe%2FVilnius to the connection string – Darius.V Apr 11 '19 at 07:21
  • `?serverTimezone=UTC` was it. – Lauri Elias Jul 15 '19 at 12:56
  • 1
    solution works with 8.0.17. Happened with a fresh MySQL installation. Can't believe that this bug hasn't been fixed after so many years. – Tilman Hausherr Aug 28 '19 at 18:37
  • Thanks this worked just with an append of a # on UTC. I ran the same code on the same driver just yesterday on another machine and it worked just fine. – TrevorLee Oct 03 '19 at 12:19
  • solution works with 8.0.19. Looks like still an issue with MySQL. – wallwalker Mar 29 '20 at 16:28
  • So if the issue happens with Tomcat but the error arises from mysql, where would one apply this fix since I am not directly interacting with mysql? – demongolem Apr 07 '20 at 14:46
  • This is the simplest way to fixe the issue: It happened to me too with the latest 5.4.15 version. But fixed with this: adding "?serverTimezone=" + TimeZone.getDefault().getID()" right next to ur database, solves the issues. eg "jdbc:mysql://localhost:3306/yourDatabase/?serverTimezone=" + TimeZone.getDefault().getID(); – Tes May 14 '20 at 03:12
  • @DuncanSungWKim I don't know if the context is the same, but I managed to fix the time zone being ruined by adding in my Spring project `@PostConstruct void started() { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); }`. – GuiRitter May 31 '20 at 14:37
  • I have faced the same and solved with following: jdbc:mysql://localhost/db?useUnicode=true&serverTimezone=CET – zacoder Jul 14 '20 at 14:32
  • When migrating legacy code to newer mysql db and drivers, I found that setting serverTimezone=UTC skews all of the queries for the shift. My servers and application are in Eastern timezone (America/Detroit). I solved my problem by using serverTimezone=serverTimezone=EST5EDT. My connection string for mysql driver version 8 is: jdbc:mysql://localhost:3306/dbname?useLegacyDatetimeCode=false&serverTimezone=EST5EDT&useUnicode=true&characterEncoding=utf8&autoReconnect=true – user2989397 Jul 14 '20 at 16:09
  • Worked liked a charm – Spiff Jekey-Green Oct 15 '22 at 17:02
146

I've solved this problem by configuring MySQL.

SET GLOBAL time_zone = '+3:00';

Dmitriy Rud
  • 1,469
  • 1
  • 8
  • 2
  • 8
    if you're using MSK timezone it's +3, then you might use folowing as a db address: `jdbc:mysql://localhost/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Moscow`. It looks like mysql-connector does not understand short timezone names. – babay Feb 27 '17 at 14:16
  • 4
    What do you do when Daylight Savings Time changes the clock? – isapir Oct 08 '18 at 21:33
  • 4
    With mysql 8.0 you can call "set persist time_zone = '+00:00';" to set it to UTC persistently, without need to edit my.cnf or restart the server. See https://mysqlserverteam.com/mysql-8-0-persisting-configuration-variables/ – ccleve Mar 19 '19 at 17:07
  • If you are setting this via a unique manual SQL query, this setting would revert to what the original value was after a DB restart. – CBA110 Jul 06 '19 at 15:27
  • does work - remember to modify your local timezone string in place of +3 SET GLOBAL time_zone = '+3:00'; – Pravin Bansal Jun 04 '20 at 05:33
82

After reading several posts on this topic, testing different configurations and based on some insights from this mysql bug thread that's what I have understood:

  • the server time zone is important in particular to convert dates stored in the database to the time zone of the application server. there are other implications but this is the most noticeable one
  • GMT x UTC time zone systems. GMT was conceived in the late 19th century and can be shifted between standard time and summer time. this property could lead to a situation where the database server shifts to summer time and the application doesn't notice it (perhaps there are other complications but I didn't research further). UTC does not vary over time (it is always within about 1 second of mean solar time at 0° longitude).
  • serverTimeZone definition was introduced in mysql jdbc connectors versions 5.1 ahead. until version 8 it could be ignored with useLegacyDatetimeCode=true , which in conjunction with useJDBCCompliantTimezoneShift=true would make the application get the database time zone on every connection. In this mode GMT time zones such as 'British Summer Time' would be converted to the internal java/JDBC format. New time zones could be defined in a .properties file such as this one
  • Starting with jdbc driver version 8, automatic time matching (useJDBCCompliantTimezoneShift) and legacy time format (useLegacyDatetimeCode) were removed (see mysql jdbc connector changelog). therefore setting these 2 parameters has no effect as they are completely ignored (new default is useLegacyDateTimeCode=false)
  • In this manner setting serverTimezone became mandatory if any of the time zones (application/database servers) are not in the format 'UTC+xx' or 'GMT+xx'
  • There is no impact of setting server time as UTC (for instance with jdbc:mysql://localhost:3306/myschema?serverTimezone=UTC, even if your application / database servers are not in this timezone. The important is for the application connection string + database to be synchronized with the same time zone. In different words, simply setting serverTimezone=UTC with a different time zone on the database server will shift any dates extracted from the database
  • The MySQL default time zone can be set to UTC+0 with the my.ini or my.cnf files (windows / linux respectively) by adding the line default-time-zone = '+00:00' (details in this StackOverflow post)
  • Databases configured on AWS (amazon web services) are automatically assigned UTC+0 default time (see AWS help page here)
epol
  • 1,022
  • 8
  • 18
  • 2
    Nice answer, thanks. The various bullets are all useful. I went with the the suggestion to put a `default-time-zone = '+00:00'` into the homebrew `/usr/local/etc/my.cnf` file. It seems that the spaces around the `=` are important though so you might edit that bullet to include them. – Mark Edington Apr 24 '19 at 02:46
55

If you are using Maven, you can just set another MySQL connector version (I had the same error, so I changed from 6.0.2 to 5.1.39) in pom.xml:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>

As reported in another answers, this issue has been fixed in versions 6.0.3 or above, so you can use the updated version:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.3</version>
</dependency>

Maven will automatically re-build your project after you save the pom.xml file.

Priyantha
  • 4,839
  • 6
  • 26
  • 46
41

The connection string should be set like this:

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

If you are defining the connection in an xml file (such as persistence.xml, standalone-full.xml, etc..), instead of & you should use &amp; or use a CDATA block.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Alireza Alallah
  • 2,486
  • 29
  • 35
38

It worked for me just by adding serverTimeZone=UTC on application.properties.
spring.datasource.url=jdbc:mysql://localhost/db?serverTimezone=UTC

ColdFire
  • 1,301
  • 13
  • 14
  • 1
    When attempting to connect to MySQL from MacOS with EDT time zone, got exception. This solution worked for me. Just adding *?serverTimezone=UTC* to the end of the JDBC URL was sufficient. – RafiAlhamd Jul 10 '20 at 07:42
  • I'm using spring boot 2.3.1 and mysql connector 8.0.21 and this worked for me. – Spencer Stewart Jul 24 '20 at 15:57
34

I solved putting below connection string in the URL

jdbc:mysql://localhost:3306/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
Nitishkumar Singh
  • 1,781
  • 1
  • 15
  • 33
  • 1
    That's not correct. The point of useLegacyDatetimeCode=false is not having to specify serverTimezone so the client corrects timezone diferences. – Aníbal Apr 16 '19 at 12:13
29

This is a bug in mysql-connector-java from version 5.1.33 to 5.1.37. I've reported it here: http://bugs.mysql.com/bug.php?id=79343

Edited: This has been corrected from mysql-connector-java 5.1.39

It was a typo in TimeUtil class in loadTimeZoneMappings method that raises a NPE locating /com/mysql/jdbc/TimeZoneMapping.properties file. If you look at the code, the file should be located within TimeUtil class loader, not TimeZone:

TimeUtil.class.getResourceAsStream(TIME_ZONE_MAPPINGS_RESOURCE);

The parameter useLegacyDatetimeCode allows to correct the difference between client and server timezones automatically when using dates. So it helps you precissely not having to specify timezones in each part. Althought using serverTimeZone parameter is a workaround, and meanwhile the patch is released, you can try better correcting the code by yourself as I did.

  • If it's a standalone application, you can try simply to add a corrected com/mysql/jdbc/TimeUtil class to your code and be careful with jar loading order. This can help: https://owenou.com/2010/07/20/patching-with-class-shadowing-and-maven.html

  • If it's a web application, the easier solution is to create your own mysql-connector-java-5.1.37-patched.jar, substituting the .class directly into the original jar.

Aníbal
  • 785
  • 8
  • 24
  • Sweet, thanks for reporting this. Glad someone has been able to pin down the bug. Do you know when the fix will be released? – bluecollarcoder Feb 09 '16 at 18:19
  • The solution you are suggesting is great, but I think modifying the driver source and managing the Maven dependency is probably too annoying for most people. – bluecollarcoder Feb 09 '16 at 18:21
  • The fix has been already commited in their repo but didn't go in the last release 5.1.38. They use to release every few months so I hope it could be soon. Meanwhile I can upload you my jar, if you want it. – Aníbal Feb 14 '16 at 21:19
  • Corrected in mysql-connector-java 5.1.39. – Aníbal May 06 '16 at 17:16
  • @doom777 This was fixed on the 6.x branch in version 6.0.3. See https://bugs.mysql.com/bug.php?id=81214 – Gili Dec 20 '16 at 16:49
  • 4
    @Gili This is not fixed as of release 6.0.6 – Imme22009 Aug 21 '17 at 01:45
  • 6
    The bug is stil present in 8.0.11 – John Little Apr 05 '19 at 22:40
  • 3
    @JohnLittle I've got this problem too in 8.0.15, but it's not caused by the bug any more. Timezones are loaded correctly, but CET and CEST (these timezones cause trouble to me) are not included neither in the `TimeZone.getAvailableIDs()` nor in `TimeZoneMapping.properties` so this solution won't help here. The solution would probaby be setting like `serverTimezone=Europe/Berlin` – JPT Apr 13 '19 at 11:52
  • @JohnLittle @JPT I faced this problem with `8.0.15` too. So I downgraded my connector jar file to `mysql-connector-java-5.1.47.jar` and I got it solved. As mentioned in this answer I used the `5.1.47` version (after `5.1.39` version) and it got solved. – Suhan Dharmasuriya Apr 30 '19 at 11:31
  • Problem still exists in 8.0.19 – Barry Chapman Oct 22 '20 at 20:03
27
  1. I added in mysql config file in section [mysqld]

    default_time_zone='+03:00'
    
  2. And restart mysql server:

    sudo service mysql restart
    

Where +03:00 my UTC time zone.

Path to config file on my os ubuntu 16.04:

/etc/mysql/mysql.conf.d/mysqld.cnf

WARNING: IF YOUR TIME ZONE HAVE SUMMER AND WINTER TIME. YOU MUST CHANGE UTC IN CONFIG IF CHANGE TIME. TWICE IN YEAR(USUALLY) OR SET CRONTAB WITH SUDO.

My url jdbc connection:

"jdbc:mysql://localhost/java"
Fortran
  • 2,218
  • 2
  • 27
  • 33
  • 1
    Having to restart Mysql is basically a nonstarter in almost all production use cases. It's becomes even more of a problem when replications are involved. – bluecollarcoder Jun 26 '17 at 16:53
  • @bluecollarcoder Require add only in [mysqld] section. Or add [mysqld] section if not has it section. Example my config https://pastebin.com/j4F7t2KS – Fortran Jun 27 '17 at 14:08
  • 1
    I updated my Linux server's /etc/localtime from /usr/share/zoneinfo/US/Pacific to /usr/share/zoneinfo/America/Los_Angeles then restarted mysql service and this fixed the issue for me. – vinnyjames Jan 31 '19 at 22:18
  • In my case for syntax provided, there was an error when restarting, and the correct syntax was: `default-time-zone='+03:00'` instead, [as per this answer](https://stackoverflow.com/a/19069310/6439240). Also coming from DBeaver. – wscourge Apr 04 '19 at 05:15
  • it s not appropriate if you have to tell to every developers in your company to alter their MySQL config :) – pheromix Apr 05 '19 at 14:39
18

The above program will generate that time zone error.

After your database name you have to add this: ?useTimezone=true&serverTimezone=UTC. Once you have done your code will work fine.

Best of luck :)

Obsidian
  • 3,719
  • 8
  • 17
  • 30
Aathil Ahamed
  • 460
  • 4
  • 16
17

I executed following on my database side.

mysql> SET @@global.time_zone = '+00:00';

mysql> SET @@session.time_zone = '+00:00';

mysql> SELECT @@global.time_zone, @@session.time_zone;

I am using Server version: 8.0.17 - MySQL Community Server - GPL

source: https://community.oracle.com/thread/4144569?start=0&tstart=0

Vishrant
  • 15,456
  • 11
  • 71
  • 120
16

I have the same problem and i solved it append only "?serverTimezone=UTC" to my string connection.

#

sinossi my problem:

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.

my dbDriver = com.mysql.jdbc.Driver

my jar = mysql-connector-java-8.0.12.jar

my java = 1.8

my tomcat = Apache Tomcat Version 8.5.32

my MySql server = MySql ver.8.0.12 
Inder
  • 3,711
  • 9
  • 27
  • 42
15

Everything that we need to fix the problem with serverTimezone:

String url = "jdbc:mysql://localhost:3306/db?serverTimezone=" + TimeZone.getDefault().getID()
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Ingvar
  • 151
  • 1
  • 2
  • It happened to me too with the latest 5.4.15 version. But fixed with this: adding "?serverTimezone=" + TimeZone.getDefault().getID()" right next to ur database, solves the issues. – Tes May 14 '20 at 03:11
14

You can use the MySQL connector in the Maven dependency,

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.14</version>
</dependency>

Then you need the set the right parameters in the application.properties file,

spring.datasource.url=jdbc:mysql://localhost:3306/UserReward?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=testuser
spring.datasource.password=testpassword
# MySQL driver
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Arefe
  • 11,321
  • 18
  • 114
  • 168
14

There is no impact of setting server time as UTC (for instance with jdbc:mysql://localhost:3306/myschema?serverTimezone=UTC, even if your application/database servers are not in this timezone. The important is for the application connection string + database to be synchronized with the same time zone.

In other words, simply setting serverTimezone=UTC with a different time zone on the database server will shift any dates extracted from the database

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
Hermann N'ZI
  • 131
  • 1
  • 3
11

I'm using mysql-connector-java-8.0.13 and had the same problem. I created my database in the command line console and solved this problem by using @Dimitry Rud's solution on the command line:

SET GLOBAL time_zone = '-6:00';

I didn't need to restart anything, set the time and immediately run my code in eclipse, it connected with no problems.

The bug is supposed to be fixed in an older version, but I think I got this error because after I created the database in the console, I didn't set this. I'm not using workbench nor another app to manage this rather than the console.

Rene Knop
  • 1,788
  • 3
  • 15
  • 27
RAdrian
  • 111
  • 1
  • 3
10

Apparently, to get version 5.1.33 of MySQL JDBC driver to work with UTC time zone, one has to specify the serverTimezone explicitly in the connection string.

spring.datasource.url = jdbc:mysql://localhost:3306/quartz_demo?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
harun ugur
  • 1,718
  • 18
  • 18
7

From mysql workbench run the following sql statements:

  1. SET @@global.time_zone = '+00:00';
  2. SET @@session.time_zone = '+00:00';

with the following sql statements check if the values were set:

SELECT @@global.time_zone, @@session.time_zone;

toof06
  • 81
  • 1
  • 4
7

This worked for me.

on DBeaver 6.0 : Go to Connection Settings > Driver Properties > Server Time Zone > Set UTC.

Also, in spring boot config, had to set below property.

jdbc:mysql://localhost:/?serverTimezone=UTC

Anil Gowda
  • 71
  • 2
  • 3
6
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/resultout? useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root",""))

This is actually the solution to this problem, but don't just copy and paste it in your program. If you just read the line you will find 'resultout', that's the name of my database, and you have to write your's.

There are three string components, first one is url, second is username, and third one is password. In above paragraph we cleared, url. The second and third String components as said your username and password you have to change accordingly.

Thanks

Deathstorm
  • 818
  • 11
  • 36
5

i Got error similar to yours but my The server time zone value is 'Afr. centrale Ouest' so i did these steps :

MyError (on IntelliJ IDEA Community Edition):

    InvalidConnectionAttributeException: The server time zone value 'Afr. centrale Ouest' 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 u....

I faced this issue when I upgraded my mysql server to SQL Server 8.0 (MYSQL80).

The simplest solution to this problem is just write the below command in your MYSQL Workbench -

  SET GLOBAL time_zone = '+1:00'

The value after the time-zone will be equal to GMT+/- Difference in your timezone. The above example is for North Africa(GMT+1:00) / or for India(GMT+5:30). It will solve the issue.

Enter the Following code in your Mysql Workbench and execute quesry

[source link for question/problem ]

[source link for answer]

[Solution ScreenShot ]

iifast2
  • 303
  • 1
  • 3
  • 9
4

I had the same problem when I try to work with spring boot project on windows.

Datasource url should be:

spring.datasource.url=jdbc:mysql://localhost/database?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

4

Run below query to mysql DB to resolve the error

MariaDB [xxx> SET @@global.time_zone = '+00:00';
Query OK, 0 rows affected (0.062 sec)

MariaDB [xxx]> SET @@session.time_zone = '+00:00';
Query OK, 0 rows affected (0.000 sec)

MariaDB [xxx]> SELECT @@global.time_zone, @@session.time_zone;
Yann
  • 2,426
  • 1
  • 16
  • 33
4

I have added the following line to my /etc/mysql/my.cnf file:

default_time_zone='+00:00'

Restarted the MySQL server:

systemctl restart mysql

And it works like a charm.

tedi
  • 6,350
  • 5
  • 52
  • 67
4

Setting the time zone by location for a spring boot application inside the application.properties file to

spring.datasource.url=jdbc:mysql://localhost:3306/db?serverTimezone=Europe/Berlin

resolved the problem for the CET / CEST time zone. The pom.xml uses the maven artifact

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.22</version>
    </dependency>
alex
  • 2,464
  • 23
  • 32
3

I also was having the exact same problem in LibreOffice Base. So I just specified a non 'daylight savings time zone' in the connection string.
**enter image description here**

I tried without the "&serverTimezone=MST" but that failed as well.

I also tried "&serverTimezone=MDT" and that failed, so for some reason, it doesn't like daylight savings time!

GordR
  • 91
  • 8
2

In my case, it was a test environment and I had to make an existing application to work without any configuration changes, and if possible without any MySQL config changes. I was able to fix the issue by following @vinnyjames suggestion and changing server timezone to UTC:

ln -sf /usr/share/zoneinfo/UTC /etc/localtime
service mysqld restart

Doing that was enough for me to solve the issue.

Dmitriusan
  • 11,525
  • 3
  • 38
  • 38
2

my.ini

At the end add this line:

default-time-zone = '+05:30'

from the terminal run this command

>> sudo mysql -e "SET GLOBAL time_zone = ‘+5:30’;" -u root
>> sudo mysql -e "SELECT @@global.time_zone;" -u root

If both of them do not work try using this request from sqoop using terminal

>> sqoop list-databases --connect "jdbc:mysql://localhost/employees?serverTimezone=UTC" --username sqoop -P

Or you can just replace your request URL with this

jdbc:mysql://localhost/employees?serverTimezone=UTC
1

I solved this issue without any single code change. just goto system time setting and set the time zone. In my case the default time zone was UTC which I changed to my local time zone. After I did restart all services, everything worked for me.

1

I am late, But If you are struggling through the following error and using datasource(javax.sql.DataSource):

The server time zone value 'CEST' is unrecognized or represents more than one time zone.

Set following line to get rid of the error:

MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerTimezone("UTC");
Rituraj Singh
  • 579
  • 1
  • 5
  • 16
1

I faced the same error and in my case, I change the Server Port Number to 3308 where previously it was 3306. This connect my project to the MySQL database.

enter image description here

Here we have to change the connection code also.

Class.forName("com.mysql.cj.jdbc.Driver");
cn=(java.sql.Connection)DriverManager.getConnection("jdbc:mysql://localhost:3308/test2?zeroDateTimeBehavior=convertToNull","root","");

Changing the port number in the connection code is also necessary as localhost:3308 to resolved the error.

Also, the admin properties in my case. enter image description here

1

The connection string worked for me this

jdbc:mysql://localhost/<yourDbName>?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

I am using Gradle my build.gradle file is like this

dependencies {
    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.17'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
Rossi Alex
  • 81
  • 1
  • 2
0

Agree with @bluecollarcoder answer, but it's better to use TimeZone.getDefault().getID(); at the end of the connection string:

"jdbc:mysql://localhost/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=" + TimeZone.getDefault().getID();  

In this case Timezone parameter automatically updates depending on the local machine timezone.

Yerbol
  • 43
  • 3
  • That's not correct. The point of useLegacyDatetimeCode=false is not having to specify serverTimezone so the client corrects timezone diferences. – Aníbal Apr 16 '19 at 12:13
-2

I also got the same running java JDBC in NetBeans. This is how it fixed

I use Xampp. In conf button of Apache I opened httpd.conf file and on first line I typed

# Set timezone to Europe/Athens UTC+02:00 
SetEnv TZ Europe/Athens.

In conf button of MySQL I opened my.ini file and on last line I typed "Europe/Athens"

Stopped and started both Apache and MySQL

Problem fixed.

*(Local mechine time zone is different, but no problem.)

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
-2

Just modify the connection string with the following code in the application.properties file.


spring.datasource.url=jdbc:mysql://localhost:3301/Db?
   useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=
   false&serverTimezone=UTC

Michael Nelles
  • 5,426
  • 8
  • 41
  • 57
Mittal
  • 11
  • 1