首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 冲浪宝典 > 网络资源 > MySQL中的字符集涵义及使用方法总结(二)
【标  题】:MySQL中的字符集涵义及使用方法总结(二)
【关键字】:MySQL
【来  源】:http://blog.csdn.net/iihero/archive/2006/09/20/1250254.aspx

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

五.乱码的避免
最好让上述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) {
      }
    }
  }
}
有什么问题,欢迎来讨论。
 
成功案例-BOSCH高速包装机器人:【上一篇】
Nebula2探秘03-Object System研究:【下一篇】
【相关文章】
  • 在AJAX开发中集成数据库(mysql)技术
  • mysql对XML的支持----准翻译
  • Mysql学习总结
  • 用源码包安装php-4.4.0+mysql-4.1.9+apache-2.0.54.txt
  • MySQL中的字符集涵义及使用方法总结(一)
  • Apache+Mysql+PHP(含GD,libpng,jpeg,zlib,freetype)
  • MySQL 5.0新特性教程 存储过程
  • mysql的FAQ
  • MYSQL的字符集
  • MYSQL做同步实验
  • 【随机文章】
  • JavaScript 数组
  • [shell] expect的一些例子
  • 我知道AWT/Swing不受欢迎的根本原因了:外观不够漂亮!附图
  • 极品QQ面板-Nokia3650
  • CorelDRAW 编辑轮廓线
  • SELECT列表框提示
  • Matlab 可视化数学分析界面
  • 思科实习生面试的体会
  • linux下手动安装postgresql
  • Subversion 十分钟上手
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.