Chinese garbled code solution for JSP and MySQL interaction and its summary

Source: Internet
Author: User
First, a StringConvert bean (GBtoISO () and ISOtoGB () methods are implemented to solve some Chinese garbled characters during interaction with the MySQL database: read MySQL Chinese content in the JSP program. These two methods can solve the garbled problem.
However, the Chinese content written from JSP to MySQL is garbled and displayed as "?" again. Here, character information is lost during encoding and conversion. Sadly, after logging on to MySQL in the command line window, execute insert into customer VALUES ('Character ',...) when such a statement is written to the data table, the Chinese content is displayed normally !!! The character set used by the database is utf8.

After hitting the wall multiple times, I finally found a solution: When I checked the MySQL manual, I saw a statement like this: To allow multiple character sets to be sent from the client, the "UTF-8" encoding shoshould be used, either by grouping "utf8" as the default server character set, or by locking ing the JDBC driver to use "UTF-8" through the characterEncoding property.

In addition, you can use this syntax in the query statement to convert a string to a given character set: _ charset str.
Charset must be a character set supported by the server. In this example, the default character set used by the shopdb database is utf8, so the test is started:
Enter insert into publish Values ('8', _ gb2312 'Higher Education Press') to write the data INTO "?"
Try inserting INTO publish Values ('8', _ gbk 'Higher Education Press').
Insert into publish Values ('8', _ utf8' Higher Education Press '). This is even more simple and there is nothing !!

Crazy !! No way. Use the show character set; command to view the character sets supported by MySQL. I thought I would try it all again. Browse a bit, found that there are not a few familiar character set, there is only one latin1 (ISO-8859-1) is more common, it won't be it, a try really is.
Insert into publish Values ('8', _ latin1 'Higher Education Publishers.

This finally found the method, the Tomcat configuration of the database connection pool url to "... characterEncoding = UTF-8", and then write the Chinese content written to the database
String s2 = new String (s1.getBytes ("gb2312"), "ISO-8859-1") for transcoding, where s1 is a Chinese String. Then write to the database and everything is normal.

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.