技术开发 频道

小议MySQL中的字符集处理

  六.使用java代码显示字符集变量及测试字符集的显示

  因为只是作测试用,所以没加修饰。测试时,只需要按照上述三个方法修改字符集即可。 

  1 import java.sql.*;
  2
  3 /** *//**
  4 *
  5 Title:
  6
  7 *
  8 *
  9 Description:
10
11 *
12 *
13 Copyright: Copyright (c) 2006
14
15 *
16 *
17 Company:
18
19 *
20 * @author not attributable
21 * @version 1.0
22 */
23 public class TestCharset ...{
24   String username = "root";
25   String passwd = "";
26   Connection conn = null;
27   String charset = null;
28   public TestCharset() ...{
29   }
30
31   public void connect() throws SQLException, ClassNotFoundException ...{
32     Class.forName("com.mysql.jdbc.Driver");
33     String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
34     conn = DriverManager.getConnection(url, username, passwd);
35     charset = url.substring(url.lastIndexOf("=")+1);
36   }
37
38   public void getCharset() throws SQLException...{
39     Statement stmt = conn.createStatement();
40     System.out.println("=======show variables like 'chara%'========");
41     ResultSet rset = stmt.executeQuery("show variables like 'chara%'");
42     while (rset.next()) ...{
43       System.out.println(rset.getString(1) + " ------> " + rset.getString(2));
44     }
45     rset.close();
46     System.out.println("=======show variables like 'collation%'========");
47     rset = stmt.executeQuery("show variables like 'collation%'");
48     while (rset.next()) ...{
49       System.out.println(rset.getString(1) + " ------> " + rset.getString(2));
50     }
51     rset.close();  
52     stmt.close();
53   }
54
55   public void testGetValuesISO8859_1() throws Exception  ...{
56     Statement stmt = conn.createStatement();
57     try ...{
58       stmt.executeUpdate("drop table t12345");
59     } catch (Exception e) ...{
60     
61     }
62     stmt.executeUpdate("create table t12345(id int primary key, name varchar(32))");
63     String sz = new String("中文".getBytes("gbk"), "ISO8859_1");
64     stmt.executeUpdate("insert into t12345 values(1, '" + sz + "')");
65     ResultSet rset = stmt.executeQuery("select * from t12345");
66     rset.next();
67     System.out.println("测试中文值: " + new String(rset.getString(2).getBytes("ISO8859_1"), "GBK"));
68     rset.close();
69   
70     stmt.close();
71   }
72   public void testGetValuesGBK() throws Exception  ...{
73     Statement stmt = conn.createStatement();
74     try ...{
75       stmt.executeUpdate("drop table t12345");
76     } catch (Exception e) ...{
77
78     }
79     stmt.executeUpdate("create table t12345(id int primary key, name varchar(32))");
80     stmt.executeUpdate("insert into t12345 values(1, '中文')");
81     ResultSet rset = stmt.executeQuery("select * from t12345");
82     rset.next();
83     System.out.println("测试中文值: " + rset.getString(2));
84     rset.close();
85
86     stmt.close();
87   }
88   public void testGetValuesUTF8() throws Exception  ...{
89      Statement stmt = conn.createStatement();
90      try ...{
91        stmt.executeUpdate("drop table t12345");
92      } catch (Exception e) ...{
93
94      }
95      stmt.executeUpdate("create table t12345(id int primary key, name varchar(32))");
96      //String sz = new String("中文".getBytes("gbk"), "UTF8");
97      stmt.executeUpdate("insert into t12345 values(1, '中文')");
98      ResultSet rset = stmt.executeQuery("select * from t12345");
99      rset.next();
100      System.out.println("测试中文值: " + rset.getString(2));
101      rset.close();
102
103      stmt.close();
104   }
105   public void disconnect() throws SQLException...{
106     if (conn != null) conn.close();
107   }
108   public static void main(String[] args) ...{
109     TestCharset t = new TestCharset();
110     try ...{
111       t.connect();
112       t.getCharset();
113       if (t.charset.equals( "ISO8859_1" ))
114         t.testGetValuesISO8859_1();
115       else if (t.charset.equals("GBK"))
116         t.testGetValuesGBK();
117       else if (t.charset.equals("UTF-8"))
118         t.testGetValuesUTF8();
119     } catch (Exception e) ...{
120       //System.out.println(e.getMessage());
121       e.printStackTrace();
122     } finally ...{
123       try ...{
124         t.disconnect();
125       } catch (Exception e2) ...{
126       }
127     }
128   }
129 }
0
相关文章