java.sql.BatchUpdateException: Incorrect string value: '\xD6\xD0\xB9\xFA\xB9\xA4...' for column 'con
今天收到运营的BUG,出现了类似回档的问题
查看了服务器日志,发现持续一段时间数据库抛出异常
- <SPAN style="FONT-SIZE: 14px">Caused by: java.sql.BatchUpdateException: Incorrect string value: '\xF0\x9F\x92\x97\xE2\x80...' for column 'innerNotice' at row 1
- at com.mysql.jdbc.ServerPreparedStatement.executeBatchSerially(ServerPreparedStatement.java:814)
- at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1452)
- at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
- at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
- at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
- ... 16 more</SPAN>
Caused by: java.sql.BatchUpdateException: Incorrect string value: '\xF0\x9F\x92\x97\xE2\x80...' for column 'innerNotice' at row 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatchSerially(ServerPreparedStatement.java:814)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1452)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 16 more
百度无结果,只能查google
得到的解释如下:
MySQL's utf8
permits only the unicode characters that can be represented with 3 bytes in UTF-8. Here you have a string with a character that needs 4 bytes: \xF0\x90\x8D\x83 (U+10343 GOTHIC LETTER SAUIL).
意思大概是,MySQL的UTF-8只支持3个字节的unicode字符,无法支持四个字节的Unicode字符。
查询了服务器类似的代码,发现了判断MySql支持Unicode字符的方法,大概意思为:
- for i=1->n
- int c=str.codePointAt(i);
- if (c<0x0000||c>0xffff) {
- return false;
- }
for i=1->n
int c=str.codePointAt(i);
if (c<0x0000||c>0xffff) {
return false;
}
调用后解决此问题。
附上两个讨论这个问题的地址:
1. 解释来源地址
2. 4字节Unicode字符的讨论帖
http://topic.youkuaiyun.com/u/20091107/17/c0eb2463-b4bb-4197-bd67-0459db8aa137.html
name varchar(20),
title varchar(10) character set utf8
);
在测试:
Hibernate: insert into Teacher (name, title, id) values (?, ?, ?)
呵呵,我的问题终于解决了!继续奋斗!!!
不过更有效的方法是把mysql的编码改为gb2312.在重启mysql,但是在改之前创建的数据库的表是没法改变的,就得自己重新创建数据库啦!!!