解决Java应用程序中的SQLException:服务器时区值未识别问题;MySQL连接问题:服务器时区值 ‘Öйú±ê׼ʱ¼ä‘ 未被识别的解决方法

目录

​编辑

问题背景

解决方案


问题背景

今天遇见一个这个问题,解决后发出来分享一下:

java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' 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:127) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at BookManagement.<init>(BookManagement.java:22) at BookManagement.main(BookManagement.java:64) Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' 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.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83) at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128) at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201) at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225) at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391) at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:993) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852) ... 7 more


解决方案

 下面是我问题的解决。

此错误是由于 MySQL JDBC 驱动程序在尝试确定服务器的时区时遇到问题。为了解决这个问题,你可以在 JDBC URL 中明确指定 serverTimezone 参数。

在你的 BookManagement 类中,找到以下连接字符串:

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/BookManagement?useSSL=false", "root", "mysecret");

并更改为:

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/BookManagement?useSSL=false&serverTimezone=UTC", "root", "mysecret");

 

这里,我们添加了 &serverTimezone=UTC 参数,以明确告诉 JDBC 驱动程序使用 UTC 时区。当然,你可以根据实际需要选择其他时区,但 UTC 通常是一个安全的选择。

应用此更改后,再次尝试运行你的程序。这应该会解决你遇到的时区问题。


这个问题的通用解决方案

这个问题是由于MySQL服务器和JDBC驱动程序之间的时区不匹配引起的。为了解决这个问题,你可以采取以下步骤:

  1. 配置JDBC驱动程序的时区属性:在连接到MySQL数据库之前,确保你的Java应用程序中的JDBC驱动程序已经配置了正确的时区属性。你需要将serverTimezone属性设置为与MySQL服务器时区匹配的值。例如,如果你的MySQL服务器位于UTC时区,可以将此属性设置为"UTC"。

String url = "jdbc:mysql://localhost:3306/your_database?serverTimezone=UTC";
Connection connection = DriverManager.getConnection(url, "your_username", "your_password");
  1. 更新MySQL服务器时区设置:确保你的MySQL服务器的时区设置与JDBC驱动程序配置的时区匹配。你可以在MySQL服务器上执行以下SQL命令来更改时区设置:

SET GLOBAL time_zone = 'UTC';

 

  1. 注意:这只是一个示例,你应该将时区设置为你实际使用的时区。

  2. 重启MySQL服务器:在更改了MySQL服务器的时区设置后,重新启动MySQL服务器以确保更改生效。

  3. 重新运行应用程序:重新运行你的Java应用程序,查看是否仍然出现时区相关的错误。

这些步骤应该能够解决这个问题,确保MySQL服务器和Java应用程序之间的时区匹配。如果问题仍然存在,你可以考虑查看MySQL服务器的时区配置,以确保它与你的预期一致。此外,检查你的JDBC连接字符串和驱动程序版本是否正确配置也是很重要的。

🌌点击下方个人名片,交流会更方便哦~
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

命运之光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值