Incorrect string value when trying to insert UTF-8 into MySQL via JDBC?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with Java and MySQL, one common problem developers encounter is the "Incorrect string value" error when attempting to insert UTF-8 encoded data via JDBC (Java Database Connectivity). This error can be confusing, particularly when dealing with multilingual applications. Let's dive into this issue, explore why it occurs, and discuss how to fix it.
Understanding the "Incorrect string value" Error
The "Incorrect string value" error typically emerges when MySQL attempts to store a string containing characters that aren't compatible with the character set defined for that table or column. If the data includes special Unicode characters (such as emojis, accented letters, or non-Latin scripts) and MySQL is not configured correctly, this error will appear.
Technical Explanation
By default, MySQL tables might not use a character set that supports all possible Unicode characters. The standard UTF-8 encoding (utf8) in MySQL only supports the Basic Multilingual Plane (BMP), which uses three bytes per character. Characters beyond this set, such as emojis or characters from certain Asian scripts, require four bytes.
The utf8mb4 character set, on the other hand, supports all Unicode characters, as it can use four bytes per character. If you haven't configured your database to support utf8mb4, you'll receive "Incorrect string value" errors when trying to insert characters beyond the BMP.
JDBC and Character Encoding
When using JDBC, the connection's character encoding plays a crucial role. JDBC by default uses the platform's default encoding, which may not be UTF-8. Therefore, you need to specify characterEncoding=UTF-8 and useUnicode=true in the connection string to ensure that Java objects are correctly converted to their UTF-8 representation.
Example Scenario
Let's say you have a MySQL table defined as follows:
Now, if you try to insert a string containing the emoji "😊" via JDBC, you might execute the following code:
This code will raise an exception like:
Solving the Problem
To resolve this issue, both your database and JDBC configuration need to support utf8mb4. Here are the steps:
- Update the Database:Change the character set and collation of the database, tables, and columns:
- Modify the JDBC URL:Update the connection string to specify
utf8mb4:
Key Points Summary
| Topic | Key Point |
| Error Message | "Incorrect string value" indicates a character set incompatibility. |
| Character Sets | utf8 supports only BMP (3 bytes); utf8mb4 supports full Unicode (4 bytes). |
| JDBC Configuration | Ensure characterEncoding=UTF-8 and useUnicode=true are in the connection URL. |
| Database Setup | Use ALTER statements to convert existing structures to utf8mb4. |
| Common Causes | Attempting to store 4-byte characters with utf8 column/table encoding. |
| Fix | Use utf8mb4 for the database, and adjust JDBC connection settings for accurate handling. |
Additional Considerations
- MySQL Server Variables: Ensure server variables such as
character_set_server,character_set_database, andcollation_serverare set toutf8mb4. - Driver Version: Use a recent version of the MySQL Connector/J, as older versions might have discrepancies in handling character sets.
- Environment Differences: Be aware of potential differences in character handling across development, staging, and production environments to ensure consistency.
By understanding these components and ensuring both the server and client configurations are correctly set, you can effectively manage string encoding to handle internationalization and special characters seamlessly in your applications.

