My way of the garbled Chinese garbled solution and summary of--jsp and MySQL interaction

Source: Internet
Author: User
Tags character set command line insert mysql mysql in mysql manual mysql database tomcat
js|mysql| Interactive | solve | chinese | Chinese garbled
First implemented a Stringconvert bean (Gbtoiso () and ISOTOGB () two methods), to solve the MySQL database when interacting with some of the Chinese garbled problem: in the JSP program read MySQL Chinese content, Using these two methods can solve the garbled problem.

But the Chinese content from JSP to MySQL has become garbled, and read it again as "?? "There should be a loss of character information in the encoding conversion process." What's depressing is that when I log in to MySQL in the Command line window, I execute the INSERT into Customervalues (' character ',...) When such a statement is written to the datasheet, the Chinese content is displayed as normal!!! The character set used by the database is UTF8.



Hit the wall several times, and finally found a way to solve the problem: when looking at the MySQL manual, see a statement like this: Toallow multiple character sets to is sent from the client, the "UTF-8" encoding Should is used, either by configuring "UTF8" as the defaultserver character set, or by configuring the JDBC driver to use "UTF-8" through the Characterencoding property.



In addition, when reviewing the MySQL authority guide, it is found that in query statements you can use such syntax to convert a string to a given character set: _charset str.

Where CharSet must be a character set supported by the server. In this case, the default character set used by the SHOPDB database is UTF8, and the test begins:

First enter insert into publish Values (' 8 ', _gb2312 ' higher Education Press ') written after Chinese into "?? ”

Try again insert into publish Values (' 8 ', _GBK ' higher Education Press ') results ibid.

INSERT into Publish Values (' 8 ', _utf8 ' higher Education Press ') This more simply, nothing!!



It's going crazy!! No way, with show character set; command to view MySQL-supported character sets, I thought I'd try it. There's always one to succeed. Browsing a bit, found that there are not a few familiar with the character set, only a latin1 (iso-8859-1) is more common, it is not it, a try is indeed.

INSERT into Publish Values (' 8 ', _latin1 ' higher Education Press ') input Chinese can be displayed correctly.



It's finally found a way to change the URL of the database connection pool configured under Tomcat to "... characterencoding=utf-8" and then use the Chinese content written to the database

String s2 = new String (S1.getbytes ("gb2312"), "Iso-8859-1") for transcoding, where S1 is the Chinese string. And then write to the database everything appears normal.



To solve this problem to see more than n data, is a summary: Because of the character set and character encoding, in the OS and between the program to pass data (especially multiple character sets in the data) will produce garbled and the loss of character information. The key to solving this problem is to understand the character set and character encoding used by the data output and receiver, and if the two encodings are different, they need to be transferred at the data exit or entrance. Generally speaking, in writing code, compile, and run the character data will be transmitted, so you need to be particularly careful.

When writing code, you might use some kind of development tool, such as the eclipse I'm using. Perhaps in writing when everything is normal, but once saved and then open the document again, all the Chinese characters have become garbled. This is because at the time of writing, these character data are in a stream of memory, OK, this is fine, but save the data in this stream will be written to the hard disk, using your development tool default encoding method, If it's unfortunate that your development tools default encoding is Iso-8859-1, the Chinese character information will not be stored correctly. In eclipse, you can view and modify the default character encoding: Project->properties->info, which has "default encoding for text file." If set to GBK, then write the code and save the pass.

For JSP programs, the code is written to container, and first they are converted to. java files, which are then compiled into. class to be submitted to the server for execution. This process also has a character encoding problem. The Java Compiler (JAVAC) uses the operating system's locale as the default character encoding, as does the JRE (Java Runtime Environment). Chinese characters can be displayed correctly only when the compilation and running environment is encoded in the same way as the storage source file. Otherwise, you need to make transcoding at run time so that they use compatible encodings. The settings here can be divided into several levels: the language that is supported by the operating system layer, which is most important, because it affects the default character encoding of the JVM and has a direct effect on the display of characters, such as fonts, and the Java server layer, where most servers can customize the character encoding, For example, Tomcat can set the character encoding by setting the javaencoding parameter in Web.xml, which is UTF-8 by default.

IE can also be set to always use UTF-8 encoding to send requests. Application layer, each program under the server can set its own code, which I have not yet used, to learn later.

At runtime, the application is likely to interact with the external system, such as reading and writing to the database, and reading and writing to the external file. In these cases, the application will inevitably have to exchange data with the external system. So for Chinese characters, the encoding of data entry is particularly important. General external systems have their own character encoding, my example of the configuration of MySQL is the use of UTF-8 encoding. JSP page by setting "charset=gb2312",

Using GB2312 encoding, you need an explicit transcoding to handle Chinese characters correctly when interacting with the database.


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.