java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '=' 异常处理,及MySQL数据库编码设置

时间:2022-12-23 02:09:04

java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '=',意思是说字符编码不一样,不能进行比较,也就是说数据库内部的编码都不一样,有的数据是latin1_swedish_ci,有的数据是utf8_general_ci,,因此解决此问题的核心就是将数据库所有的编码进行统一。

1、查看数据库编码,使用sql语句:show variables like 'character_set_%';

正确的如下图:

java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT)  and (utf8_general_ci,COERCIBLE) for operation '=' 异常处理,及MySQL数据库编码设置

如果编码不对,可使用以下sql语句进行修改:

set character_set_client=utf8;

set character_set_connection=utf8;

set character_set_database=utf8;

set character_set_results=utf8;

set character_set_server=utf8;

set character_set_system=utf8;

2、查看排序规则,使用sql语句:show variables like 'collation_%';

正确的如下图:

java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT)  and (utf8_general_ci,COERCIBLE) for operation '=' 异常处理,及MySQL数据库编码设置

如果不对,可使用以下sql语句进行修改:

set collation_connection=utf8;

set collation_database=utf8;

set collation_server=utf8;