Solve the error when inserting MySQL: Incorrect string value:'\xF0\x9F\x98\xAD“,...' for column'commentContent' at row 1

1. Problem description

When inserting data into mysql database, Incorrect string value:'\xF0\x9F\x98\xAD",...' for column'commentContent' at row 1 This error appeared. Google found that it was caused by database encoding problems. Yes, the reason is that there are emoj expressions in our comment data, and these expressions are encoded in a unit of four bytes, and the utf-8 encoding that we usually use is a unit of 3 bytes by default in the mysql database For encoding, it is for this reason that an error occurs when storing data in the mysql database

2. Solution

    (1):修改mysql数据库的编码为uft8mb4  
    (2):修改数据表的编码为utf8mb4
ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4;

(3): Modify the connection code to connect to the database

    public static final String URL = "jdbc:mysql://localhost:3306/"+DATABASENAME+"?useunicode=true&characterEncoding=utf8";
    将其修改成:public static final String URL = "jdbc:mysql://localhost:3306/"+DATABASENAME;

Guess you like

Origin blog.csdn.net/CharlesYooSky/article/details/112213389