How to troubleshoot Chinese garbled characters in MySQL Databases connected to FreeBSD through JDBC

Source: Internet
Author: User

The gbk is not installed when the database is installed on FreeBSD. As a result, the mysql database backed up from the windows system cannot be imported to freebsd mysql, the result is that the gb2312 encoding method is used on windows, but mysql on freebsd does not, you can only open the backup file on Windows and modify the default character set gb2312 to utf8 to import the file to mysql of freebsd.

After the import is complete, use ECLIPSE as a small program to connect to the database in mysql using jdbc. The result is garbled.

View mysql status in freebsd system

Mysql> status;
--------------
Mysql Ver 14.7 Distrib 4.1.18, for portbld-freebsd6.1 (i386) using 5.0

Connection id: 52
Current database: schoolmis
Current user: root @ localhost
SSL: Not in use
Current pager: more
Using OUTFILE :''
Using delimiter :;
Server version: 4.1.18-Log
Protocol Version: 10
Connection: localhost via UNIX socket
Server characterset: Latin1
DB characterset: Latin1
Client characterset: Latin1
Conn. characterset: Latin1
UNIX socket:/tmp/MySQL. Sock
Uptime: 1 hour 17 min 51 sec

Threads: 2 Questions: 494 Slow queries: 0 Opens: 44 Flush tables: 1 Open tables: 19 Queries per second avg: 0.106
--------------
It turns out that MySQL uses the default encoding. If it is not adjusted, latin1 is actually the same as ISO8859_1. So when reading these characters, you need to process them; otherwise, garbled characters will be generated.

Try {
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
Connection con = DriverManager
. GetConnection ("jdbc: mysql: // 10.33.250.2/schoolmis? User = sa & password = sa & useUnicode = true & characterEncoding = utf8 ");
Statement stmt = con. createStatement ();
// String strSQL = "insert into student values ('Great Wall ')";
// Stmt.exe cuteUpdate (strSQL );
// Stmt. close ();
Stmt = con. createStatement ();
ResultSet rs = stmt.exe cuteQuery ("select * from ");
While (rs. next ()){
String str = new String (rs. getString ("studentid"). getBytes ("ISO8859_1"), "GBK ");
System. out. println (str );

}
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Another method

Of course, when MySQL is latin1 encoded, it can also be stored with GBK.
Connection con = DriverManager. getConnection ("jdbc: mysql: // 10.33.250.2: 3306/user = sa & password = sa? UseUnicode = true & characterEncoding = GBK ");
Connection con = DriverManager
. GetConnection ("jdbc: mysql: // 10.33.250.2/schoolmis? User = sa & password = sa & useUnicode = true & characterEncoding = utf8 ");
Statement stmt = con. createStatement ();
String strSQL = "insert into student values ('Great Wall ')";
Stmt.exe cuteUpdate (strSQL );
Stmt. close ();

 

 

 

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.