Treatment of Java and MySQL Chinese

zhaozj2021-02-16  48

Finally, close-up an article to commemorate :-) My environment: Chinese Simplified WIN2000 PRO SP3 MYSQL Server 4.0.12 J2SDK 1.4.01 ConnetCor-J 3.0.7 Question: Inserted with JDBC, read database species text String complicated. 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 display normal. 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: 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. In the program, it is very bored to add a debugging statement: Out.println ("Chinese"); 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 on the screen, but it doesn't matter here.) This has a problem, I am inserting a database in the command line into the database is GBK (this is the default of Simplified Chinese Windows), So JDBC Driver accepts the conversion of GBK-> Unicode when accepting query results. Verify that the Chinese string S, New String (S.GetByte ("ISO-8XXX"), "GBK"), "GBK"), "GBK"), "GBK"), "GBK") Original appearance. 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.

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

New Post(0)