๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOG2026-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)

Solution 1โœ“ Most common cause

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;
Solution 2

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=utf8mb4
Solution 3

Use @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; }
Solution 4

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);
Solution 5

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

Spring Boot 2.x

Uses MySQL Connector/J 8.0.x.

Spring Boot 3.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.

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase