Detailed steps to store emoji expressions in MySQL

Detailed steps to store emoji expressions in MySQL

Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x94\xA5' for column An error occurs when storing Emoji expressions in Mysql.

iOS emoticons (emoji emoticons), although this emoticon is utf8 encoded, one character takes up 4 bytes, while MySQL utf8 encoding can only store 3-byte characters. In MySQL 5.6, you can set the encoding to utf8mb4, which is a superset of utf8.

1. Modify the MYSQL character set

Mysql sets the character encoding set to utf8mb4, and the corresponding database, table, and field are set to utf8mb4. Note that if the utf8 encoding set of an existing table is changed to utf8mb4, then the database, table, and field must be set and checked once. If only the database is changed, the corresponding table and field will not change, and the same is true for changing the table. You may also need to restart the database if you changed the configuration.

character_set_server=utf8mb4

2. Upgrade JDBC

Lower versions of JDBC will display garbled characters, garbled expressions, and even garbled Chinese characters.

The following are some corresponding situations of JDBC versions. The details are as follows: JDBC version supports EMOJI. The picture is found on the Internet. My 5.1.6 test inserts and displays Chinese and Emoji expressions normally.

3. Inherit the server configuration jdbc:mysql://xxx.xxx.xxx.xxx:3306/dbwww58com_chrcsm?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true. The parameters after the connection address are set to UTF-8 or can be automatically recognized. If not, try to remove the bold part to let the connection automatically recognize the server configuration. The specific situation depends on the JDBC version. Inherit the server configuration jdbc:mysql://xxx.xxx.xxx.xxx:3306/dbwww58com_chrcsm?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true. The parameters after the connection address are set to UTF-8 or can be automatically recognized. If not, try to remove the bold part to let the connection automatically recognize the server configuration. The specific situation depends on the JDBC version.

4. Set the init_connect parameter of MYSQL

After setting, you can verify it by showing variables like 'init_connect';, as shown in Figure 2 init_connect parameter query:

5. The encoding set is set in the code. Perhaps it is because of the JDBC version. I use this method, and both insertion and query can proceed normally. connection.prepareStatement("set names utf8mb4").executeQuery();Then insert it through connection.prepareStatement. The encoding set is set in the code perhaps because of the JDBC version. I use this method, and both insertion and query can proceed normally. connection.prepareStatement("set names utf8mb4").executeQuery();Then insert it through connection.prepareStatement.

You may also be interested in:
  • Reasons and solutions for failure to insert emoji expressions in MySQL
  • Analysis of the solution to MySQL's inability to store emoji expressions
  • How to deal with errors in MySQL's emoji table storage [change encoding to utf8mb4]
  • Analysis of solutions to the problem that MySQL cannot store Emoji expressions
  • Solution to the problem of failure to insert emoji expressions into MySQL
  • How to enable Java backend MySQL database to support emoji expressions
  • Tutorial on how to set up MySQL to store emoji characters
  • How to insert Emoji expressions into MySQL

<<:  Linux (CentOS7) installs Tomcat and sets Tomcat as a startup item (taking tomcat8 as an example)

>>:  WeChat Mini Program to Implement Electronic Signature

Recommend

Example code for implementing random roll caller in html

After this roll call device starts calling the ro...

Four solutions for using setTimeout in JS for loop

Table of contents Overview Solution 1: Closures S...

Some notes on mysql self-join deduplication

Let me briefly explain the functional scenario: T...

CSS to achieve the small sharp corner effect of bubbles

Effect picture (the border color is too light, pu...

Implementation of mysql data type conversion

1. Problem There is a table as shown below, we ne...

SQL insert into statement writing method explanation

Method 1: INSERT INTO t1(field1,field2) VALUE(v00...

Example of implementing a virtual list in WeChat Mini Program

Table of contents Preface analyze Initial Renderi...

Detailed explanation of using top command to analyze Linux system performance

Introduction to Linux top command The top command...

Solve the problem of yum installation error Protected multilib versions

Today, when installing nginx on the cloud server,...

How to configure wordpress with nginx

Before, I had built WordPress myself, but at that...

Analysis of problems caused by MySQL case sensitivity

MYSQL is case sensitive Seeing the words is belie...

Some tips on deep optimization to improve website access speed

Some tips for deep optimization to improve websit...