MySQL garbled in two cases

Source: Internet
Author: User

For the MySQL database garbled problem, there are two cases:

1. MySQL database encoding problem (set when building the library).

2. Link the URL encoding setting of the MySQL database.


For the first question, the current personal discovery can only be solved by re-building the library, when the database is built, select the UTF-8 character set. I tried to modify the existing database character set to Uft

-8, but does not work at all, the inserted Chinese is still garbled (Chinese display as:??? )。 When the library is rebuilt with the character set UTF-8, Chinese is displayed normally.


For the second question, this is the case: when I built the library, the default character set for the database was UTF-8, and it was perfectly normal to insert the Chinese display directly via MySQL Workbench. But when inserting data using Mybaits, Chinese is displayed as "???" Such a garbled. But the Chinese obtained from the database is not garbled. Tracking database operations, the Chinese in the SQL statement is still normal, but after inserting into the MySQL database is garbled, so the judgment may be a database connection problem. Later on the Internet to find the information, found that the MySQL database can indeed set the connection string encoding method, as follows:

Jdbc:mysql://127.0.0.1:3306/test? Useunicode=true&characterencoding=utf8

After adding the useunicode=true&characterencoding=utf8 parameter, inserting Chinese is normal.


The purpose of the addition is to specify the encoding and decoding format of the characters.

For example, suppose that the MySQL database is GBK encoded (or possibly others, such as Ubuntu under Latin1), and the project database is UTF-8 encoded. If Useunicode=true&characterencoding=utf-8 is added at this time, then there are two aspects of the function:

1. When data is stored:

when storing the project data, the database decodes the data into bytecode using the UTF-8 format, and then the decoded bytecode is reused in the database using the GBK encoding.

2. When data is taken:

when fetching data from the database, the database will first decode the data in the database into bytecode in GBK format, then encode the decoded bytecode again in UTF-8 format, and then return the data to the client.

MySQL garbled in two cases

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.