In the development of the program, when we use the MySQL database development, sometimes we will encounter their own input is the Chinese, why the database is stored in the????
1 , in the Configure connection URL when, plus? Useunicode=true&characterencoding=utf-8
2. Edit/etc/my.cnf
Add under [mysqld]
Default-character-set=utf8
Add under [client]
Default-character-set=utf8
But what is the principle of doing this? Why is this going to solve the problem?
Gradually analyze:
1 , Useunicode=true does this use a plus? The answer is no, look through the connector-j-en.a4.pdf.
Its default value is True
2. mysqld under the Default_character_set=utf8; , does this need to be? The answer is no need. View refman-5.1
Its role is to
When mysqld is started, the server character set and proofing rules are determined based on the initial option settings used. You can use--default-character-set to set character sets;
If the table character set and proofing rules are not specified in the CREATE TABLE statement, the database character set and proofing rules are used as default values. The character set and proofing rules for the default database can be used as character_set_database and collation_database system variables. The server sets the values for both variables whenever the default database changes. If there is no default database, these two variables have the same value as the corresponding server-level variables (Character_set_server and Collation_server).
Plain English is: If you do not specify a character encoding when you build the table, you will use Character_set_database, and if Character_set_database also has no value, use Character_set_server
3. Client under the Default_character_set=utf8; It is needed, but what is its role?
Its role is equivalent to executing the following 3 commands
SET character_set_client = UTF8
SET character_set_results = UTF8;
SET character_set_connection = UTF8;
The functions of these 3 parameters are as follows
System variable Character_set_client: Used to specify the encoding to parse the client passing data
System variable Character_set_connection: Used to specify the encoding used for internal processing of the database
System variable Character_set_results: Used to specify how the data is encoded to be returned to the client
Now tell me about it . characterencoding in useunicode=true&characterencoding=utf-8
its role is to specify values for Character_set_client and character_set_connection, and to use Charactersetresults=utf-8 in JDBC links, You can set the value of the Character_set_results
But as mentioned above, when you configure the client's default_character_set , characterencoding and charactersetresults It doesn't matter whether you set these two variables or not.
The conclusions are as follows:
If you only want to configure the server:
Add under [client]
Default-character-set=utf8 can be
If you only want to configure the client:
can you use Jdbc:mysql://localhost:3306/test characterencoding=utf-8& charactersetresults=utf-8?
Using JDBC
Character_set_client-utf8
Character_set_connection-utf8
Character_set_database-utf8
Character_set_filesystem-binary
Character_set_results-
Character_set_server-utf8
Character_set_system-utf8
Character_sets_dir-/usr/share/mysql/charsets/
Using the MySQL Client
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/ |