JDBC插入中文出现乱码问题

使用命令行插入中文的时候出现:

ERROR 1366: 1366: Incorrect string value: 'xE5xA5xB3' for column

这是因为创建表的时候未指定编码集,需使用utf-8

例如:

create table student(id int(8) not null auto_increment, name varchar(32) default null, grade_id int(8) not null, picture varchar(128) default null, sex char(4) default null, primary key (id))character set=utf8;

使用JDBC插入中文数据的时候显示未“???”或乱码,是因为JDBC的打开连接没有指定字符集。例:

String url = "jdbc:mysql://localhost:3306/lab_system?useSSL=true&characterEncoding=utf-8";

测试结果:

+----+-----------+----------+---------+------+
| id | name      | grade_id | picture | sex  |
+----+-----------+----------+---------+------+
|  1 | ???       |       10 |         | ?    |
|  2 | ???       |       10 |         | ?    |
|  3 | 胀死啊    |       10 |         | 男   |
+----+-----------+----------+---------+------+
原文地址:https://www.cnblogs.com/meilj/p/9235420.html