Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The support of mysql/Java server to emoji and the solution to the problem

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The mysql/Java server supports emoji and the solution to the problem. I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Problem description

Save the underlying Weibo data to mysql. Some of the data failed to be saved. After viewing Tomcat, the core error message is as follows:

/ / throw an exception sql.SQLException: Incorrect string value:'\ xF0\ x9F\ x98\ x97\ xF0\ x9F.. 'that is not supported by the character set For column 'CONTENT' at row 1

Cause analysis

Prior to MYSQL 5.5, utf8 coding only supported 1 to 3 bytes, that is, the unicode coding region of BMP (basically 0000~FFFF). Emoji emoji is 4 bytes, while mysql's utf8 encoding is up to 3 bytes, so after causing the data not to be inserted into 5.5, mysql adds utf8mb4 encoding, which can support 4 bytes, that is, a character can have up to 4 characters, so utf8mb4 coding can support more character sets. Therefore, if the project is to store user text for mobile products, the DB character set must be upgraded from traditional character sets such as UTF8/GBK to utf8mb4

[note]

Utf8mb4 is compatible with utf8 and can represent more characters than utf8. Therefore, there is no need to worry about character compatibility when upgrading the DB character set from traditional character sets such as UTF8 or GBK to utf8mb4.

Solution method

Filter special emoji

Content = content.replaceAll ("[\ x {10000} -\ x {10FFFF}]", "); / / simple, rude and impersonal

Upgrade database character set

1. The mysql version supported by utf8mb4 is 5.5.3. If the mysql is lower than this version, please upgrade first.

2. Modify the database, table and column character sets

3. Modify the mysql configuration file my.cnf (windows is my.ini). My.cnf is usually located in the etc/mysql/ directory. After finding it, add the following content in the following three parts:

[client] default-character-set = utf8mb4

[mysql] default-character-set = utf8mb4

[mysqld] character-set-client-handshake = FALSEcharacter-set-server = utf8mb4collation-server = utf8mb4_unicode_ciinit_connect='SET NAMES utf8mb4'

4. Restart mysql and check the character set

At the mysql command line, enter:

SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE' collation%'

Character set before reboot

Character set after restart

[note]

You must ensure that the following fields are utf8mb4 character_set_client character_set_connection character_set_database character_set_results character_set_server

After the above four steps are completed, use mysql client navicat to manually add Emoji emoji ⛄to the table. If you can save it successfully, it means that the database has been upgraded successfully.

For the specific differences in these character set configurations, please refer to the in-depth Mysql character set settings.

5. Check the server-side db configuration file

/ / the link used when configuring spring is as follows: url= "jdbc:mysql://localhost:3306/gzhou?allowMultiQueries=true&useUnicode=true&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&autoReconnectForPools=true&failOverReadOnly=false&maxReconnects=10" / / if characterEncoding=utf8mb4, insert data Times error Caused by: java.sql.SQLException: Unsupported character encoding 'utf8mb4'// remove characterEncoding or characterEncoding=utf8

Emoji expression

In the android system, Sogou and Baidu input methods have their own Emoji expressions.

[note]

If Emoji emoticons are not compatible between systems, you can only see squares

After reading the above, have you mastered the mysql/Java server's support for emoji and the solution to the problem? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report