//注: 里面的*不是导致问题的特殊字符,因为特殊字符无法显示,总之是某个转成utf8后有4bytes的字符
背景:
数据库编码,建表编码,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
--------------
现象:
插入的数据中如果含有某些特殊字符,会导致插入数据失败,例如字符串”测试*插入数据”,在console中insert是正常的,但是使用java代码insert的时候报错:
// 输出很长,重点就一行
2012-02-06 14:44:43,741 ERROR BlaBlaServiceImpl:110 - insertOrUpdateBlaBla failed!
--- Cause: java.sql.SQLException: Incorrect string value: '
\xF0\x9F\x
92\x90</...' for column ……
问题的可能原因:(未证实)
mysql中规定utf8字符MaxLen=3,但是某些unicode字符转成utf8编码之后有4个字节,于是就杯具了
String c = "*" ; // *代表某个utf8编码后有4个byte的字符
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://bit.ly/Ao3iJO
问题可能是因为某些unicode字符转成utf8之后变成了4个字节
分享到:
相关推荐
本文主要介绍了关于MySQL存储表情报错:java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x92\xA9\x0D\x0A…’的相关解决方法,分享出供大家参考学习,下面话不多说了,来一起看看详细的介绍: ...
java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK ……
主要给大家介绍了关于MySQL存储表情时报错:java.sql.SQLException: Incorrect string value: 'xF0x9Fx92xA9x0Dx0A...'的解决方法,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
NULL 博文链接:https://listen-raining.iteye.com/blog/1962065
在oracle里面运行一下,解决Exception java.sql.SQLException ORA-00600 内部错误代码
NULL 博文链接:https://zhang-zling.iteye.com/blog/980488
服务器出现java.sql.SQLException No suitable driver found for 的
java.sql.SQLException: null, message from server: “Host ‘223.72.41.7’ is not allowed to connect to this MySQL server” 客户端访问时报错: 解决方法: 1,登陆服务器 mysql> use mysql; //用mysql ...
oracle ora-各种常见java.sql.SQLException归纳
Q: I am working with ... I am using updateBinaryStream method of resultset to update the BLOB field but it is failing after giving following exception java.sql.SQLException: Internal Error: Unable to
mysql5.6连接驱动jaf,可处理报错java.sql.SQLException: No suitable driver
oracle 10g 数据库驱动下载:oracle jdbc dirver
异常错误:java.sql.SQLException: The server time zone value ‘?й???’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTime...
java.sql.SQLException: Operation not allowed after ResultSet closed java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way, or a Connection should be passed in
用户昵称中存在emoji表情,调用jdbc往mysql数据库插入的时候抛出异常 java.sql.SQLException: Incorrect string value: '\xF0\x9F\x90\x9B' 失败原因 mysql的utf8编码的一个字符最多3个字节,但是一个emoji表情为4...
问题是这样的…我在VS2008直接运行打开网页,可以连接... 异常详细信息: System.Data.SqlClient.SqlException: 无法打开登录所请求的数据库 “TakeClass”。登录失败。 用户 ‘2D2727E2578F446\ASPNET’ 登录失败。
该用户与可信 SQL Server 连接无关联。 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient....
Cause: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x8C\xB7' for column 'nickname' at row 1 解决方案 修改nickname的编码格式,没必要修改整个表。这种方式也不需要重启数据库,修改完即生效 ...
主要介绍了解决IDEA错误 Cause: java.sql.SQLException: The server time zone value的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; /** * @author Administrator * */ public class sqlServer { String DBDriver=...