Mysql5 inserting garbled characters

Source: Internet
Author: User

Mysql5 insertion garbled problem in the database upgraded from 4.2 to 5.1.6-comm... latin1 is followed by the default encoding. Chinese characters are garbled in previous programs. The previous method is to add charset = gb2312 to source, and then execute set names gb2312 before insertion; I have found many articles on the Internet, except for the above writing, that is, directly changing the database settings. However, this database is a product bought by someone else, so I have to write articles on the code, after tossing for a long time, come to the following conclusions to share with you, latin1 is the iso8859-1, so before inserting the need to convert the content to the iso8859-1, as follows: Encoding iso88591 = Encoding. getEncoding ("iso8859-1"); Encoding df = Encoding. default; byte [] gb2312bytes = df. getBytes (content); // byte [] asciiBytes = Encoding. convert (df, iso88591, gb2312bytes); tring str = iso88591.GetString (gb2312bytes); after this conversion, the charset parameter is not added to the source, and the set names ** is not required in the program **, the data can be inserted normally.

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.