MySQL 5.x

Source: Internet
Author: User


MySQL 5.x handles all Chinese characters. I use Java to connect to the MySQL database and write data through JDBC. The Chinese character string directly generated in Java is written into the database and becomes the question mark "?". Below are my records for solving this problem. First, I use Putty to connect to MySQL to check Chinese writing. Here is a setting. If this setting is incorrect, garbled characters may occur. MySQL then provides many variables about character sets. You can use the MySQL command to view details. Enter the command: mysql> show variables like '% char %'; the following content is displayed: www.2cto.com

I believe all these variables are utf8? For example, some are 'utf-8' and some are 'latin1? Change all to 'utf8' to ensure consistency without garbled characters. SET the variable statement format: mysql> SET character_set_server = 'utf8'; Modify variable names one by one to those with values other than utf8, and execute them one by one. In addition, unless all systems + all users are pure Microsoft environments, do not use the GBK character set promoted by Microsoft, which is not universal. Next I will explain why utf8 must be used instead of a UTF-8 or UTF-8 name. You can see the special variable character_sets_dir. You can see many character set files in the specified directory. Here, Index. xml is an Index file of the Character Set Name, which contains the following section:

We just need to make the specified Character Set name consistent with the charset name set here. It is written as utf8, so our variables must be set as utf8. The variable set by the command will be restored after restart. Therefore, we can consider using the my. cnf file to directly configure these MySQL variables. You must back up the entire file before modifying the configuration file. This file cannot be changed. In the test result, add default-character-set = utf8 in [client] And character_set_server = utf8 in [mysqld, then ensure that there is a default-character-set = utf8 in the bottom [mysql. After this setting, restart mysqld and you do not need to reset these variables every time you enable MySQL. Www.2cto.com ---------- the database has been resolved, followed by the problem of connecting to the database using Java. The URL of the JDBC connection is required. For MySQL, this connection should set several parameters: useUnicode = true & characterEncoding = UTF-8. This writing should be strictly consistent, there must be no deviation, such as the UTF-8 written utf8 will cause function disorder. If there are no other connection parameters, use? Bootstrap these two parameters. Otherwise, use & Bootstrap. Of course, the data in Java cannot be garbled. This is another thing. To ensure that the value of the monitoring variable is not garbled during Java runtime, you can ensure that the read/write database is no longer garbled.
 

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.