Today, while climbing the NetEase Cloud music song commentary, when inserting data into the MySQL database, incorrect string value: ' \xf0\x9f\x98\xad ',... ' for column ' commentcontent ' Row 1 This error, Google found that the original is due to the database coding problem, because we have a emoj expression in the comment data, and these expressions are encoded in a unit of four bytes, And we usually use the Utf-8 encoding in the MySQL database by default is based on 3 bytes per unit encoding, this is the reason for the data stored in the MySQL database when there is an error, then this question how we should solve it.
I am divided into the following three steps to solve:
(1): Modify the MySQL database code for UFT8MB4
(2): Modify the data table encoding for UTF8MB4
(3): Modify the connection code of the connection database
(1): Modify the MySQL database code for UFT8MB4
To modify the method:
Refer to: http://blog.csdn.net/poice00/article/details/52129351
(2): Modify the data table encoding for UTF8MB4
Execute command: ALTER TABLE table_name CONVERT to CHARACTER SET utf8mb4;
(3): Modify the connection code of the connection database
My original database connection code is:
public static final String URL = "jdbc:mysql://localhost:3306/" +databasename+? useunicode=true&characterencoding =utf8 ";
Modify it to: public static final String URL = "jdbc:mysql://localhost:3306/" +databasename;