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

Grid systems in web design

Formation of the grid system In 1692, the newly c...

Analysis of different MySQL table sorting rules error

The following error is reported when MySQL joins ...

JavaScript to achieve lottery effect

This article shares the specific code of JavaScri...

Example of setting up a whitelist in Nginx using the geo module

Original configuration: http { ...... limit_conn_...

How to install JDK and Mysql on Ubuntu 18.04 Linux system

Platform deployment 1. Install JDK step1. Downloa...

PHP scheduled backup MySQL and mysqldump syntax parameters detailed

First, let's introduce several common operati...

Write a mysql data backup script using shell

Ideas It's actually very simple Write a shell...

Docker cleaning killer/Docker overlay file takes up too much disk space

[Looking at all the migration files on the Intern...

About if contains comma expression in JavaScript

Sometimes you will see English commas ",&quo...

Mysql table creation foreign key error solution

Database Table A: CREATE TABLE task_desc_tab ( id...

Analysis of MySQL query sorting and query aggregation function usage

This article uses examples to illustrate the use ...

How to load third-party component libraries on demand in Vue3

Preface Take Element Plus as an example to config...

Summary of several principles that should be followed in HTML page output

1. DOCTYPE is indispensable. The browser determin...