[Archiving articles]

The problem

An exception occurred when inserting data into the Mysql database. Procedure

SQL state [HY000]; error code [1366]; Incorrect string value: ‘\xF0\x9F\x90\x94\xF0\x9F… ‘ for column ‘COLUMN_NAME’ at row 1; nested exception is java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x90\x94\xF0\x9F… ‘ for column ‘COLUMN_NAME’ at row 1

why

Check the data saved to Column, and find that saved data [including Emoji];

Emoji is [4 bytes], the MYSQL version used at that time is [5.5.18], the database code is UTF8, and UTF8 can only store [3 bytes] of data;

The solution

Mysql > alter database [character encoding] from [utf8] to [utf8MB4];

Specific operation

1. Modify the MySQL character encoding

Modify my CNF

 [mysqld] character-set-server=utf8mb4

 [mysql] default-character-set=utf8mb4 

** You must restart Mysql service ** after modification

2. Modify the character encoding of the database table

The command

alter table TABLE_NAME convert to character set utf8mb4 collate utf8mb4_bin;
Copy the code