Java, tomcat, and MySQL garbled Summary

Source: Internet
Author: User

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.

 

 

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.