连接数据库报时区错误的解决方法

当在Servlet中连接MySQL数据库时遇到时区错误,错误信息提示未配置服务器时区,解决办法是在数据库连接URL后添加参数`serverTimezone=UTC`,例如:`jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true`。这样设置字符编码和时区后,即可避免时区不识别的问题。

连接数据库报时区错误的解决方法

如果我们在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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值