ERROR 1366 (HY000): Incorrect string value:
此种乱码解决方案如下:
首先,设置自己数据库的编码统一:
Connection id: 81
Current database:
Current user: root@localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.0.67-community-nt MySQL Community Edition (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: gbk
Db characterset: gbk
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
随后到自己的表里面:
如我的这个表:content 表
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| ad | smallint(6) | NO | | 0 | |
| title | char(30) | YES | | NULL | |
| author | varchar(30) | YES | | NULL | |
| content | text | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
mysql> insert into content(title,author,content) values('插入成','修改于','文字符');
立刻报错:
ERROR 1366 (HY000): Incorrect string value: '\xD0\xDE\xB8\xC4\xD3\xDA' for colum
n 'author' at row 1
mysql> insert into content(title,author,content) values('插入成','修改于','文字
符');
ERROR 1366 (HY000): Incorrect string value: '\xD0\xDE\xB8\xC4\xD3\xDA' for colum
n 'author' at row 1
想招吧,哎:以下是解决方案:
mysql> alter table content modify author varchar(30) character set gbk;
对,就是这一句代码。很实用的,将表里面的所有列都进行一次这样的“洗礼”,大功告成!
----
不,还没完,页面需要声明用GBK或者GB2312编码,而且在页面需要进行转码:
String content1 = new String(request.getParameter("content1").getBytes("ISO-8859-1"));
页面读取不需要转码,写入需要转码
没有所谓的命运,只有不同的选择!