Encoding Exception: "Incorrect String Value" with Emojis in Java-MySQL
In a Java application using MySQL, an exception "java.sql.SQLException: Incorrect string value: 'xF0x9Fx91xBDxF0x9F...'" may be encountered when inserting a string containing emoji characters. This error occurs due to differences in character encoding.
The string value in question contains "walmart obama ??", where "???" represents emojis. In the database, the variable where the string is being inserted is defined as varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL. However, the MySQL version used by the application only supports basic multilingual plane (BMP) characters in UTF-8 encoding. Emoji characters, which fall outside of the BMP, cannot be represented properly in this encoding.
To resolve the issue, it is necessary to switch to UTF-8mb4 encoding, which supports supplementary characters. This involves making the following adjustments:
By making these changes, your application will be able to properly handle emoji characters using UTF-8mb4 encoding, resolving the "Incorrect string value" exception.
The above is the detailed content of How to Resolve \'Incorrect String Value\' Errors When Inserting Emojis into a MySQL Database in Java?. For more information, please follow other related articles on the PHP Chinese website!