Treatment of Java and MySQL Chinese

xiaoxiao2021-03-06  112

Treatment of Java and MySQL Chinese

Question: Insert the database species text string with JDBC.

First, things in the mysql database are binary storage, support any data, of course, including Chinese. You go to the command line

INSERT INTO TESTTABLE VALUES ('Chinese);

Select * from testtable;

All are displayed normally.

However, although there is no problem in accessing Chinese, there is a problem when sorting and matching. So if there is Chinese in your database, remember to add a line in [mysqld] in the configuration file, such as [mysqld] in WinNTmy.ini:

Default-character-set = GBK

Then restart MySQL Server. Note that GBK should be lowercase, otherwise MySQLD can't start.

Second, the database is fine, look at the Java program. Add a debug statement that is bored in the program:

OUT.PRINTLN ("Chinese");

It also shows normal, indicating that the entire Java environment is no problem.

So, of course, in contact with Java and MySQL, MySQL JDBC Driver has a problem.

Analysis, Java uses Unicode, and MySQL defaults use ISO-8XXX (forgot), and JDBC Driver transmits the query string to MySQL Server, will do Unicode-> ISO-8XXX conversions, from MySQL Server Accept results When you do ISO-8xxx-> Unicode conversion. (Unicode-> GBK is displayed when the result is displayed on the screen, but it doesn't matter.)

This has a problem, I am inserting a database in the command line is GBK (this is the default of Simplified Chinese Windows), so the JDBC Driver accepts the result of GBK-> Unicode when the Query results are accepted.

Verify that the Chinese string S read from the database,

New String (S.GetByte ("ISO-8XXX"), "GBK")

Make a Unicode-> ISO-8XXX to the original look that is stored in the database. We know that it is GBK, so manually comes to GBK-> Unicode, so the Java program is explicitly normally.

Similarly, when writing to the database, we look forward to JDBC Driver will put unicode-> GBK, the result is unicode-> ISO-8xxx, of course, is garbled.

There are a lot of articles, let's go, and tell us: To solve Chinese problems, you can handle it.

This is really irresponsible. If each string must be handlered, the program is designed.

I want to think about it, I don't know if the guy who writes mySQL JDBC Driver will even do it.

So I see the Readme inside Connector-J-3.0.7, find a solution:

Connection = drivermanager.getConnection ("JDBC: mysql: // localhost / test? user = root & password = & useful = true & characterencoding = GBK");

This is to tell JDBC Driver to force it to transfer in specified parameters. If Mysql Server must use ISO-8xxx, it only uses the previous method. But I remember my mysql is GBK, I didn't change my.ini? How does JDBC Driver automatically detect the character set of mysql server?

At this time, I saw the benefits of open source :-) Connector-J-3.0.7 Source code does have code read the information of MySQL Server, including character sets. Know it from the comment,

The author converts its own conversion function for Unicode to the single-byte character set, and knowing much less than JVM. So there is a paragraph in the code, and the database is used to call your own conversion function if you are using a single byte. But after this code, I forgot to hand over the multi-byte character set to the JVM, so it became the default ISO-8xxx conversion.

My modified way: commysqljdbcconnecter.java 1969 lines of this file,

this.dounicode = true; // force the issue

Move it all over the line to 1964, put it in front of the line below:

Try {

The JDBC Driver that re-emits this code. Your Java Access Database can read and write Chinese correctly, but remember Mysql Server to default-character-set = GBK

I use it to test a few small programs, and Chinese have normal display, and they are not dead, and they are abnormal. Oh, I feel very good.

转载请注明原文地址:https://www.9cbs.com/read-102901.html

New Post(0)