需要设置server character set , database character set, client character set,如果有一个不大一样,就有可能导致乱码
。
server character set,client character set可以在mysql config文件中设置,比如设置为utf8:
vi /etc/my.cnf
# server
[mysqld]
default_character_set=utf8
# client
[mysql]
default_character_set=utf8
然后重启mysqld。设置database 字符集,可以在mysql中进行。对于新建database:
create database database_name default character set utf8;
对于已存在database:
alter database database_name default character set utf8;
改进jdbc client的字符集, 在设置连接字串时设定:如
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8