数据库连接报错:The server time zone value '�й���׼ʱ��' is unrecognized

本文解析了在使用Maven框架结合原生JDBC进行MySQL操作时遇到的时区配置错误,并提供了两种解决方案:一是降低MySql驱动版本至5.1.46;二是若需使用高版本驱动,则在连接字符串中明确指定serverTimezone参数。

最近使用maven框架学习原生JDBC时遇到了如下错误:

Exception in thread "main" 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.

查询资料后发现导致报错的原因是使用的MySql驱动版本过高导致的,如下:

最终的解决办法有两种:

①降低使用的MySql驱动版本
 

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

②如果必须使用高版本驱动的话,在使用驱动管理器获取连接对象的数据库地址中加入serverTimezone=UTC

Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/batistest?serverTimezone=UTC&characterEncoding=utf8",
                "root",
                "root");

 

当 Java 使用 `DriverManager.getConnection` 连接数据库时出现 “The server time zone value 'й���׼ʱ��' is unrecognized...” 错误,通常是因为 JDBC 驱动程序无法识别数据库服务器返回的时区值。解决办法是在数据库连接 URL 中明确指定一个具体的时区。 以下是具体的解决步骤和示例代码: ### 解决步骤 在数据库连接 URL 中添加 `serverTimezone` 参数,并指定一个具体的时区。常见的时区值有 `GMT`、`Asia/Shanghai` 等。 ### 示例代码 ```java import java.sql.*; import java.util.ArrayList; import java.util.List; class emp { private int id; private String name; public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } } public class Main { public static void main(String[] args) { Connection connection = null; CallableStatement callableStatement = null; ResultSet resultSet = null; List<emp> ls = new ArrayList<>(); try { // 加载 JDBC 驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 建立数据库连接,指定时区为 Asia/Shanghai connection = DriverManager.getConnection("jdbc:mysql:///demo1026?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai", "zhang", "1997"); // 准备 SQL 语句 callableStatement = connection.prepareCall("select * from emp"); // 执行查询 resultSet = callableStatement.executeQuery(); // 处理查询结果 while (resultSet.next()) { emp emps = new emp(); emps.setId(resultSet.getInt("id")); emps.setName(resultSet.getString("name")); ls.add(emps); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (resultSet != null) resultSet.close(); if (callableStatement != null) callableStatement.close(); if (connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` ### 代码解释 - `Class.forName("com.mysql.cj.jdbc.Driver");`:加载 MySQL 的 JDBC 驱动。 - `DriverManager.getConnection`:建立数据库连接,在连接 URL 中添加了 `serverTimezone=Asia/Shanghai` 参数,明确指定时区为中国上海的时区。 - 后续代码为执行 SQL 查询并处理结果的常规操作。 ### 其他可选时区值 除了 `Asia/Shanghai`,还可以使用其他时区值,例如: - `GMT`:格林威治标准时间 - `UTC`:协调世界时 - `America/New_York`:美国纽约时区 根据实际需求选择合适的时区值。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值