First, a StringConvert bean (GBToISO () and ISOTOGB (), solves the partial Chinese garbled problem when interacting with the mysql database: read the Chinese content of mysql in the JSP program, using these two methods Solve garbled problems.
However, both the Chinese content written to mysql from JSP has become garbled, and it is displayed as "?" When it is read, and the character information in the encoded conversion process should appear. Depressed, after logging in to MySQL in the command line window, executing the statement such as "Insert INTO CUSTOMER VALUES", writes to the Chinese content in the data table is also normal. of! ! ! The character set used by the database is UTF8.
Multiple times, finally discovered a path to solve the problem: When viewing the MySQL manual, see a such statement: To allow multiple character sets to be ready, the client, the "UTF-8" Encoding Should Be Used, Either By Configuring "UTF8" as the default server character set, or by configuring the jdbc driver to use "UTF-8" THROUGH The Characterencoding Property.
In addition, when viewing the "MySQL Authority Guide", it is found that such syntax can be converted to a given character set in the query statement: _charset STR.
Where the charSet must be a character set supported by the server. In this case, the default character set used by the Shopdb database is UTF8, so start testing:
Enter insert INTO PUBLISH VALUES ('8', _ GB2312 'Higher Education Press ") After writing Chinese becomes" ?? "
Try INSERT INTO PUBLISH VALUES ('8', _ GBK 'Higher Education Press "results
Insert Into Publish Values ('8', _ UTF8 'Higher Education Press') This is more simply, nothing! !
Crazy !! There is no way, use the show character set; command to view the character set supported by mysql, I think I have a total of a success. After browsing, I found that there are only a few familiar character sets, only one Latin1 (ISO-8859-1) is more common, it will not be it, it is sure enough.
Insert Into Publish Values ('8', _ Latin1 'Higher Education Press ") Enter Chinese can display correctly.
In this way, find the method, change the URL of the database connection pool configured under Tomcat to "... Characterencoding = UTF-8", then use the Chinese content written to the database
String S2 = New string (S1.GetBytes ("GB2312"), "ISO-8859-1") performs transcoding, where S1 is a Chinese string. Then write to the database everything is normal.
To solve this problem to view N multi-information, now a summary: the character set and the character coding method, the data is transmitted between the OS and the program (especially the data in the Multiple Character Sets), will generate garbled and characters. The loss of information. The key to solving this problem is to understand the character sets and character encoding methods used by the data output and the receiving end. If the two coding methods are different, they need to be transcoded at the data outlet or inlet. Generally, in writing code, compilation, and running during operation, it is necessary to be particularly careful. When writing code, you may use some development tool, for example I are using Eclipse. Perhaps everything is everything is normal, but once it is saved, open the document again, all Chinese characters become garbled. This is because when writing, these character data are in a stream, OK, this is no problem, but when saved, the data in this stream will be written to the hard disk, which is your development tool default. Coding method, if unfortunately, your development tool default encoding is ISO-8859-1, Chinese character information cannot be stored correctly. Eclipse can view and modify the default character encoding mode: Project-> Properties-> Info, here is "default encoding for text file". If set to GBK, write code and save this.
For JSP programs, after writing the code, they will be handed over to Container, first they will be converted to .java files, then compile into .class to submit to the server. This process also has a character encoding problem. The Java Compiler (Javac) uses the operating system language environment as the default character encoding method, the JRE (Java Runtime Environment) is also true. The Chinese characters can be displayed correctly when the character encoding method of the compilation and running environment is the same as the encoding method of the storage source file. Otherwise, you need to transfer at runtime to make them use compatible encoding. The settings here can be divided into several levels: the language supported by the system layer, which is the most important because it affects the default character encoding method of the JVM, and the display of characters, such as font, etc., J2EE server layer Most servers can customize the character encoding, such as Tomcat, can set the javaEncoding parameter setting character encoding through web.xml, default is UTF-8.
IE can also be set to always use UTF-8 encoding to send requests. Application layer, each program under the server can set your own encoding method, this I haven't used it yet, after learning.
Running transcodation, running period, application is likely to interact with external systems, such as reading and writing of the database, reading and writing external files. In these cases, the application is from data exchange with external systems. Then, for Chinese characters, the encoding method of data entrance and exit is particularly important. The general external system has its own character encoding method, and MySQL configured in my example is UTF-8 encoding. The JSP page is set by setting "charset = GB2312",
Use GB2312 encoding, you need explicit transcoding to properly process Chinese characters when it interacts with the database.