执行遇到的错误
在学习javaweb尝试MyBatis查询时,执行遇到错误
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.
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: 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.
### The error may exist in UserMapper.xml
### The error may involve UserMapper.selectAll
### The error occurred while executing a query
### Cause: 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.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:135)
at com.blog.MyBatisDemo.main(MyBatisDemo.java:22)
错误原因
1、错误一
正在加载的com.mysql.jdbc.Driver
类已经过时,应该使用新的驱动类com.mysql.cj.jdbc.Driver
。这通常是因为你的代码或配置文件中直接指定了旧的驱动类名。解决方法是更新你的代码或配置文件,使用新的驱动类名。
2、错误二
错误信息显示“服务器时区值无法识别或代表多个时区”,这是由于MySQL服务器和JDBC驱动程序之间时区配置不匹配导致的。具体错误信息为:“The server time zone value '�й���ʱ��' is unrecognized...”。解决这个问题的方法是在连接字符串中明确指定时区。你需要在数据库连接URL中添加serverTimezone
参数,设置为你期望的具体时区,例如设置为UTC时区
解决办法
1、针对错误一
更新驱动类名:确保你的代码或配置文件中使用的是
com.mysql.cj.jdbc.Driver
作为驱动类名,替代旧的com.mysql.jdbc.Driver
2、针对错误二
配置时区:在数据库连接URL中添加serverTimezone
参数,确保时区正确配置。
要在 JDBC 连接 URL 中配置时区,需要添加 serverTimezone
参数
<property name="url" value="jdbc:mysql:///mybatis?useSSL=false&serverTimezone=UTC"/>