[转帖]java.sql.BatchUpdateException: Incorrect string value: '\xD6\xD0\xB9\xFA\xB9\xA_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1624 | 回复: 0   主题: [转帖]java.sql.BatchUpdateException: Incorrect string value: '\xD6\xD0\xB9\xFA\xB9\xA        下一篇 
曹先丰
注册用户
等级:中士
经验:208
发帖:72
精华:0
注册:2012-1-9
状态:离线
发送短消息息给曹先丰 加好友    发送短消息息给曹先丰 发消息
发表于: IP:您无权察看 2014-12-18 15:57:46 | [全部帖] [楼主帖] 楼主

今天收到运营的BUG,出现了类似回档的问题

查看了服务器日志,发现持续一段时间数据库抛出异常

[java] view plaincopy
<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>
[java] view plaincopy
<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>


但是数据库的字符集是UTF-8的,查询了抛错的字段,也是UTF-8
百度无结果,只能查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字符的方法,大概意思为:

[java] view plaincopy
for i=1->n
int c=str.codePointAt(i);
if (c<0x0000||c>0xffff) {
      return false;
}
[java] view plaincopy
for i=1->n
int c=str.codePointAt(i);
if (c<0x0000||c>0xffff) {
      return false;
}


调用后解决此问题。

附上两个讨论这个问题的地址:

1. 解释来源地址

http://stackoverflow.com/questions/10957238/incorrect-string-value-when-trying-to-insert-utf-8-into-mysql-via-jdbc


2. 4字节Unicode字符的讨论帖

http://topic.csdn.net/u/20091107/17/c0eb2463-b4bb-4197-bd67-0459db8aa137.html
id int primary key,
name varchar(20),
title varchar(10) character set utf8
);


在测试:

Hibernate: insert into Teacher (name, title, id) values (?, ?, ?)


呵呵,我的问题终于解决了!继续奋斗!!!

不过更有效的方法是把mysql的编码改为gb2312.在重启mysql,但是在改之前创建的数据库的表是没法改变的,就得自己重新创建数据库啦!!!

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论