Insert into studentinfo values ('20140901', 'Chinese encoding ');
I recently used MySQL databases, and I was very conscious of a very simple database. I didn't expect a headache for the connection: Chinese garbled characters.
Then I went to Baidu and found that there were a lot of colleagues who encountered this problem. I tried a few, but it didn't help me much. Then I thought about it for a long time and finally moved it out. I would like to share with you, so that you may not find any good solutions in the future.
First, the Internet says that you should use UTF-8 encoding when installing the SDK. I didn't try this. After all, I don't want to uninstall the installation.
I checked that the default eclipse encoding is GBK. Therefore, when I modified MySQL, it was also changed to GBK.
The procedure is as follows:
1. Open the MySQL configuration file my. ini directory and find it under the installation directory. My directory is: C: \ Program Files \ mysql \ MySQL Server 6.0
Modify the data under the [client] and [MySQL] nodes as follows. If no data is added, modify default-character-set = GBK.
2. Encoding modification for a single database
Set a database separately:
Alter database student Character Set GBK;
Command Line: Set character_set_server = GBK;
View the encoding format statement: view the encoding format: Show variables like 'character % ';
Well, create the database below. If there is garbled code before, you 'd better drop it: Drop table studentinfo;
Create Table studentinfo (ID int, name varchar (20 ))
Insert into studentinfo values ('20140901', 'Chinese encoding ');
Okay. Link MySql in eclipse to display Chinese characters.
Import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement; public class sqltest {public static void main (string [] ARGs) throws classnotfoundexception, sqlexception {sqltest q = new sqltest (); q. doconnect ();} void doconnect () throws classnotfoundexception, sqlexception {class. forname ("com. mySQL. jdb C. Driver "); // load the driver connection con = drivermanager. getconnection (" JDBC: mysql: // localhost/student? Useunicode = true & characterencoding = GBK "," root "," "); // connect to the MySQL database statement = con. createstatement (); // select different methods based on different operations. If the query operation is executed and a result set is returned, select the executequery method here, if you want to add, delete, or modify the execution, the returned number of rows will be affected. Select the executeupdate () method. Resultset rs = statement.exe cutequery ("select * From studentinfo"); system. out. println ("success"); While (RS. next () {system. out. println ("student ID:" + ":" + Rs. getstring (1) + "name" + ":" + Rs. getstring (2);} Rs. close (); con. close () ;}
Note: When the link is added with the encoding format? Useunicode = true & characterencoding = GBK
So far, the noon encoding problem has been solved ~~