The JSP Chinese garbled problem page is filtered to get Chinese characters, but inserted into MYSQL_MySQL

Source: Internet
Author: User
The JSP Chinese garbled problem page is filtered to get Chinese characters, but inserted into the MYSQL database becomes "?" Why?
When I recently logged on to the database on the company's server, all the results obtained when I executed the query statement in the command line were garbled as long as they were in Chinese, which is a headache,
I checked some information and summarized it as my reference.

MySQL Character Set Support has two aspects: Character set and Collation ).

The support for character sets is refined to four layers:
Server, database, table, and connection ).

1. MySQL default character set: MySQL can specify character sets to a database, a table, and a column.
Traditional programs do not use such complex configurations when creating databases and data tables. they use default configurations.

(1) when compiling MySQL, a default character set is specified, which is latin1;
(2) when installing MySQL, you can specify a default character set in the configuration file (my. ini). If this character set is not specified, this value inherits from the one specified during compilation;
(3) when mysqld is started, you can specify a default character set in the command line parameters. if not, this value inherits from the configuration in the configuration file,
Character_set_server is set to the default character set;
(4) when creating a new database, unless explicitly specified, the character set of this database is set to character_set_server by default;
(5) when a database is selected, character_set_database is set to the default character set of the database;
(6) when creating a table in the database, the default character set of the table is set to character_set_database, which is the default character set of the database;
(7) when setting a column in a table, unless explicitly specified, the default character set in this column is the default character set of the table. If no modification is made,
Then all columns of all tables in all databases are stored in latin1,

However, if we install MySQL, we usually choose multi-language support, that is, the installer will automatically set default_character_set to UTF-8 in the configuration file,
This ensures that by default, all columns of all tables in all databases are stored in UTF-8.

2. view the default character set (mysql character set is latin1 (ISO_8859_1) by default. Generally, you can run the following two commands to view the character set and sorting method of the system:

Mysql> show variables like 'character % ';
+ -------------------------- + ----------------------------------- +
| Variable_name | Value |
+ -------------------------- + ----------------------------------- +
| Character_set_client | latin1 |
| Character_set_connection | latin1 |
| Character_set_database | latin1 |
| Character_set_filesystem | binary |
| Character_set_results | latin1 |
| Character_set_server | latin1 |
| Character_set_system | utf8 |
| Character_sets_dir | D: "mysql-5.0.37" share "charsets" |
+ -------------------------- + ----------------------------------- +

Mysql> show variables like 'collation _ % ';
+ ---------------------- + ----------------- +
| Variable_name | Value |
+ ---------------------- + ----------------- +
| Collation_connection | utf8_general_ci |
| Collation_database | utf8_general_ci |
| Collation_server | utf8_general_ci |
+ ---------------------- + ----------------- +

3. modify the default character set

(1) the simplest modification method is to modify the character set key value in mysql's my. ini file,
For example, after default-character-set = utf8 character_set_server = utf8 is modified, restart the mysql service. service mysql restart uses mysql> show variables like 'character % '; view, the database encoding has been changed to utf8 + ------------------------ + encoding + | Variable_name | Value | + encoding + | character_set_client | utf8 | character_set_connection | utf8 | character_set_database | u Tf8 | character_set_filesystem | binary | character_set_results | utf8 | character_set_server | utf8 | character_set_system | utf8 | character_sets_dir | D: "mysql-5.0.37" share "charsets" | + -------------------------- + ------------------------------- + (2) There is also a way to modify the character SET, is to use the mysql command mysql> SET character_set_client = utf8; mysql> SET character_set_connection = utf8; mysql> SET character_set_data Base = utf8; mysql> SET character_set_results = utf8; mysql> SET character_set_server = utf8; mysql> SET collation_connection = utf8; mysql> SET collation_database = utf8; mysql> SET collation_server = utf8; generally, even if the default character set of the table is set to utf8 and the query is sent through the UTF-8 encoding, you will find that the database is still garbled.
The problem lies in the connection layer. The solution is to execute the following sentence before sending the query: set names 'utf8'; it is equivalent to the following three commands: SET character_set_client = utf8; SET character_set_results = utf8; SET character_set_connection = utf8; I don't know why. after I SET all the three character sets on my machine to utf8, the Chinese query results are still garbled,
Only after character_set_results is set to GBK can Chinese characters be displayed normally from the command line.
The simplest modification method is to modify mysql's my. the character set key value in the INI file, for example, default-character-SET = utf8 character_set_server = after utf8 is modified, restart the mysql service, such as service mysql restart or SET character_set_client = utf8; SET character_set_database = utf8; SET character_set_results = utf8; SET character_set_server = utf8; SET collation_connection = utf8; SET collation_database = utf8; SET collation_server = utf8; if you use SQLYOG, you may see garbled characters in the table after you put it into the database. however, it can still be displayed as Chinese. use Nacivats should be better. you can directly set the character set of the database. if the server is tomcat. it is best to set tomcat. in server. the settings in xml are as follows:
  EnableLookups = "false" redirectPort = "8443" acceptCount = "100" debug = "0"
ConnectionTimeout = "20000" disableUploadTimeout = "true" URIEncoding = "UTF-8"/> Note: URIEncoding = "UTF-8" is added by yourself. restart should be no problem ..

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.