MySQL Chinese does not display the problem

Source: Internet
Author: User

MySQL Character set support (Character set supports) has two aspects:
The character set (Character set) and the Sort method (Collation).
Support for character sets is refined to four levels:
Servers (server), database, data tables (table), and connections (connection).

1.MySQL Default Character Set

MySQL Specifies the character set that can be refined to a database, a table, a column, and what character sets should be used.

However, traditional programs do not use a complex configuration when creating databases and datasheets, and they use the default configuration, so where does the default configuration come from?

(1) When compiling MySQL, a default character set is specified, and this character set is latin1;
(2) When installing MySQL, you can specify a default character set in the configuration file (My.ini), and if not specified, this value is inherited from the compile-time specified;
(3) When starting mysqld, you can specify a default character set in the command line arguments, if not specified, this value inherits from the configuration in the configuration file, at this time Character_set_server is set to this default character set;
(4) When a new database is created, the character set of the database is set to Character_set_server by default unless explicitly specified;
(5) When a database is selected, Character_set_database is set to the default character set of this database;
(6) When creating a table in this 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 a column is set in the table, the default character set of the column is the set of defaults for the table unless explicitly specified;
(8) Character_set_client refers to the current client's character set

Simply summarize, if there is no change anywhere, then all the tables of all the columns of the database are stored with latin1, but if we install MySQL, we will generally choose multi-language support, that is, the installation program will automatically in the configuration file Default_character _set is set to UTF-8, which guarantees that all the columns of all the tables in the database are stored UTF-8 by default.

2. View the default character set (by default, MySQL's character set is Latin1 (iso_8859_1)
In general, the settings for viewing the character set and ordering of the system can be set through the following two commands:
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. Modifying the default character set
(1) The simplest method of modification is to modify the character set key values in the MySQL My.ini file,
such as Default-character-set = UTF8
Character_set_server = UTF8
After the modification, restart the MySQL services, service MySQL restart
Use mysql> show VARIABLES like ' character% '; view, discover that database encoding has been changed to UTF8
+--------------------------+---------------------------------+
| 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 | D: "mysql-5.0.37" Share "charsets" |
+--------------------------+---------------------------------+

(2) There is also a way to modify the character set, that is, the command to use MySQL
mysql> SET character_set_client = UTF8;
mysql> SET character_set_connection = UTF8;
mysql> SET character_set_database = 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;

In general, even if the default character set for the table is UTF8 and the query is sent through UTF-8 encoding, you will find that the database is still garbled. The problem is on the connection connection layer. The workaround is to execute the following sentence before sending the query:

SET NAMES ' UTF8 ';

It is equivalent to the following three-sentence instruction:
SET character_set_client = UTF8;
SET character_set_results = UTF8;
SET character_set_connection = UTF8;

Summarize:
Therefore, the use of what database version, whether it is 3.x, or 4.0.x or 4.1.x, in fact, it is not important for us, it is important to have two:
1) correctly set the database code. MySQL4.0 the following version of the character set is always the default iso8859-1,mysql4.1 when installed will let you choose. If you are ready to use UTF-8, you will need to specify a good UTF-8 when you create the database (you can change it after you create it, and you can specify the table's character set for more than 4.1 versions)
2) correctly set the database connection encoding. After you have set up the encoding of the database, you should specify the encoding of the connection when you connect to the database, such as when using a JDBC connection, specify the connection as the UTF8 mode.

Configuration URL in Java:

String url= "Jdbc:mysql://localhost:3306/student? user=root&password=123&useunicode=true&characterencoding=gb2312 ";

MySQL Chinese does not display the problem

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.