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
相關文章
- 解決: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
- java.sql.SQLException: No value specified for parameter 1 異常分析JavaSQLException
- java.sql.SQLException: The server time zone value ‘???ú±ê×??±??‘ is unrecognized or represents moreJavaSQLExceptionServerZed
- mysql 報錯:ERROR 1366 (HY000): Incorrect string value: ‘\xD5\xC5\xC8\xFD‘ for column ‘name‘ at row 1MySqlError
- 【MySQL報錯】1366 - Incorrect string value: ‘\xE6\x80\xBB\xE7\xBB\x8F...‘ for column ‘name‘ at row 1MySql
- PHP連線SQL資料庫無法顯示emoji表情的解決辦法(變成問號、1366 Incorrect string value)PHPSQL資料庫
- 解決 Incorrect datetime value: '0000-00-00 00:00:00' 報錯
- java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä‘ is unrecognized...報錯解決JavaSQLExceptionServerZed
- Idea資料庫引入異常:java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä‘ is unrecognizedIdea資料庫JavaSQLExceptionServerZed
- 資料庫報錯java.sql.SQLException: Field ‘id‘ doesn‘t have a default value資料庫JavaSQLException
- StringRedisTemplate操作Redis時拋: Unexpected token (VALUE_STRING)Redis
- Java原始碼閱讀-String中的private final char value[];Java原始碼
- JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from StringJSONErrorJavaLDA
- [Vue warn]: Invalid prop: type check failed for prop "unlinkPanels". Expected Boolean, got String with value "true".VueAIBooleanGo
- SpringBoot升級到3.2.0報錯Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.StringSpring BootBeanObjectJava
- MySQL, Incorrect usage of UNION and ORDER BYMySql
- vue報錯:[Vue warn]: Invalid prop: type check failed for prop "value". Expected Number, got String....VueAIGo
- 查LOB文字內容報錯ORA-06502: numeric or value error: character string buffer..Error
- Java更新資料庫報錯:Data truncation: Cannot create a JSON value from a string with CHARACTER SETJava資料庫JSON
- SQLSTATE [22007]: Invalid datetime format: 1292 IncorrectSQLORM
- CF708D Incorrect Flow 題解
- java.sql.SQLException: 數字溢位JavaSQLException
- java.sql.SQLException: Fail to convert to internal representationJavaSQLExceptionAI
- golang multiple-value xxx in single-value contextGolangContext
- String,String Builder,String Buffer-原始碼UI原始碼
- Failed to execute user defined function(anonfun$concatStr$1: (map<string,string>, string) => string)AIFunction
- [20231103]sqlplus column new_value old_value.txtSQL
- [20230303]sqlplus column new_value old_value.txtSQL
- JavaScript select valueJavaScript
- @ConfigurationProperties和@Value
- Oracle分析函式-first_value()和last_value()Oracle函式AST
- MyIsam 表 “ Incorrect key file for table 't_ '; try to repair it”AI
- String s = “hello“和String s = new String(“hello“)的區別
- 7.94 FIRST_VALUE
- 7.92 FEATURE_VALUE
- 7.12 ANY_VALUE