Let MySQL support emoji expression store

Source: Internet
Author: User

Java background error, as follows.

Aused by:java.sql.SQLException:Incorrect String value: ' \xf0\x9f\x98\x84 ' for column ' review ' at row 1at Com.mysql.jdbc. Sqlerror.createsqlexception (sqlerror.java:1074) at Com.mysql.jdbc.MysqlIO.checkErrorPacket (mysqlio.java:4096) at Com.mysql.jdbc.MysqlIO.checkErrorPacket (mysqlio.java:4028) at Com.mysql.jdbc.MysqlIO.sendCommand (Mysqlio.java : 2490) at Com.mysql.jdbc.MysqlIO.sqlQueryDirect (mysqlio.java:2651) at Com.mysql.jdbc.ConnectionImpl.execSQL ( connectionimpl.java:2734) at Com.mysql.jdbc.PreparedStatement.executeInternal (preparedstatement.java:2155) at Com.mysql.jdbc.PreparedStatement.executeUpdate (preparedstatement.java:2458)

Check Reason:

Tracking found that the original was using the phone entered a smiley face (emoji expression). The UTF-8 encoding can be two, three, and four bytes. The emoji expression is 4 bytes, while MySQL UTF8 encodes up to 3 bytes, so data cannot be plugged in.

Solution:

  Converts the MySQL encoding from UTF8 to UTF8MB4.

1. Modify My.ini [mysqld]

init-connect= ' SET NAMES utf8mb4 ' character-set-server=utf8mb4

2. In the background configuration MySQL connection parameter, do not add characterencoding parameter. When this parameter is not added, the default value is AutoDetect.

3. Convert the already built tables to UTF8MB4

ALTER TABLE ' Daily_nursing_record ' CONVERT to CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; (Replace table_name with your table name)

4. Set the type of field you want to use emoji as: (this is not necessarily required)

ALTER TABLE ' table_name ' MODIFY COLUMN ' column_name ' text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

5. Note Show CREATE table XXX; Ensure that each field does not have a separate character set specified

Let MySQL support emoji expression store

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.