IDEA连接MySQL数据库问题

IDEA与MySQL的连接问题

参考大佬博客,链接:https://blog.csdn.net/swi_0521/article/details/89104748#comments

### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is 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连接MySql时出现: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
 

后来查阅资料发现这都是因为安装mysql的时候时区设置的不正确 mysql默认的是美国的时区,而我们中国大陆要比他们迟8小时,采用+8:00格式。使用的数据库是MySQL,在你没有指定MySQL驱动版本的情况下它自动依赖的驱动是8.0.12很高的版本,这是由于数据库和系统时区差异所造成的,在jdbc连接的url后面加上serverTimezone=GMT即可解决问题,如果需要使用gmt+8时区,需要写成GMT%2B8,否则会被解析为空。再一个解决办法就是使用低版本的MySQL jdbc驱动,5.1.28不会存在时区的问题。
解决方法:
在连接字符串后面加上?serverTimezone=UTC
其中UTC是统一标准世界时间。
完整的连接字符串示例:jdbc:mysql://localhost:3306/test?serverTimezone=UTC 
另外:jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8,可以解决中文乱码输入问题;

所以也可以和上面的一起结合:

jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC


另一种情况改成:jdbc:mysql://localhost:3306/test?serverTimezone=GMT 之后才行。
jdbc是mysql-connector-java-8.0.11.jar,设置过时区 set time_zone = ‘+8:00’

总结:出现这个问题先看自己系统设置的时区是UTC还是GMT,再找对应的解决办法

你可能感兴趣的:(IDEA与数据库)