Solve the JDBC Chinese problem of MySQL in Linux

Source: Internet
Author: User
In addition to the well-known Chinese problems, mysql instances on Linux may encounter this problem: You use the mysql5x version and configure the database to use the UTF-8 character set, you

In addition to the well-known Chinese problems, mysql instances on Linux may encounter this problem: You use the mysql5x version and configure the database to use the UTF-8 character set, you

In addition to the well-known Chinese characters, mysql instances on Linux may encounter the following problems:

You use mysql5x and configure the database to use the UTF-8 character set, you use the linux Standard jdk environment, you use a high enough mysql jdbc driver, you use the UTF-8 character set as the basic character set for J2EE. You use a standard CharacterEncodingFilter to complete Transparent Character Set conversion.

In this way, in most cases, you have not felt the interference of Chinese characters on Java applications. However, when saving a form, you find that Chinese characters are saved as garbled characters or question marks.

The reason is that this is a known BUG of the mysql driver. I failed to find (not looking for) the original BUG report, but it means, jdbc driver may not be able to correctly identify the dash character in the jdbc url.

If you specify the default collation type to the UTF-8 for the test database (for example, alter database test character set utf8 ;), reading data from the database can automatically adopt the correct decoding sequence. Therefore, your Java application looks so normal under the "read-only" status, this issue is not exposed until you try to submit the form.

The solution is to replace "UTF-8" in the URL with "UTF8" to avoid the above traps.

In addition, the jdbc url separates parameters by the "&" character instead of the "&" character. If you write "&" in the URL, the following parameters cannot be correctly applied to the driver layer. This problem is complicated because you may need to escape the code when writing URLs in different locations:

1. when writing a URL in an xml file, add & Because xml considers & as the escape character, so when you write user = root & password = xxx to xml, it is equivalent to Writing user = root & password = xxx in java source code.

2. When writing a URL in the properties file, & there is no escape meaning. Therefore, the URL format is basically the same as that in the Java source code. The so-called basic consistency involves another special character ":". In properties, it is optional to use the "\:" escape sequence to represent a ":", that is, many times, you can mix these two formats.

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.