This is how my connection is set:
Connection conn = DriverManager.getConnection(url + dbName + "?useUnicode=true&characterEncoding=utf-8", userName, password);
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
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).
The column's collation is utf8_general_ci.
What could be the problem?
Best Answer
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).If you have MySQL 5.5 or later you can change the column encoding from
utf8
toutf8mb4
. This encoding allows storage of characters that occupy 4 bytes in UTF-8.You may also have to set the server property
character_set_server
toutf8mb4
in the MySQL configuration file. It seems that Connector/J defaults to 3-byte Unicode otherwise: