问题:java.sql.sqlException: Incorrect string value: '\xE5\x85\xAC\xE4\xBC\x91' for column ...
使用Intellij IDEA 编写Spring Boot,集成Hibernate代码时,插入中文到数据库中报以上的错,首先检查了URL:
dburl=jdbc:MysqL://localhost:3306/myblog?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8
OK,没问题,其次查看项目编码:
OK,也没问题,都是UTF8,无奈之下百度之:找到了以下的解决办法:
1.MysqL配置文件地址
/etc/MysqL/my.cnf
2.在[MysqLd]在下方添加以下代码
[MysqLd]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
3.重启MysqL服务
sudo service MysqL restart
4.检测字符集是否更新成utf8.
进入MysqL,MysqL -u root -p,输入show variables like '%character%' 查看字符集
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/MysqL/charsets/ |
+--------------------------+----------------------------+
小插曲:我的文件不是/etc/MysqL/my.cnf,而是: /etc/MysqL/MysqL.conf.d/MysqLld.cnf
Ubuntu 版本:16.04.2,MysqL版本:Server version: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)