报错信息如下:
java.sql.SQLException: Incorrect string value: '\xE5\x88\x98\xE6\x97\xAD...' for column 'Name' at row 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
出现这个错误原因:
数据库的编码格式为latin1,而需要插入到数据库中的中文却是utf8。
解决办法:
1查看数据库编码格式:
show variables like 'character%';
2查看数据库表的编码:
show create table <表名>;
3 修改数据库的编码:
alter database <数据库名> character set utf8;
4 修改数据库某表的编码:
alter table <表名> character set utf8;
5 修改数据库某表的某一字段的编码:
alter table <表名> change <字段名> <字段名> <类型> character set utf8;
举例:
mysql> show databases; mysql> use testdb; mysql> show tables; mysql> show create table factory_list; mysql> alter table factory_list character set utf8; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table factory_list change Name Name varchar(256) character set utf8; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0