Garbled MySQL database solution

Source: Internet
Author: User
Tags mysql command line

It has been nearly two months since the previous project was launched, and the problem has not been summarized. Take advantage of the days of the cold cave, the season of all things withered to their own experience, in order to promote the future.

No doubt, whetherProgramDevelopment or website development, the most tangled problem is garbled code. I have a deep understanding and are deeply affected. However, after thorough reflection and expert guidance, we finally solved this thorny problem. Share it.

Garbled characters occur in many stages of the development process. For example, the front-end page (website development case) needs to set the encoding method for the process of submitting data to the servlet, And the servlet also needs to set the encoding method for receiving data, servlet needs to be set when outputting data directly to the front-end, when connecting to the database, and when creating a database, it also needs to be set. When querying information in the MySQL command line table, there may also be garbled characters, in this case, you also need to handle it. Now, I will show you how to solve these problems in my website project.

1. set the encoding method of the web page in the JSP page to UTF-8. of course, gb2312 or GBK can also be used, and the latter two can implement Chinese encoding. Maybe such data is inserted into the database and will not be garbled during query. In any case, first determine an encoding scheme, and then the encoding settings of each place must be consistent with that of this place. MyCodeYes: <% @ page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>

2. In the servlet class in the background, set the encoding method that accepts foreground parameters in the dopost method to UTF-8. For example, req. setcharacterencoding ("UTF-8"); of course, if you need to use servlet to generate a Web page, you must set the encoding scheme of the generated web page: resp. setcontenttype ("text/html; charset = UTF-8 ").

3. When writing the tool class connected to the database, write the encoding method: String url = "JDBC: mysql: // 117.0.0.1/xiaohuier88040? User = xiaohuier88040 & Password = xiaohuier88040 & useunicode = true & charactorencoding = utf8 ". Why do you need to write this? We will continue to introduce it later. Note that there is no barrier between the encoding methods utf8. If there is a barrier, it cannot be anything else.

So far, the encoding process for garbled characters has been completed. It should be noted that the data stored in the database under such settings is encoded according to the UTF-8 encoding specification, but UTF-8 cannot support Chinese characters. Therefore, the Chinese information found in the MySQL Command Prompt line is garbled. In this case, you need to execute set names = GBK before the query statement, and then execute the query statement, you can see the cute Chinese.

Attention should also be paid to the small details of irrelevant encoding methods. For example, when I insert data without adding single quotation marks of the string type, the result shows that the Chinese characters cannot be inserted successfully. I thought it was a coding problem. As a result, I tried my best to find out the encoding problem and finally did not solve it. Finally, I found this problem at the prompt of a previous employee on the Internet. Really shouldn't. I have solved this problem before. Carelessly kills cats.

In the next few chapters, I will discuss the principles of setting and encoding. Continuous attention.

 

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.