問題描述
這是我的連接設置方式:Connection conn = DriverManager.getConnection(url + dbName + "?useUnicode=true&characterEncoding=utf-8", userName, password);
This is how my connection is set:
Connection conn = DriverManager.getConnection(url + dbName + "?useUnicode=true&characterEncoding=utf-8", userName, password);
我在向表中添加行時遇到以下錯誤:不正確的字符串值:'\xF0\x90\x8D\x83\xF0\x90...' 對于第 1 行的 'content' 列
And I'm getting the following error when tyring to add a row to a table:
Incorrect string value: '\xF0\x90\x8D\x83\xF0\x90...' for column 'content' at row 1
我正在插入數千條記錄,當文本包含 \xF0 時我總是收到此錯誤(即錯誤的字符串值總是以 \xF0 開頭).
I'm inserting thousands of records, and I always get this error when the text contains \xF0 (i.e. the the incorrect string value always starts with \xF0).
該列的排序規則是 utf8_general_ci.
The column's collation is utf8_general_ci.
可能是什么問題?
推薦答案
MySQL 的 utf8
只允許 UTF-8 中可以用 3 個字節表示的 Unicode 字符.這里有一個需要 4 個字節的字符:\xF0\x90\x8D\x83 (U+10343 哥特式字母 SAUIL).
MySQL's utf8
permits only the Unicode characters that can be represented with 3 bytes in UTF-8. Here you have a character that needs 4 bytes: \xF0\x90\x8D\x83 (U+10343 GOTHIC LETTER SAUIL).
如果您有 MySQL 5.5 或更高版本,您可以將列編碼從 utf8
更改為 utf8mb4
.這種編碼允許在 UTF-8 中存儲占用 4 個字節的字符.
If you have MySQL 5.5 or later you can change the column encoding from utf8
to utf8mb4
. This encoding allows storage of characters that occupy 4 bytes in UTF-8.
您可能還需要在 MySQL 配置文件中將服務器屬性 character_set_server
設置為 utf8mb4
.似乎 Connector/J 默認為 3 字節Unicode 否則:
You may also have to set the server property character_set_server
to utf8mb4
in the MySQL configuration file. It seems that Connector/J defaults to 3-byte Unicode otherwise:
例如,要在 Connector/J 中使用 4 字節 UTF-8 字符集,請使用 character_set_server=utf8mb4
配置 MySQL 服務器,并將 characterEncoding
排除在連接器/J 連接字符串.然后連接器/J 將自動檢測 UTF-8 設置.
For example, to use 4-byte UTF-8 character sets with Connector/J, configure the MySQL server with
character_set_server=utf8mb4
, and leavecharacterEncoding
out of the Connector/J connection string. Connector/J will then autodetect the UTF-8 setting.
這篇關于“不正確的字符串值"嘗試通過 JDBC 將 UTF-8 插入 MySQL 時?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!