问题

一些背景:

我在Tomcat 7上运行了一个Java 1.6 webapp。数据库是MySQL 5.5。以前,我使用Mysql JDBC驱动程序5.1.23连接到数据库。一切都有效。我最近升级到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.

为什么会这样?


#1 热门回答(216 赞)

显然,要使MySQL JDBC驱动程序的5.1.33版本与UTC时区一起使用,必须在连接字符串中明确指定serverTimezone

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

#2 热门回答(40 赞)

如果你正在使用Maven,你可以设置另一个MySQL连接器版本(我有相同的错误,所以我从6.0.2改为5.1.39)inpom.xml

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

如另一个答案中所述,此问题已在6.0.3或更高版本中修复,因此你可以使用更新版本:

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

保存33229999文件后,Maven将自动重新构建项目。


#3 热门回答(22 赞)

连接字符串应设置如下:

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

如果你在anxml文件中定义连接(例如,persistence.xml,standalone-full.xml等),而不是&,则应使用&amp;或使用aCDATAblock。


原文链接