花了大半天,終于解決了MySql5.0.12的中文亂碼問題.
(我用的是:MySql5.0.12 和 mysql-connector-java-3.1.10-bin.jar)
把基本代碼列出來(注意粗體藍色部分):
1.在mysql中建表:
mysql> create table stud(
-> name varchar(20) not null,
-> password varchar(20) not null)
-> TYPE=MyISAM,
-> default character set gb2312;
2.java代碼:
......
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=lk&useUnicode=true&characterEncoding=gb2312");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "insert into stud(name,password) values(‘張三‘,‘a(chǎn)bcd‘)";
stmt.executeUpdate(sql);
sql = "select * from stud";
ResultSet rst = stmt.executeQuery(sql);
while (rst.next()) {
System.out.println(rst.getString("name"));
}
}
......
3,登陸MySql后,用"select * from stud"來查看記錄,如果發(fā)現(xiàn)還是亂碼的話,應(yīng)該這樣登陸MySql:
mysql --default-character-set=gb2312 -u root -p
在console里就能正常顯示stud表的中文了.
mysql> select * from stud; +------+----------+ | name | password | +------+----------+ | 張三 | abcd | +------+----------+ 1 row in set (0.00 sec) |
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=487457