使用Java连接MySQL数据库出现如下报错:
Exception in thread “main” 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.
- 源代码
//2. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//3. 获取数据库连接对象
Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
修改
在url后面添加
?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
修改后的代码如下:
//2. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//3. 获取数据库连接对象
Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "123456");