See a good article, transcribed in this
Chinese garbled seems to be a program in the eternal topic and difficulties, such as MySQL access Chinese garbled, but I want to do anything, there must be a train of thought, there is a way to know how to solve the problem, otherwise, even if a temporary solution to the problem, but soon after the same problem may be anxious to get mad, MySQL Chinese garbled problem is so.
Only for the MySQL Chinese garbled solution, I think the main principle can be attributed to five words: "coding consistency", as long as follow this principle, then the Chinese garbled is not difficult to solve, then what is "code consistency"? Child monkeys summarized as follows four aspects of the code must be consistent, in order to prevent Chinese garbled, the following will be explained in detail.
First I want to set the code for UTF8, why to set the UTF8 code it? That is because this is a general-purpose code, such as China's usual use of GBK, GB2312, Big5 and so on only for Chinese, but not for the other text is not applicable, in order to make this problem with the text encoding universal, so I set the UTF8 this code.
Coding consistency involves four aspects: application coding, database system coding, database encoding, application and database system connection coding.
1. Application code
Refers to your program files in the text encoding, to JSP file as an example to illustrate, is your JSP page encoding, such as:
<%@ page contenttype= "text/html; Charset=utf-8 "%>
In such a page, it is often called the database interface to the database access operations.
2, corresponding to the database system code
Database system is your use of MySQL (and other database system), the database system encoding to be consistent with the application code, its encoding settings according to the operating system can be set in the following ways
2.1. mysql encoding settings in WINDOSW environment
A. Abort MySQL Service
B, in the MySQL installation directory to find My.ini, if not the My-medium.ini copy as a my.ini can
C, after opening My.ini, under [client] and [mysqld] add Default-character-set=utf8, save and close
D. Start the MySQL service
(Note: Settings in the Windows environment I have not verified)
2.2. mysql encoding settings in Linux environment
First we log in to MySQL, execute: Show variables like ' character% '; to see how the database system is currently encoded, you will typically see the following:
+ ———————— –+ —————————-+
| 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 | /usr/share/mysql/charsets/|
+ ———————— –+ —————————-+
That is, the default encoding is Latin1, and it is clear that we are going to modify it to UTF8.
By: Vi/etc/mysql/my.cnf Modify the My.cnf file, it is important to note that if the my.cnf file does not exist, then go to the installation directory of MySQL, by executing: CP share/mysql/my-large.cnf/etc/ MY.CNF generate the My.cnf file.
Join the following:
Under Client configuration [clients], add the following:
# # # Default character set is UTF8
Default-character-set=utf8
Locate the [mysqld] section, and add the following:
# # # Default character set is UTF8
Default-character-set=utf8
# # # (set the MySQL database to use UTF8 encoding to let MySQL database run for UTF8)
init_connect= ' SET NAMES utf8′
After completion, restart the MySQL service and then execute show variables like ' character% ';
Mysql> Show variables like ' character% ';
+ ———————— –+ —————————-+
| variable_name | Value |
+ ———————— –+ —————————-+
| character_set_client | UTF8 |
| character_set_connection | UTF8 |
| Character_set_database | UTF8 |
| Character_set_filesystem | binary |
| Character_set_results | UTF8 |
| Character_set_server | UTF8 |
| Character_set_system | UTF8 |
| Character_sets_dir | /usr/share/mysql/charsets/|
+ ———————— –+ —————————-+
3. Database encoding
After changing the database system code, next to the database encoding, please note the database system and database differences, database refers to the database you created and the tables contained, database encoding settings is very simple, in the creation of the data table when set, for example:
CREATE TABLE Test
(
ID integer NOT NULL auto_increment,
NAME varchar (comment ' Test field '),
TYPE Integer,
Primary KEY (ID)
)
default CharSet UTF8;
Where default charset UTF8 means that the character encoding for this table is UTF8.
4, the application and database system connection code
The connection encoding is equivalent to the communication between the application and the database system, and it also needs to set the encoding, which is explained by the JDBC Connection statement:
jdbc:mysql://127.0.0.1:3306/test?useunicode=true&characterencoding=utf-8&user=test& password=111
The above statement is a database of test, the user name is test, the password is 111 database connection, which is encoded as characterencoding=utf-8.
Through the above four aspects of the setup, then the MySQL Chinese garbled easily solved!
If you do not remember a period of time the MySQL is how to solve the Chinese garbled, then you just know the above solutions, I would like to resolve it will be very fast.
Transfer from http://www.zihou.me/html/2010/06/07/2245.html
If all this is set correctly, you have to think about the problem of the program itself, if the page into the database itself is garbled, then it is the problem of the program, carefully check, sure enough, the problem appears in the servlet processing, add the following statement problem successfully resolved.
Req.setcharacterencoding ("Utf-8");
MySQL Database Chinese garbled