Three methods to solve MYSQL Chinese garbled characters

Source: Internet
Author: User
Tags mysql tutorial

Three methods to solve MYSQL Chinese garbled characters

Method 3

1> in this article, garbled characters are eliminated in three steps:

> Eliminate page garbled characters,

> Remove Chinese garbled characters from the mysql tutorial,

> Eliminates Chinese garbled characters in the mysql database tutorial;

15> note the difference between jsp and mysql in the special webpage effect p/jsp.html target = _ blank>:

Jsp: gb2312, gbk, UTF-8 ....

Mysql: gb2312, gbk, utf8 ....

Key Issue: encoding. We recommend that you use gb2312 or gbk. This document uses gb2312 and gbk is not tested;

[Step 1]: Eliminate gibberish code running on pure html pages and jsp pages in tomcat Environment

This is because the browser cannot select the encoding method. You can manually select "gb2312" from ie. You can also add the meta tag or page command to the webpage;

The meta tag is as follows:

<Meta http-equiv = "content-type" content = "text/html; ch *** t = gb2312">

The page command is as follows:

<% @ Page language = "java" contenttype = "text/html; ch *** t = gb2312" pageencoding = "gb2312" %>

As mentioned above, meta may not work in 2 k (it may be bad in xp). For simplicity, adding the page command seems to be a permanent task;

Conclusion: by adding the page command, the pure jsp page or html page can be displayed normally in the tomcat environment.

[Step 2]: Eliminate Chinese garbled characters read from the mysql database

There are two goals: Step 1: Check that Chinese is normal from the mysql client, Step 2: Check that Chinese is normal from the web page;

1: Before using an SQL script to create a database, set the default mysql encoding method to "gb2312 ",

This step can be done in the mysql-administrator-1.1.0-rc-win.msi, or the mysql server wizard;

After the settings are complete, use SQL scripts to create a database. Note that if the SQL script contains a statement that sets the character set to latin1 or UTF-8, you must delete it.

You have found that the SQL script in the current version of online bookstore contains the default Character Set latin1, which must be deleted!

After the database is created, you can use mysql cc or mysql admin to check the character set of the created database. If the character set is correct, mysql cc or mysql admin can view Chinese characters normally;

2: Generally, Chinese characters can be displayed normally on the webpage at this step, but there are always exceptions. Please note the following in the database connection string:

String url = "jdbc: mysql: // localhost/bookstore? Useunicode = true & characterencoding = gb2312 ";

Conn = drivermanager. getconnection (url, "root ","");

Generally, you can use string url = "jdbc: mysql: // localhost/bookstore" to display Chinese characters normally.

Conclusion: The default Character Set of the mysql server database is modified to "gb2312", and Chinese characters can be displayed normally after being read from mysql;

Exception: Garbled characters may still exist at this time. For example, in the online bookstore on the CD-ROM of this book, please refer to the previous instructions.

[Step 3]: Eliminate Chinese garbled characters inserted into the mysql database

This step is relatively simple. Add the following code to the jsp page:

Request. setcharacterencoding ("gb2312 ");

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.