The processing of Java and MySQL Chinese problems

Source: Internet
Author: User
Tags character set command line ini insert mysql query mysql database
mysql| Problem | Chinese problem: insert with JDBC, read the database kind of text string garbled.








first of all, everything in the MySQL database is binary storage, supporting any data, of course, including Chinese. You go to the command line


INSERT into testtable values (' Chinese ');


select * from TestTable;


all appear normal.


However, although access to Chinese is no problem, there are problems with sorting and matching. So if you have Chinese in your database, remember to add a row in the configuration file, such as [mysqld] in C:winntmy.ini:


DEFAULT-CHARACTER-SET=GBK


then restart the MySQL server. Note GBK to lowercase, or mysqld can not start.








second, the database is fine, look at the Java program below. It's boring to add a sentence to a program. Debug statement:


out.println ("Chinese");


is also displayed correctly, indicating that the entire Java environment is fine.








So, of course, is the part of the Java and MySQL connection, MySQL jdbc driver problem.


analysis, Java internal use of Unicode, and MySQL defaults to use iso-8xxx (forgotten), so JDBC driver to the query string to the MySQL server, will do unicode->iso-8xxx conversion, When you accept the results from MySQL server, you do a iso-8xxx->unicode conversion. (It will UNICODE->GBK when the results are displayed on the screen, but it's not about here.) )


That's a problem. The Chinese string for inserting the database under the command line is GBK (this is the default for Simplified Chinese windows), so the JDBC driver accept the query result, it should do gbk->unicode conversion.


Verify that the Chinese text string s, read from the database,


New String (S.getbyte ("iso-8xxx"), "GBK")


first to make a unicode->iso-8xxx into its original image stored in the database. We know that it is GBK, so hand-gbk->unicode, so that the Java program is normal.


similar, write to the database, we look forward to the JDBC driver will UNICODE->GBK, the result is unicode->iso-8xxx, of course, is garbled.








has a lot of articles, so far, and told us: to solve the Chinese problem, hand-transcoding it yourself.


this is really irresponsible. If each string has to be manually transcoding, there is a problem with the program design.


think that the guy who writes MySQL JDBC driver won't even know the transcoding?


so I look at the connector-j-3.0.7 inside the Readme and find a solution:


connection = drivermanager.getconnection ("Jdbc:mysql://localhost/test?user=root&password=&useunicode =TRUE&CHARACTERENCODING=GBK ");


This is to tell the JDBC driver to force the specified parameter to be transferred to the code








Actually, there are still problems. If MySQL server must use ISO-8XXX, then only one way to do it. But I remember my mysql is GBK, not all changed My.ini? How does JDBC driver automatically detect MySQL server's character set?


this time to see the benefits of open source:-) connector-j-3.0.7 source code does read MySQL server information, including character sets. As you know from the notes,


authors write their own conversion functions for Unicode conversion to Single-byte character sets, and claim to be much faster than the JVM. So there's a paragraph in the code that calls its own conversion function if the database is using a single byte. But after this code forgot to give the multibyte character set to the JVM to convert, so it becomes the default iso-8xxx conversion.


I modify the way: Commysqljdbcconnecter.java This file 1969 lines,


This.dounicode = true; Force the issue


moves the top four lines to 1964 lines before the following line:


try {


Use this code to rewrite the JDBC driver, your Java access to the database program without making any changes to read and write Chinese, but remember that MySQL server to DEFAULT-CHARACTER-SET=GBK


I use it to test a few small programs, Chinese are displayed normal, and there is no panic, abnormal. Hehe, I feel good about myself.








Related Article

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.