eclipse使用jdbc连接mysql出现“The server time zone value '�й���׼ʱ��' is unrecognized or represents more th”

在Eclipse中通过JDBC连接MySQL时遇到时区问题,错误提示'The server time zone value 'PRC' is unrecognized or represents more than one time zone.'。解决方法是在连接URL中添加参数`?serverTimezone=UTC`,例如:`jdbc:mysql://localhost:3306/monkey1024?serverTimezone=UTC`。同时注意在执行SQL语句时,不需要在末尾添加分号。

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

mysql中建表:

create table t_user(

Id int primary key auto_increment,

Name varchar(40),

Password varchar(40),

Email varchar(60),

Birthday date

);

插入数据:

 Insert into t_user(name,password,email,birthday)

Values('tiger','123456','tiger@163.com','1994-12-01'),

('rabbit','123456','tiger@163.com','1997-06-11'),

('sheep','123456','sheep@163.com','1995-07-15');

eclipse中查表

package com.monkey1024.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBC_Test01 {
    public static void main(String[] args) throws Exception {
        // 注册驱动
        //Class.forName("com.mysql.jdbc.Driver");
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 获取连接Connection
        Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/monkey1024?serverTimezone=UTC", "root", "123456");
        System.out.println("mysql connection success!");
        //得到执行sql语句的对象Statement
        Statement stmt=conn.createStatement();
        //执行sql语句,并返回结果
        ResultSet rs=stmt.executeQuery("select id,name,password,email,birthday from t_user");
        //处理结果
        while(rs.next()) {
            System.out.println(rs.getObject("id"));
            System.out.println(rs.getObject("name"));
            System.out.println(rs.getObject("password"));
            System.out.println(rs.getObject("email"));
            System.out.println(rs.getObject("birthday"));
            System.out.println("-----------------");
        }
        //关闭Connection
        rs.close();
        stmt.close();
        conn.close();
    }
}
注意:1) Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/monkey1024?serverTimezone=UTC", "root", "123456")中不添加?serverTimezone=UTC会出现如题报错。

           2)executeQuery中的sql不加“;”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值