intellij idea报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one

在SpringBoot2.0+Mybatis+MySQL项目中遇到服务器时区错误,原因是MySQL时区设置不正确。解决方法是修改MySQL配置文件my.ini,在[mysqld]节点下添加时区配置,重启MySQL服务,将时区设置为+8:00,从而消除报错。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、报错内容

用springboot2.0+mybatis+mysql搭建的项目中,项目运行时报错:
在这里插入图片描述

二、出错原因

出错的原因是mysql的时区值设置的不正确。mysql默认的时区值是美国,中国的时区要比美国晚8小时,需要采用+8:00的格式
我们登录mysql,查看当前的时区值,发现时区值是system

mysql -uroot -proot123 //登录mysql
show variables like'%time_zone'; //查看mysql中设置的时区值

在这里插入图片描述

三、解决步骤

解决方法就是修改mysql的配置文件。

1、找到配置文件my.ini,用notepad打开
在这里插入图片描述
2、搜索[mysqld],在[mysql]节点下加上这一行

default-time-zone='+08:00'

我的代码package hotel; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; /**jdbc快速入门demo*/ public class JDBCDemo2 { public static void main(String[] args) throws Exception { // Class.forName("com.mysql.jdbc.Driver"); //注册驱动 String url ="jdbc:mysql://127.0.0.1:3306/hotel?useSSL=false"; String username = "root"; String password = "Lqh2005320"; //获取链接 Connection conn = DriverManager.getConnection(url,username,password); //执行sql语句 String sql= "update room set stock = 15where id = 1"; Statement stmt = conn.createStatement();//获取对象statement int count = stmt.executeUpdate(sql); } } 出现错误D:\程序\bin\java.exe "-javaagent:C:\Users\moon\IntelliJ IDEA 2024.1.1\lib\idea_rt.jar=3640:C:\Users\moon\IntelliJ IDEA 2024.1.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\tem\jdbc\out\production\jdbc-demo;D:\tem\jdbc\jdbc-demo\lib\mysql-connector-java-8.0.11.jar hotel.JDBCDemo2 Exception in thread "main" java.sql.SQLException: The server time zone value 'ÖÐ&sup1;ú±ê&times;&frac14;ʱ&frac14;ä' 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/java.sql.DriverManager.getConnection(DriverManager.java:683) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:230) at hotel.JDBCDemo2.main(JDBCDemo2.java:15) Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'ÖÐ&sup1;ú±ê&times;&frac14;ʱ&frac14;ä' 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 java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) 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) ... 6 more 进程已结束,退出代码为 1
最新发布
03-29
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值