We also provide a detailed description of the MySQL garbled problem in the correct solution to the garbled problem in JSP and solve the Chinese garbled problem in tomcat, I believe you will solve some garbled code problems in JSP after reading it, but some MySQL garbled code problems are still not solved.
I have found some other materials, including myself. I hope this will completely solve the problem of garbled characters in the MySQL database.
MySQLThe first method of the garbled solution is an incisive summary:
The VM is often changed, and the MySQL versions of different service providers are different. After data is imported, garbled characters and other problems cannot be displayed normally. I checked a lot of information and summarized my skills:
This is the case when importing data in WINDOWS.
Use MySQL commands
Run the doscommand to enter the MySQL bin directory and enter the MySQL-uroot-p Password Database Name <the database to be restored. For example, we need to name disk D as test. the SQL database is restored to the local test2 database:
MySQL-uroot-p password test2 is used for foreign hosts 4. x series, I feel better, no matter GBK and UTF-8 are not garbled, did not expect the new host MySQL is 5.0 version, after importing data, read out with Php is question mark, garbled text. I remember that I had a previous experience with Garbled text during Switching. The reason must be the difference between MySQL versions.
I had to check the information and found a solution. I added a set names UTF8 sentence after MySQL_connect to eliminate garbled characters in the UTF8 database. For GBK databases, I used SET NAMES GBK, the Code is as follows:
- $ mysql _mylink = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
- mysql_query("SET NAMES 'GBK'");
The database character set is UTF-8.
The connection statement uses this
- mysql_query("SET NAMES 'UTF8'");
- mysql_query("SET CHARACTER SET UTF8");
- mysql_query("SET CHARACTER_SET_RESULTS=UTF8'");
Another way is to change the MYSQL character set if your machine is running. It is generally valid in MYSQL 4 and MYSQL 5.
The second method of the mysql garbled solution: a good explanation. Try it.
Summary of mysql garbled processing:
(1) Chinese characters are processed normally in java. Displaying garbled characters in the cmd client is a problem with character sets.
(2) The length of the field is long enough. However, when a Chinese character is inserted, the system prompts com. mysql. jdbc. MysqlDataTruncation: Data truncation: Data too long for column.
(3) garbled characters are the final issue of character sets. We should consider the following aspects from Character Set settings: server, client, database, connection, and results.
Solution:
(1) modify the my. ini (MySQL Server Instance Configuration file)
- # CLIENT SECTION
- [client]
- port=3306
- [mysql]
- default-character-set=gbk
- # SERVER SECTION
- [mysqld]
- default-character-set=gbk
(2) modify the db. opt configuration file under the corresponding database directory in the data Directory
- default-character-set=gbk
- default-collation=gbk_chinese_ci
(3) Character Set specified in the database connection string
URL = jdbc: mysql: // yourIP/college? User = root & password = yourPassword & useUnicode = true & characterEncoding = gbk
Note: in MySQL 4.1.0, useUnicode = true & characterEncoding = gbk must be added. if the version is later than 5.0, it does not matter if you do not add it!
(4) Specify the character set when creating a database
- create database yourDB CHARACTER SET gbk;
(5) On the doscommand, Set
- set names gbk
Okay. Check the above four settings. It should be OK! There will be no issues such as com. mysql. jdbc. MysqlDataTruncation: Data truncation: Data too long for column.
The above content is an introduction to the mysql garbled solution. I hope you will get some benefits.