iihero@ChinaUnix, ehero.[iihero] 数据库技术的痴迷爱好者. 您可以通过iihero AT qq.com联系到我 以下是我的三本图书: Sybase ASE in Action, Oracle Spatial及OCI高级编程, Java2网络协议内幕
分类: Mysql/postgreSQL
2013-07-17 14:25:49
import java.sql.*;
/**
* Title:
*
* Description:
*
* Copyright: Copyright (c) 2006
*
* Company:
*
* @author not attributable
* @version 1.0
*/
public class TestCharset {
String username = "root";
String passwd = "";
Connection conn = null;
String charset = null;
public TestCharset() {
}
public void connect() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
conn = DriverManager.getConnection(url, username, passwd);
charset = url.substring(url.lastIndexOf("=")+1);
}
public void getCharset() throws SQLException{
Statement stmt = conn.createStatement();
System.out.println("=======show variables like 'chara%'========");
ResultSet rset = stmt.executeQuery("show variables like 'chara%'");
while (rset.next()) {
System.out.println(rset.getString(1) + " ------> " + rset.getString(2));
}
rset.close();
System.out.println("=======show variables like 'collation%'========");
rset = stmt.executeQuery("show variables like 'collation%'");
while (rset.next()) {
System.out.println(rset.getString(1) + " ------> " + rset.getString(2));
}
rset.close();
stmt.close();
}
public void testGetValuesISO8859_1() throws Exception {
Statement stmt = conn.createStatement();
try {
stmt.executeUpdate("drop table t12345");
} catch (Exception e) {
}
stmt.executeUpdate("create table t12345(id int primary key, name varchar(32))");
String sz = new String("中文".getBytes("gbk"), "ISO8859_1");
stmt.executeUpdate("insert into t12345 values(1, '" + sz + "')");
ResultSet rset = stmt.executeQuery("select * from t12345");
rset.next();
System.out.println("测试中文值: " + new String(rset.getString(2).getBytes("ISO8859_1"), "GBK"));
rset.close();
stmt.close();
}
public void testGetValuesGBK() throws Exception {
Statement stmt = conn.createStatement();
try {
stmt.executeUpdate("drop table t12345");
} catch (Exception e) {
}
stmt.executeUpdate("create table t12345(id int primary key, name varchar(32))");
stmt.executeUpdate("insert into t12345 values(1, '中文')");
ResultSet rset = stmt.executeQuery("select * from t12345");
rset.next();
System.out.println("测试中文值: " + rset.getString(2));
rset.close();
stmt.close();
}
public void testGetValuesUTF8() throws Exception {
Statement stmt = conn.createStatement();
try {
stmt.executeUpdate("drop table t12345");
} catch (Exception e) {
}
stmt.executeUpdate("create table t12345(id int primary key, name varchar(32))");
//String sz = new String("中文".getBytes("gbk"), "UTF8");
stmt.executeUpdate("insert into t12345 values(1, '中文')");
ResultSet rset = stmt.executeQuery("select * from t12345");
rset.next();
System.out.println("测试中文值: " + rset.getString(2));
rset.close();
stmt.close();
}
public void disconnect() throws SQLException{
if (conn != null) conn.close();
}
public static void main(String[] args) {
TestCharset t = new TestCharset();
try {
t.connect();
t.getCharset();
if (t.charset.equals( "ISO8859_1" ))
t.testGetValuesISO8859_1();
else if (t.charset.equals("GBK"))
t.testGetValuesGBK();
else if (t.charset.equals("UTF-8"))
t.testGetValuesUTF8();
} catch (Exception e) {
//System.out.println(e.getMessage());
e.printStackTrace();
} finally {
try {
t.disconnect();
} catch (Exception e2) {
}
}
}
}