Solve the problem of Chinese garbled when JDBC connects MySQL

Source: Internet
Author: User
Tags mysql command line

The use of JDBC to the MySQL database inserted in Chinese when garbled, strictly speaking through Hibernate. Record the solutions found after searching and checking documents.

    • The first thing to tell the database is the character set used by the string to be inserted, the default character set used by MySQL is latin1. The string I want to save is UTF-8 encoded (character set is Unicode), so the table containing this field should be encoded using UTF-8. There are several workarounds.

      1. Specifies the character set encoding of the database when the database is established, so that all tables in the database will use the database's character set encoding by default. Such ascreate database foo charset utf8;
      2. Specifies the character set encoding when the table is under construction. Such ascreate table foo (id char(20)) charset utf8;
      3. Specifies the character set encoding used by a column. Such ascreate table foo (id char(20) charset utf8);

      If you want, you can also specify the rules for character ordering, that is, specifying collation, such as create database foo charset utf8 collate utf8_general_ci; , you can also specify a separate table, the collation rule used by the column.

    • Then, when using JDBC to connect to the database, tell JDBC what character set encoding to use to communicate with the server. Simple, just make a little change when JDBC specifies the database path. For example, jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf8 . Note If the "&" in the XML file is changed to "&".

If you're using GBK encoding, change all of the above mentioned UTF8 to GBK should be, as long as the server and client use the character set encoding unified.

The default character set used by the MySQL command line client is latin1, and if you use this to insert Chinese, it will also appear garbled. The workaround is to execute the statement set names ‘utf8‘ to tell the server to use UTF-8 encoding to communicate with the client. You can also use set charset ‘utf8‘ , it differs from set names only on collation. Set names and set CharSet are equivalent to executing three statements, the specific content can be seen in the MySQL document 10.4 section. I think this method is also possible when using JDBC, so if the specified database address in JDBC does not tell the character set encoding used, the same effect can be achieved by executing the above statement.

Let's talk about my environment, Windows xp,mysql5.0 installation, rails1.2.3.

There are 3 types of solutions that have been tried:

1. Re-reconfigure the instance with the MySQL Server Instance Config Wizard Wizard. Encode to UTF8

2. Modify the My.ini file under the MySQL folder. As follows:

[MySQL]

Default-character-set=utf8

[Mysqld]
Default-character-set=utf8

3. SET character_set_client =utf8;
SET character_set_results = UTF8;
SET character_set_connection = UTF8;

You can enter mysql>show VARIABLES like '%character% '; command to see the effect.

The above 2 kinds of way is actually a principle, see how you used to change it, and set the value is in the Character_set_database and Character_set_server.

These are online a search a large-style processing, here also do a summary. But I went through the above way to do, not only garbled the problem is not resolved, and the database table can not be inserted in Chinese.

Solve the problem of Chinese garbled when JDBC connects MySQL

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.