Solution to the error message "java.sql.SQLException: Incorrect string value:'\xF0\x9F\x92\xA9\x0D\x0A...'" when storing emoticons in MySQL

Solution to the error message "java.sql.SQLException: Incorrect string value:'\xF0\x9F\x92\xA9\x0D\x0A...'" when storing emoticons in MySQL

Preface

This article mainly introduces the solution to the MySQL storage table error: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\xA9\x0D\x0A...', and shares it for your reference. Let's take a look at the detailed introduction:

This project is based on Spring MVC + MySQL + Druid DataSource. An error message appears when inserting Emoji characters into the database:

java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\xA9\x0D\x0A...'

Ordinary characters take up to 3 bytes, which is enough to use utf8. Emoji characters take up 4 bytes and need to be stored using the utf8mb4 character set. The solution requires changes in two places: the MySQL server and the connecting client.

MySQL Server

I have previously introduced a tutorial on how to set up MySQL to store emoji characters. If you want to learn more, you can take a look at it first.

On the server side, change the fields that need to store emoji characters to use the utf8mb4 character set:

ALTER TABLE mytable MODIFY COLUMN myfiled varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Client

The client needs to set the character set of the connection to utf8mb4.

The setup is as follows:

set names utf8mb4;

The open source druid datasource from Alibaba is used. It has a property called connectionInitSqls, which is used to set the SQL executed when the physical connection is initialized. So we can use connectionInitSqls to initialize the connection.

Spring is configured as follows:

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
 <!-- Basic attributes url, user, password -->
 <property name="url" value="jdbc:mysql://localhost:3888/majing?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" />
 <property name="username" value="dbusername"/>
 <property name="password" value="dbpasswod"/>
 <property name="connectionInitSqls" value="set names utf8mb4;" />
 <!-- Other configurations -->
</bean>

Other database connection pools such as dbcp2 and HikariCP have the connectionInitSqls attribute.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • How to handle the failure of inserting rare characters in MySQL (Incorrect string value)
  • MySQL throws Incorrect string value exception analysis
  • MySql error Incorrect string value for column
  • Solution to Incorrect string value in MySQL

<<:  How to start/stop Tomcat server in Java

>>:  JS asynchronous code unit testing magic Promise

Recommend

Windows 10 1903 error 0xc0000135 solution [recommended]

Windows 10 1903 is the latest version of the Wind...

Meta tags in simple terms

The META tag, commonly referred to as the tag, is...

Detailed explanation of the execution process of MySQL query statements

Table of contents 1. Communication method between...

Nginx 502 Bad Gateway Error Causes and Solutions

I have encountered the Nginx 502 Bad Gateway erro...

Detailed process of installing logstash in Docker

Edit docker-compose.yml and add the following con...

The qualities and abilities a web designer should have

Web design is an emerging marginal industry that c...

Three ways to implement animation in CSS3

This is a test of the interviewee's basic kno...

How to use Nginx proxy to surf the Internet

I usually use nginx as a reverse proxy for tomcat...

Introduction to cloud native technology kubernetes (K8S)

Table of contents 01 What is Kubernetes? 02 The d...

This article takes you to explore NULL in MySQL

Table of contents Preface NULL in MySQL 2 NULL oc...

javascript realizes 10-second countdown for payment

This article shares the specific code of javascri...

Markup language - web application CSS style

Click here to return to the 123WORDPRESS.COM HTML ...

How to build an ELK log system based on Docker

Background requirements: As the business grows la...