【转载】MySQL 出现的时区问题

MySQL 出现的时区问题

使用低版本的MySQL jdbc驱动,不会存在时区的问题。

1.驱动版本

问题

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”。

2.时区问题

发现读出数据库中的时间相差几个小时,这是由于数据库和系统时区差异所造成的。

需要在数据库连接串中设置时区。

serverTimezone=GMT即可解决问题,如果需要使用GMT+8时区,需要写成GMT%2B8,否则会被解析为空

serverTimezone=GMT%2B8
连接串
jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT%2B8

3.乱码问题

发现读出数据库乱码问题,需要设置编码格式。

useUnicode=true
characterEncoding=utf-8

连接串
jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8

————————————————

原文链接:https://blog.csdn.net/jeikerxiao/article/details/86216915

# Java  mysql 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×