背景知识:

我有一个运行在Tomcat 7上的Java 1.6 web应用程序。数据库为MySQL 5.5。之前,我使用Mysql JDBC驱动程序5.1.23连接到DB。一切工作。我最近升级到Mysql JDBC驱动程序5.1.33。升级后,Tomcat在启动应用程序时会抛出这个错误。

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.

为什么会这样?


当前回答

我也有同样的问题,我解决了它只附加”?serverTimezone=UTC"到我的字符串连接。

#

我的问题是:

java.sql.SQLException:服务器时区值“CEST”无法识别或表示多个时区。如果希望利用时区支持,则必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更特定的时区值。

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 

其他回答

显然,要使MySQL JDBC驱动程序5.1.33版本与UTC时区兼容,必须在连接字符串中显式指定serverTimezone。

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

运行下面的查询到mysql DB来解决这个错误

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;

连接字符串应该像这样设置:

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

如果你在xml文件(如persistence.xml, standalone-full.xml等)中定义连接,你应该使用&或者使用CDATA块。

同意@bluecollarcoder的答案,但最好使用TimeZone.getDefault().getID();在连接字符串的末尾:

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

在这种情况下,Timezone参数根据本地机器时区自动更新。

我也有同样的问题,我解决了它只附加”?serverTimezone=UTC"到我的字符串连接。

#

我的问题是:

java.sql.SQLException:服务器时区值“CEST”无法识别或表示多个时区。如果希望利用时区支持,则必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更特定的时区值。

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