Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to end mysql Chinese garbled

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to end the mysql Chinese garbled code, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

I don't know how many days I have been worrying about Chinese problems. I hate Swedes most-who told him to invent mysql and set the default setting: latin1. I used to wonder if the Chinese invented mysql (my see Dog), hehe, then do we still have such trouble? The default is gk2312 or gbk. If you consider Taiwan friends add a big5. Just forget it. But what? This is not the case. I have no choice but to baidu, google, or even yahoo. I have tried all the ways I can find for a long time, until today I found it and figured it out. So I am eager to share the following experience with you.

In order to be clear, I would like to give an example:

It's simple to accept two inputs from html, and then the jsp process writes to mysql and returns the result from the database to display.

Mysql_jstl.html

CH14-Mysql_jstl.html

Save the information in Mysql-write it in JSTL

Last name:

First name:

Mysql_jstl.jsp

CH14-Mysql_jstl.jsp

Save the information in Mysql-write it in JSTL

INSERT INTO employee (employee_id, last_name, first_name, birth, sex, emmail)

VALUES (?)

SELECT * FROM employee

Take out all new names from employee:

New name:

The next step is to create a database named sample

Then create a table: employee with the following contents (it should be email, but I accidentally typed the database under construction and made a mistake):

+-+ +

| | Field | Type | Null | Key | Default | Extra | |

+-+ +

| | employee_id | int (10) unsigned | NO | PRI | NULL | auto_increment |

| | last_name | varchar (20) | YES | | NULL |

| | first_name | varchar (20) | YES | | NULL |

| | birth | date | YES | | NULL |

| | sex | enum ('masking dagger') | YES | | m |

| | emmail | varchar (39) | YES | | NULL |

+-+ +

If only this is the case, there will be such an error: the error reported is: sqle=com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long

For column 'last_name' at row 1 from employee!

Some cases are as follows: | name |

+-+

| |? |

| | 54243654321 | |

| |? |

| |? |

+-+ question mark appears!

As we have said earlier, the default code of mysql is latin1, which is not the gbk we need, so we need to modify it to utf8, because if we want to correctly display Chinese complex, simplified, Japanese and Korean using utf8, the modification method is as follows:

ALTER DATABASE sample # modify the encoding of the entire database here

CHARACTER SET utf8

DEFAULT CHARACTER SET utf8

COLLATE utf8_general_ci

DEFAULT COLLATE utf8_general_ci

Of course, you can also specify the encoding when building the database, such as:

CREATE DATABASE sample

CHARACTER SET utf8

DEFAULT CHARACTER SET utf8

COLLATE utf8_general_ci

DEFAULT COLLATE utf8_general_ci

The next thing to do is to open my.nin in the directory where mysql is located.

Mysql > show variables like'% character%' +-- +-- + | Variable_name | Value | +- -+-- + | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | utf8 | | character_set_results | latin1 | | character_set_server | utf8 | character_set_system | utf8 | | character_sets_dir | C:MySQLMySQL Server 5.0sharecharsets | +-- +-- + mysql > show variables like'% collation%' +-- +-+ | Variable_name | Value | +-+-+ | collation_connection | latin1_swedish_ci | | collation_database | utf8_general _ ci | | collation_server | utf8_general_ci | +-+-+

Then you look at the executable Mysql.html file: what you can see this time is

Mysql > select * from employee

+-+ +

| | employee_id | last_name | first_name | birth | sex | emmail | |

+-+ +

| | 12 |? | 1978-12-11 | f | aaa@asdf.com |

| | 13 |? | 1978-12-11 | f | aaa@asdf.com |

| | 14 |? | 1978-12-11 | f | aaa@asdf.com |

| | 15 |? | 1978-12-11 | f | aaa@asdf.com |

| | 16 |? | 1978-12-11 | f | aaa@asdf.com |

| | 17 |? | 1978-12-11 | f | aaa@asdf.com |

+-+ +

Of course, this is not what we want to see, we need the reality of perfect and correct Chinese:

We have one last resort:

Mysql > SET NAMES 'gbk'; Query OK, 0 rows affected (0.00 sec)

Because what we need is gbk.

Take a look at the character settings in mysql:

Mysql > SHOW VARIABLES LIKE'% character%'

+-+

| | Variable_name | Value |

+-+

| | character_set_client | gbk |

| | character_set_connection | gbk |

| | character_set_database | utf8 |

| | character_set_results | gbk |

| | character_set_server | utf8 |

| | character_set_system | utf8 |

| | character_sets_dir | C:MySQLMySQL Server 5.0sharecharsets |

+-+

7 rows in set (0.00 sec)

Mysql > SHOW VARIABLES LIKE'% collation%'

+-+ +

| | Variable_name | Value |

+-+ +

| | collation_connection | gbk_chinese_ci |

| | collation_database | utf8_general_ci |

| | collation_server | utf8_general_ci |

+-+ +

This is what we finally need.

Let's take a look at the results in the database after Mysql_jstl.jsp:

Mysql > select * from employee

| | 14 | Wang | Peng Ji | 1978-12-11 | f | aaa@asdf.com |

| | 15 | Tian | Wang Guang | 1978-12-11 | f | aaa@asdf.com |

| | 16 | interest | deposit | 1978-12-11 | f | aaa@asdf.com |

| | 17 | go | small cup | 1978-12-11 | f | aaa@asdf.com |

+-+ +

Can be happy to see the Chinese, and also displayed correctly in the browser.

But only that way, when you restart mysql

All the settings are invalid again.

Mysql > show variables like'% character%'

+-+

| | Variable_name | Value |

+-+

| | character_set_client | latin1 |

| | character_set_connection | latin1 |

| | character_set_database | utf8 |

| | character_set_results | latin1 |

| | character_set_server | utf8 |

| | character_set_system | utf8 |

| | character_sets_dir | C:MySQLMySQL Server 5.0sharecharsets |

+-+

Mysql > SHOW VARIABLES LIKE'% collation%'

+-+ +

| | Variable_name | Value |

+-+ +

| | collation_connection | latin1_swedish_ci |

| | collation_database | utf8_general_ci |

| | collation_server | utf8_general_ci |

+-+ +

Read again and garbled:

Mysql > select * from employee

+--

| | employee_id | last_name | first_name | birth | s |

+--

| | 12 |? | 1978-12-11 | f |

| | 13 |? | 1978-12-11 | f |

| | 14 |? | 1978-12-11 | f |

| | 15 |? | 1978-12-11 | f |

| | 16 |? | 1978-12-11 | f |

| | 17 |? | 1978-12-11 | f |

+--

So we need to set up the Chinese code gbk that can be recognized by the system on the client side and not save it to the my.ini file. So modify the my.ini file

Add some code to the [mysql] section and change it to: default-character-set=gbk so that the settings are saved.

Just restart it. Mysql > show variables like'% character%'

+-

| | Variable_name | Value |

+-

| | character_set_client | gbk |

| | character_set_connection | gbk |

| | character_set_database | utf8 |

| | character_set_results | gbk |

| | character_set_server | utf8 |

| | character_set_system | utf8 |

| | character_sets_dir | C:MySQLMySQL |

+-

Mysql > SHOW VARIABLES LIKE'% collation%'

+-+ +

| | Variable_name | Value |

+-+ +

| | collation_connection | gbk_chinese_ci |

| | collation_database | utf8_general_ci |

| | collation_server | utf8_general_ci |

The +-+-+ above is how to end the mysql Chinese garbled code. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report