java.sql.SQLException: Incorrect string value
轉自:http://comeonbabye.iteye.com/blog/1467272
//注: 裡面的*不是導致問題的特殊字元,因為特殊字元無法顯示,總之是某個轉成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
相關文章
- 【MySQL】Incorrect string value 問題一則MySql
- 解決:java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x92\x94‘ for columnJavaSQLException
- 【BUG記錄】MySQL插入Emoji表情報錯"Incorrect string value"MySql
- MySQL: Incorrect string value: '\xF0\xA4\xBD\x82'分析MySql
- MySQL ERROR 1366(HY000):Incorrect string value:''for column''at row 1解決方案MySqlError
- MySQL 亂碼實戰解決ERROR 1366 (HY000): Incorrect string value: 'MySqlError
- Mybatis Data truncation: Truncated incorrect DOUBLE value: '*'MyBatis
- kettle建立資源庫Incorrect integer value
- java.sql.SQLException: The server time zone value ‘???ú±ê×??±??‘ is unrecognized or represents moreJavaSQLExceptionServerZed
- java.sql.SQLException: No value specified for parameter 1 異常分析JavaSQLException
- ORACLE匯出文字到MYSQL 報錯 Incorrect integer value: ''OracleMySql
- mysql 報錯:ERROR 1366 (HY000): Incorrect string value: ‘\xD5\xC5\xC8\xFD‘ for column ‘name‘ at row 1MySqlError
- java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä‘ is unrecognized...報錯解決JavaSQLExceptionServerZed
- PHP連線SQL資料庫無法顯示emoji表情的解決辦法(變成問號、1366 Incorrect string value)PHPSQL資料庫
- 【MySQL報錯】1366 - Incorrect string value: ‘\xE6\x80\xBB\xE7\xBB\x8F...‘ for column ‘name‘ at row 1MySql
- 解決 Incorrect datetime value: '0000-00-00 00:00:00' 報錯
- 資料庫報錯java.sql.SQLException: Field ‘id‘ doesn‘t have a default value資料庫JavaSQLException
- Idea資料庫引入異常:java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä‘ is unrecognizedIdea資料庫JavaSQLExceptionServerZed
- android 獲取string.xml中的valueAndroidXML
- StringRedisTemplate操作Redis時拋: Unexpected token (VALUE_STRING)Redis
- MySQL-修改表的編碼和列的編碼-Incorrect string value: '\xE6\xB1\x9F\xE5\xAE\x81...' for column 'unitname' at rowMySql
- ERROR getting 'android:label' attribute: attribute is not a string valueErrorAndroid
- ORA-06502 PL/SQL: numeric or value error stringSQLError
- Java原始碼閱讀-String中的private final char value[];Java原始碼
- MYSQL寫入資料時報錯ERROR 1366 (HY000): Incorrect string value: '\xE8\x8B\xB1\xE5\xAF\xB8...' for c 插入中文不能插...MySqlError
- JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from StringJSONErrorJavaLDA
- Could not resolve placeholder 'redis.pool.maxTotal' in string value "${redis.pool.maxTotal}"Redis
- MySQL, Incorrect usage of UNION and ORDER BYMySql
- ERROR 1292 (22007): Truncated incorrect DOUBLE value 和ORA-01722: invalid numberError
- java.sql.SQLException: Scope not recognizedJavaSQLExceptionZed
- Error: Attribute application@label value=(@string/appname) from AndroidManifestErrorAPPAndroid
- [Vue warn]: Invalid prop: type check failed for prop "unlinkPanels". Expected Boolean, got String with value "true".VueAIBooleanGo
- vue報錯:[Vue warn]: Invalid prop: type check failed for prop "value". Expected Number, got String....VueAIGo
- cornerstone the server may be unreachable or the url may be incorrectServer
- java.sql.SQLException: 數字溢位JavaSQLException
- SpringBoot升級到3.2.0報錯Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.StringSpring BootBeanObjectJava
- java.sql.SQLException: Fail to convert to internal representationJavaSQLExceptionAI
- LOW_VALUE、HIGH_VALUE、ENDPOINT_VALUE轉換--UTL_RAW、DBMS_STATS.CONVERT_RAW_VALUE