preface

Mysql > save wechat nickname or other emoji-containing text to mysql database

Java.sqlexception: Incorrect String value: '\xF0\x9F\x92\x94' for column 'name',Copy the code

This is because the database character encoding is usually UTF8 (the supported encoding range is \u0000-\uFFFF), and the Emoji encoding range is outside of MySql’s boundaries.

There are two solutions:

Alter mysql character set to UTf8mb4

Refer to the link: https://blog.csdn.net/shoot_your_face/article/details/67632062Copy the code

2. Use the Emoji conversion library

The following two are recommended:

Github.com/vdurmont/em…

<dependency> <groupId>com.vdurmont</groupId> <artifactId>emoji- Java </artifactId> <version>5.1.1</version> </dependency>Copy the code

Github.com/binarywang/…

<dependency> <groupId>com.github.binarywang</groupId> <artifactId>java-emoji-converter</artifactId> The < version > 1.0.0 < / version > < / dependency >Copy the code

Refer to relevant documents for specific usage methods.

However, this method also has some limitations. Some new emojis cannot be successfully converted by the conversion library, so some special processing needs to be done, such as replacing them again after using the conversion library:

STR. ReplaceAll ([^ \ \ "u0000 - \ \ uFFFF]", "-");Copy the code

Convert all unconverted emoji characters to “□” or any other character to ensure that the database saves without error.