Mysql 5.5和JDBC连接时出现的时区问题

本文介绍了如何解决使用Java连接MySQL数据库时遇到的三个常见问题:时区配置错误、驱动加载警告及字符集乱码问题。提供了具体的解决方案和更新的代码示例。

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

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.
问题显示:服务器时区的值无法识别或者代表一个以上的时区,如果想要使用时区支持,必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)去使用一个更详细的时间。

既然问题已经明白,那么修改代码即可

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?serverTimezone=UTC","root","");

然后就能成功连接Mysql了

问题二:那就是现在Mysql5.5已经不支持用下面这种的方式获取驱动了

Class.forName("com.mysql.jdbc.Driver");

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

上面提示说,已经不赞成加载“com.mysql.jdbc.Driver”这个类,新的驱动类为“com.mysql.cj.jdbc.Driver”。
Class.forName("com.mysql.cj.jdbc.Driver");

问题三:java代码将数据写入到数据库中乱码的问题

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?serverTimezone=UTC
                              &useUnicode=true&characterEncoding=utf-8","root","");
好啦,问题解决了,目前还在学习中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值