Key Methods and summary of MySql database garbled characters

Source: Internet
Author: User

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: useUnicode = true & characterEncoding = gbk must be added to MySQL 5.0 and later, adding or not does not matter!

(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! Such
Com. mysql. jdbc. MysqlDataTruncation: Data truncation: Data too long for column.

Mysql Chinese garbled Solution
I often see mysql garbled characters in the group, but I don't care much because I have been using oracle and SQL server, I used mysql yesterday and met the legendary Garbled text ...... Now let's talk about mysql garbled characters. First you enter your mysql
Use show variables to view


Here we can see your character set. character_set_server is the server code. Now, the default value is that latin1 must restart the server to change the server encoding. We will first use mysqladmin-uroot shutdown to close the server, then mysqld-c gbk starts the server-C is a shorthand for character_set_server (you can also write it as mysqld-character_set_server gbk ). after the modification, we can go to mysql to view the result:

In this way, we can change the server and database encoding to GBK.
Now we can insert a Chinese string, but why is there time?
Exception in thread "main" com. mysql. jdbc. MysqlDataTruncation: Data truncation: Data too long for column 'name' at row 1
This is because you only modified the character encoding of the mysql server and did not modify the encoding of the database. At this time, you need to manually change the encoding in db. opt in the database under the data Directory
Default-character-set = gbk
Default-collation = gbk_chinese_ci
In this way, you can insert Chinese characters After restarting the server.
But there is another problem at this time, that is, the Chinese characters are displayed in your application, but garbled characters are still displayed in the Command window.
At this time, you should pay attention to the character_set_client character or latin1 shown in the figure above. Of course, it is garbled. Therefore, when you enter the mysql client program, you must change the default encoding of the client to gbk to display it normally.
You can modify it in mysql or mysql-uroot-default-character-set = gbk at login.
So far, the Chinese problem has been solved.
At the beginning, I added a characterEncoding variable () to the program, but I thought it was useless when I thought about it, so I can still display it normally after I removed it.
Jdbc: mysql: // 127.0.0.1: 3306/ee? CharacterEncoding = gbk"

To sum up the problem of mysql garbled characters, it is best to set the server encoding and database encoding to a long gbk when setting the database. Also, check the system variables.

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.