Encoding: The page encoding is gbk2312, Java encoding is GBK, MySQL database table encoding is UTF-8, and tomcat default encoding is GBK.
Garbled characters: enter Chinese characters on the page. During the insert operation, the tomcat-level display is normal and the database displays garbled characters;
If you insert data directly in the database, the data is displayed on the page.
Error cause: the MySQL server code is not modified.
Solution: mysql. ini configuration file default_character_set = utf8
Knowledge introduction:
MySQL 4.1 character set support has two aspects: Character Set and collation ). The support for character sets is refined to four levels: Server, database, table, and connection ).
Mysql> show variables like 'character _ SET _ % ';
+ -------------------------- + ---------------------------- +
| 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 |/usr/share/MySQL/charsets/|
+ -------------------------- + ---------------------------- +
8 rows in SET (0.00 Sec)
mysql> show variables like 'collation _ % ';
+ ---------------------- + ----------------- +
| variable_name | value |
+ ---------------------- + ----------------- +
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+ ---------------------- + ----------------- +
3 rows in SET (0.00 Sec)
After modifying the mysql. ini configuration file default_character_set = utf8
Mysql> show variables like 'character _ SET _ % ';
+ -------------------------- + ---------------------------- +
| Variable_name | value |
+ -------------------------- + ---------------------------- +
| Character_set_client | Latin1 |
| Character_set_connection | Latin1 |
| Character_set_database | utf8 |
| Character_set_filesystem | binary |
| Character_set_results | Latin1 |
| Character_set_server | utf8 |
| Character_set_system | utf8 |
| Character_sets_dir |/usr/share/MySQL/charsets/|
+ -------------------------- + ---------------------------- +
7 rows in SET (0.00 Sec)
Mysql> show variables like 'collation _ % ';
+ ---------------------- + ------------------- +
| Variable_name | value |
+ ---------------------- + ------------------- +
| Collation_connection | latin1_swedish_ci |
| Collation_database | utf8_general_ci |
| Collation_server |Utf8_general_ci|
+ ---------------------- + ------------------- +
3 rows in SET (0.00 Sec)
In fact, there are several levels of garbled code: Page encoding controls display and output, IDE'sCodeEncoding controls compilation, Tomcat uriencoding controls the server encoding, and Connection Control controls the server connection to access MySQL encoding. The MySQL control level is as mentioned above: tables, databases, connections, servers, the default MySQL server is UTF-8, and the rest are the default encoding at the time of compilation.
The most convenient and trouble-free solution is to use unified encoding for all configurations. Of course, this is not foolproof. I have encountered input garbled characters in the mobile browser, which also has a lot to do with whether the client code is correct or not.