Java Insert Database garbled solution
1. Situation when the problem occurs;
Using Navicat for Mysql, the tables created are:
| room_id |
varchar |
| Room_name |
Char |
| Room_info |
varchar |
Where the character set is used: UTF8 (optional in navicat)
Java accesses a database in the following ways:
<span style= "FONT-SIZE:14PX;" >public Connection getconnection () {//Set the return value to Connection method
try {//Load Database driver class
Class.forName (" Com.mysql.jdbc.Driver ");
SYSTEM.OUT.PRINTLN ("Database-driven load succeeded");
} catch (ClassNotFoundException e) {
e.printstacktrace ();
}
try {//Get the database connection object by accessing the URL of the database
con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mydatabase", "root" , "123123");
SYSTEM.OUT.PRINTLN ("Database connection succeeded");
} catch (SQLException e) {
e.printstacktrace ();
}
return con; Returns a connection object as required by method
}</span></span>
Java gets the information from the Web page is normal, print out the running statement is:
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >insert into Table_room (room_id, Room_name, Room_info) VALUES ("H1", "good", "very good")
</span></span>
However, the use of navicat for MySQL to see the results of the operation found that the inserted Chinese characters are problematic, English normal, Chinese have become ...
2. Solution to the problem
Invalid Workaround:
To modify the encoding in Navicat, I modified the character set to GB2312 but it didn't work.
the right way to solve
The linked database is an error in the way the URL was filled out, which should be:
con = drivermanager.getconnection ("jdbc:mysql://localhost:3306/mydatabase?useunicode=true&characterencoding= UTF8 "," root "," 123123 ");
Note the following additions to the
? Useunicode=true&characterencoding=utf8
If the above method is unsuccessful, the following recommendations refer to:
1. Change UTF8 to UTF-8
2. This writing
? useunicode=true&characterencoding=utf-8
Reference: www.cnblogs.com/amboyna/archive/2008/06/18/1224570.html