设为首页 收藏本站
查看: 1710|回复: 0

[经验分享] MySQL java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x90

[复制链接]

尚未签到

发表于 2016-10-19 10:08:06 | 显示全部楼层 |阅读模式
//注: 里面的**不是导致问题的特殊字符,因为那个特殊字符je也显示不出来,而且会截断内容,所以只好这么写了
背景:
数据库编码,建表编码,Content字段编码都设置为utf8,collation是默认的utf8_default(也尝试过修改为其他的,未果,似乎不是collation的问题)

mysql> status;
--------------
mysql  Ver 14.14 Distrib 5.1.49, for debian-linux-gnu (i686) using readline 6.1
Connection id:1402357
Current database:**
Current user:**
SSL:Not in use
Current pager:stdout
Using outfile:''
Using delimiter:;
Server version:5.0.38 Debian etch distribution
Protocol version:10
Connection:** via TCP/IP
Server characterset:gbk
Db     characterset:utf8
Client characterset:utf8
Conn.  characterset:utf8
TCP port:4307
Uptime:187 days 22 hours 51 min 18 sec
Threads: 16  Questions: 409901760  Slow queries: 12290  Opens: 899  Flush tables: 1  Open tables: 246  Queries per second avg: 25.242
--------------


现象:
插入的数据中如果含有某些特殊字符,会导致插入数据失败,例如字符串”测试**插入数据。。。“,在console中insert是正常的,但是使用java代码insert的时候报错:

2012-02-06 14:44:43,741 ERROR BlaBlaServiceImpl:110 - insertOrUpdateBlaBla failed!
org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [HY000]; error code [1366];
--- The error occurred in com/company/project/base/BlaBla/BlaBla.xml.
--- The error occurred while applying a parameter map.  
--- Check the BlaBla.insertBlaBla-InlineParameterMap.
--- Check the statement (update failed).
--- Cause: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x90</...' for column 'Content' at row 1; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/company/project/base/BlaBla/BlaBla.xml.   
--- The error occurred while applying a parameter map.  
--- Check the BlaBla.insertBlaBla-InlineParameterMap.  
--- Check the statement (update failed).  
--- Cause: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x90</...' for column 'Content' at row 1
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:203)
at org.springframework.orm.ibatis.SqlMapClientTemplate.insert(SqlMapClientTemplate.java:364)
at com.company.project.base.BlaBla.BlaBlaDaoImpl.insertBlaBla(BlaBlaDaoImpl.java:81)
at com.company.project.base.BlaBla.BlaBlaServiceImpl.insertBlaBla(BlaBlaServiceImpl.java:108)
at com.company.project.check.BlaBla.ReplyRecorder.record(ReplyRecorder.java:66)
at com.company.project.check.CheckingChain.run(CheckingChain.java:67)
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/company/project/base/BlaBla/BlaBla.xml.   
--- The error occurred while applying a parameter map.  
--- Check the BlaBla.insertBlaBla-InlineParameterMap.  
--- Check the statement (update failed).  
--- Cause: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x90</...' for column 'Content' at row 1
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:107)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:393)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82)
at org.springframework.orm.ibatis.SqlMapClientTemplate$8.doInSqlMapClient(SqlMapClientTemplate.java:366)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:200)
... 5 more
Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x90</...' for column 'Content' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1989)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1362)
at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:80)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteUpdate(MappedStatement.java:216)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:94)
... 9 more


问题的可能原因:(未证实)
mysql中规定utf8字符MaxLen=3,但是某些unicode字符转成utf8编码之后有4个字节,于是就杯具了

String c = "*"  ;
byte[] bytes = c.getBytes("utf8");
for(byte b : bytes){
System.out.print(Integer.toHexString(0x00FF & b)+" ");
}
// 输出 f0 9f 8d 8e


mysql> show character set;
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |
+----------+-----------------------------+---------------------+--------+


解决方案:
修改Content字段为MEDIUMBLOB(原来是MEDIUMTEXT),并且把SELECT语句修改成
SELECT CAST(Content AS CHAR CHARACTER SET utf8) AS Content ....
INSERT语句不需要修改,测试ok
参考资料

  • http://dev.mysql.com/doc/refman/5.0/en/charset-convert.html CAST函数用法
  • http://bugs.mysql.com/bug.php?id=30131 07年讨论到10年,结论是: Not a bug
  • http://stackoverflow.com/questions/2108824/mysql-incorrect-string-value-error-when-save-unicode-string-in-django 问题可能是因为某些unicode字符转成utf8之后变成了4个字节,utf8 max-length=3

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-288329-1-1.html 上篇帖子: MySQL外键 Cannot add or update a child row错误的实例解释 下篇帖子: 备忘:ubuntu12.04 安装部署环境,nginx,tomcat,mysql。以及开机启动
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表