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 ();