From the beginning to knock DRP to now hibernate, Chinese data garbled all the time "accompany" in My side:
Figure one: In the DRP distributor module, each time the modification area is successful, but the Chinese data read every time is "??? "String, but the numbers and letters of data are not affected.
Figure II: Whether it is a DRP or hibernate project, inserting the Chinese data by executing SQL or HQL statements will render the image in the picture, very silent ...
But it has been bothering me for so long, I also counted "illness into medicine", today together to the Java project to heal the Chinese data garbled this big disease.
There is a philosophical sentence of "piecemeal", but this is a wrong point of view. Because headache is not necessarily only the head of the problem, the insertion of data garbled is the same reason, so we can not simply put the problem to MySQL, or blame the Java program. The following people and I embarked on the exploration of "Data garbled" journey:
? First stop: The Chinese character set for the development environment :
In the development process of the DRP, in the Distributor maintenance module, each time the Distributor information is modified, the data can be inserted perfectly, but the Chinese in the database is "??? "Such a string causes the data read out to be garbled as well." At this point we have to do a step-by-step troubleshooting, first start from the development environment, open the window in MyEclipse, do the following:
This will ensure that our development environment for GB18030;[PS:GB18030: National standards, information Technology Chinese coded character set ]
? Second station: match the character set settings of the NAVICAT and development environment
In the above diagram, we set the Chinese coded character set for the development environment, which may solve some problems, but it still doesn't solve my problem. So I'm going to continue to explore!
Using the Navicat software, open the table of the database you want to use, right-click-Select Design table:
Select the field for which you want to enter Chinese data, and set its properties as follows:
This step will be completed, it may be your solution, but not mine; Cold, continue to go forward:
? third stop: Get in "Method"
We often set the method of one of the methods, where the use of post in the transfer of data to avoid the get way to bring the garbled.
Fourth station: "Constraints" are missing from the configuration file that connects to the database(There are two types of hibernate.cfg.xml and user.properties files)
After several stops in the desert, I finally saw a clear spring: By making a few changes to the configuration file, you can solve my immediate pressing:
Method One : Use the. xml file to configure the database:
<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" >
The Connection.url line only writes the address of the database and the corresponding database name, but does not configure the Chinese character set, so make the following modifications to this line of code:
<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" ><property name= "Hibernate.connection.url" >jdbc:mysql://localhost:3306/hibernate_extends?useunicode= True&characterencoding=utf-8</property></span>
we can notice that the change is the code that follows the database name to set the Chinese character set, but here are a few points to note :
1. Do not let the Chinese question mark "Fish Dragon mix Bead"The question mark after the database name must be in the English state, if it is the Chinese state, then in the execution of this configuration file, the Chinese state question mark will be myeclipse translated into three question marks, will issue a "configuration statement error" prompt;
2. None of them can be less.Useunicode=true followed by must be "&", the five elements of one can not be less, but in general, directly in the Web page to copy the five elements into the configuration file, will only display a "&": So the console will print: " Encoding should end with a semicolon, this error is not to add a semicolon at the end, but to note that the five elements of the "one can not be less!" ”
Mode two : Configure using the. properties File:
<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" >jdbc.driver = Com.mysql.jdbc.Driverjdbc.url = Jdbc:mysql:///shopjdbc.user = Rootjdbc.password =123</span>
When this situation is garbled, the following changes are made to Jdbc.url:
<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" >jdbc.url = jdbc:mysql:///shop?useunicode=true&characterencoding=utf-8</span>
In this way, we have completed each link of the Chinese character set settings, but also to solve the Java program "Chinese garbled problem" this big disadvantage. Here, this trip to the desert is over, to solve the mistake is not the purpose, the purpose is to solve the error in the process of analysis and thinking, not to be deceived by appearances, and then do not make "piecemeal" error, that is, teachers often give us the overall view----"Do not seek the overall situation of the lack of a corner, He who does not seek a man of the ages is short! ”
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Clumsy birds have java--mysql Chinese data garbled for what???