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 treatment of Mysql utf8mb4 character set by JDBC

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

When developing the Wechat open platform and accessing the data of Wechat official account, the nickname and text messages about users were that a large number of emoji text messages appeared, which timed out the storage range of the utf-8 character set.

When storing data, some special characters failed to store strings.

Server-side solution

When consulting the official documents of MySQL, the following official ways to support the conversion of utf8 to utf8mb4 are given

Https://dev.mysql.com/doc/refman/5.7/en/charset-unicode-conversion.html

The above steps are only for server-side configuration.

Additional special processing is required for the client

Because my current project is written in JAVA and uses JDBC connections to operate the database, here is the solution for JDBC operations

The previous configuration of JDBC's URL

String url = "jdbc:mysql://HOST:PORT/DATABASE?useUnicode=true&characterEncoding=UTF-8"

In connection configuration, declare the character encoding of UTF-8. In terms of current storage requirements, an error will be reported when storing emoji strings. Even if the database on the server side sets the utf8mb4 character set type

The client is modified to the following configuration

String url = "jdbc:mysql://HOST:PORT/DATABASE?useUnicode=true&characterEncoding=utf8mb4"

There is an error in the client connection, the character set is not supported

The solution to the above problems

The connection configuration is changed to

String url = "jdbc:mysql://HOST:PORT/DATABASE"

At the same time, the database configuration file my.cnf is modified accordingly.

[client] default-character-set = utf8mb4 [MySQL] default-character-set = utf8mb4 [mysqld] character-set-client-handshake = FALSEcharacter-set-server = utf8mb4collation-server = utf8mb4_unicode_ci

Restart the service MySQL service

The problem is solved here.

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

Database

Wechat

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

12
Report