Handling Incorrect String Values When Inserting UTF-8 into MySQL via JDBC
When attempting to insert UTF-8 data into a MySQL database using JDBC, you may encounter the error message, "Incorrect string value: 'xF0x90x8Dx83xF0x90...'" This error often occurs when the inserted text contains characters that cannot be represented in MySQL's utf8 character set.
While your connection settings include the use of Unicode ("useUnicode=true") and UTF-8 encoding ("characterEncoding=utf-8"), MySQL's utf8 encoding only supports Unicode characters that can be represented with 3 bytes in UTF-8. The character causing the error, however, requires 4 bytes.
Solution:
To resolve this issue, you have two options:
Note that Connector/J, commonly used for JDBC connections with MySQL, defaults to 3-byte Unicode. Therefore, it's recommended to leave characterEncoding out of your connection string and configure the MySQL server with character_set_server=utf8mb4 to allow Connector/J to autodetect the UTF-8 setting.
The above is the detailed content of How to Fix 'Incorrect string value' Errors When Inserting UTF-8 Data into MySQL via JDBC?. For more information, please follow other related articles on the PHP Chinese website!