MySQL中的字符集涵义及使用方法总结(二)

来源:互联网 发布:皮尔斯季后赛数据 编辑:程序博客网 时间:2024/06/08 01:25
五.乱码的避免
最好让上述9个字符集变量值保持一致,或者至少“兼容”,同时也要考虑到OS中locale的值。
当然:character_set_system例外,它是存储和表示元信息使用的字符集,一般都是ascii串,使用utf8和使用latin1基本一样,但是,如果使用中文,可能就另当别论了。下边说的全部变量是指除了character_set_system以外的其它变量。

这里推荐三个方案:
1. 全部使用latin1
但是在java程序中,它担着一定的风险,即在入库之前,需要将字符串从gbk转换到iso8859_1,出库以后,获取结果时,再从iso8859_1转到gbk.
否则会出现乱码。
这种方式比较适合于C代码,显示依赖于操作系统的locale.一般都不用转换。

2. 全中文支持,全部设置成gbk.
方法:
    在my.ini中修改添加:(这个是必须的)
    [mysqld]
    default-character-set=gbk
    在java程序里边使用"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK"这样的url,表明使用GBK进行编码。
      
3. utf8字符集支持.
方法:
    在my.ini中修改添加:
    [mysqld]
    default-character-set=utf8   
    在java程序里边使用"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"这样的url,表明使用GBK进行编码。   
    注意utf8与UTF-8的分别.
utf8的好处是java虚拟机可以自动将它与gbk进行转换,因而显示都不会有乱码。可是在控制台下(cmd),显示就有问题了。       

六.使用java代码显示字符集变量及测试字符集的显示
因为只是作测试用,所以没加修饰。测试时,只需要按照上述三个方法修改字符集即可。
import java.sql.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * 
@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) {
      }

    }

  }

}

有什么问题,欢迎来讨论。
 
原创粉丝点击