连接数据库报时区错误的解决方法
如果我们在servlet中连接数据库时,控制台出现了如下错误:
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.
那么就是因为我们没有设置时区所导致的错误:
修改方法:
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
//报错的原因是我们没有给数据库设置时区,此时我们应该就是给数据库设置时区
//在jdbc:mysql://localhost:3306/test语句后加上下列语句(注意问号千万别忘记加上)
// ?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
//characterEncoding设置的是字符编码,serverTimezone设置的是时区,将该数据放上去便可以解决上面的错误了
state = conn.createStatement();
当在Servlet中连接MySQL数据库时遇到时区错误,错误信息提示未配置服务器时区,解决办法是在数据库连接URL后添加参数`serverTimezone=UTC`,例如:`jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true`。这样设置字符编码和时区后,即可避免时区不识别的问题。

341

被折叠的 条评论
为什么被折叠?



