๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
2026-02-18 17:20:18.300 ERROR 8842 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]] with root cause
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x82' for column 'name' at row 1โก Quick Fix Works 80% of the time
Alter your MySQL database and table to use utf8mb4 character set.
ALTER DATABASE mydb CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;๐ง Why this Happens
Tap to expand the deep technical explanation
The client sent a string containing a 4-byte UTF-8 character (like an emoji: ๐, which is `\xF0\x9F\x98\x82` in bytes). Your MySQL database or table is configured to use `utf8` (which only supports 3-byte characters). MySQL rejects the 4-byte character because it cannot store it.
The HITEC City Parking Spot Analogy:
Imagine a mailbox designed to hold standard letters (utf8). Someone tries to stuff a small parcel (emoji) into it. The mail carrier (MySQL) refuses to accept it because the mailbox physically cannot hold that size of item.
๐ How to Reproduce Confirm this is your error
Create a MySQL table with `CHARACTER SET utf8`. Try to insert a string containing an emoji (e.g., `INSERT INTO users (name) VALUES ('Deva ๐');`). The insert will fail.
๐ ๏ธ Solutions (5 Ways to Fix)
Alter the database and table charset
๐ Use this as the permanent fix for MySQL databases.
Change the character set from `utf8` to `utf8mb4`, which supports full 4-byte UTF-8 characters including emojis.
ALTER DATABASE mydb CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;Specify utf8mb4 in the JDBC connection URL
๐ Use this to ensure the JDBC driver encodes strings correctly.
Append `characterEncoding=utf8mb4` to your `spring.datasource.url` so the driver knows to use 4-byte UTF-8.
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8mb4Use @Column(columnDefinition) in JPA
๐ Use this if you want Hibernate to create the table with the correct charset automatically.
Tell Hibernate to explicitly generate the column with `utf8mb4`.
@Entity
public class User {
@Column(columnDefinition = "VARCHAR(255) CHARACTER SET utf8mb4")
private String name;
}Strip emojis before saving (Fallback)
๐ Use this if you cannot change the database schema.
If you don't control the DB, filter out 4-byte characters in Java before saving.
String cleanName = name.replaceAll("[^\\p{L}\\p{N}\\p{M}\\p{Z}\\p{P}\\p{S}]", "");
user.setName(cleanName);Update MySQL configuration file (my.cnf)
๐ Use this to set utf8mb4 as the default for the entire MySQL server.
Prevent this issue for future tables by setting the server default.
# /etc/mysql/my.cnf
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci๐ Version Notes
Uses MySQL Connector/J 8.0.x.
Uses MySQL Connector/J 8.0.x or 8.1.x.
๐ก๏ธ How to Prevent This Next Time
Always configure new MySQL databases to use `utf8mb4` by default. PostgreSQL supports 4-byte UTF-8 out of the box and does not have this specific issue.