java与mysql的连接字符串不匹配问题:java.sql.SQLException: Unknown initial character set index ‘255’ received from server. Initial client character set can be forced via the ‘characterEncoding’ property.
1.错误信息:
java.sql.SQLException: Unknown initial character set index ‘255’ received from server. Initial client character set can be forced via the ‘characterEncoding’ property.
2.错误产生原因:
Mysql驱动与数据库字符集设置不匹配(可能是Mysql版本过高,在8.0版本及以上;也可能是jre版本过高。),导致系统环境变量受影响。
3.不匹配的原因:
(1)一般来说5.1匹配的是mysql8.0以下的版本
(2)8.0版本的mysql-connector-java才是匹配MySql的8.0版本
4.解决办法(主要针对MySQL版本过高):
(1)下载与MySQL相匹配的jre版本
*(2)修改连接时的url以及Class.forName()
①url的修改:String url = "jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL"
;
(亲测有效,更全面)
【也可以改为:String url = "jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8
】
②Class.forName()的修改:Class.forName("com.mysql.cj.jdbc.Driver");
(在mysql后加上.cj)
5.问题解决: