java.sql.BatchUpdateException: Incorrect string value: '\xD6\xD0\xB9\xFA\xB9\xA4...' for column 'con

java.sql.BatchUpdateException: Incorrect string value: '\xD6\xD0\xB9\xFA\xB9\xA4...' for column 'con

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

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

  1. <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  
  2.     at com.mysql.jdbc.ServerPreparedStatement.executeBatchSerially(ServerPreparedStatement.java:814)  
  3.     at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1452)  
  4.     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)  
  5.     at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)  
  6.     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)  
  7.     ... 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


但是数据库的字符集是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字符的方法,大概意思为:

  1. for i=1->n  
  2. int c=str.codePointAt(i);  
  3. if (c<0x0000||c>0xffff) {  
  4.     return false;  
  5. }  
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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值