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

本文介绍如何在MySQL中正确配置字符集以避免乱码问题,并提供了三种配置方案。同时,通过Java代码示例展示了不同字符集设置下的数据读写效果。
五.乱码的避免
最好让上述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代码显示字符集变量及测试字符集的显示
因为只是作测试用,所以没加修饰。测试时,只需要按照上述三个方法修改字符集即可。
importjava.sql.*;

/***//**
*<p>Title:</p>
*
*<p>Description:</p>
*
*<p>Copyright:Copyright(c)2006</p>
*
*<p>Company:</p>
*
*
@authornotattributable
*
@version1.0
*/

publicclassTestCharset...{
Stringusername
="root";
Stringpasswd
="";
Connectionconn
=null;
Stringcharset
=null;
publicTestCharset()...{
}


publicvoidconnect()throwsSQLException,ClassNotFoundException...{
Class.forName(
"com.mysql.jdbc.Driver");
Stringurl
="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
conn
=DriverManager.getConnection(url,username,passwd);
charset
=url.substring(url.lastIndexOf("=")+1);
}


publicvoidgetCharset()throwsSQLException...{
Statementstmt
=conn.createStatement();
System.out.println(
"=======showvariableslike'chara%'========");
ResultSetrset
=stmt.executeQuery("showvariableslike'chara%'");
while(rset.next())...{
System.out.println(rset.getString(
1)+"------>"+rset.getString(2));
}

rset.close();
System.out.println(
"=======showvariableslike'collation%'========");
rset
=stmt.executeQuery("showvariableslike'collation%'");
while(rset.next())...{
System.out.println(rset.getString(
1)+"------>"+rset.getString(2));
}

rset.close();
stmt.close();
}


publicvoidtestGetValuesISO8859_1()throwsException...{
Statementstmt
=conn.createStatement();
try...{
stmt.executeUpdate(
"droptablet12345");
}
catch(Exceptione)...{

}

stmt.executeUpdate(
"createtablet12345(idintprimarykey,namevarchar(32))");
Stringsz
=newString("中文".getBytes("gbk"),"ISO8859_1");
stmt.executeUpdate(
"insertintot12345values(1,'"+sz+"')");
ResultSetrset
=stmt.executeQuery("select*fromt12345");
rset.next();
System.out.println(
"测试中文值:"+newString(rset.getString(2).getBytes("ISO8859_1"),"GBK"));
rset.close();

stmt.close();
}

publicvoidtestGetValuesGBK()throwsException...{
Statementstmt
=conn.createStatement();
try...{
stmt.executeUpdate(
"droptablet12345");
}
catch(Exceptione)...{

}

stmt.executeUpdate(
"createtablet12345(idintprimarykey,namevarchar(32))");
stmt.executeUpdate(
"insertintot12345values(1,'中文')");
ResultSetrset
=stmt.executeQuery("select*fromt12345");
rset.next();
System.out.println(
"测试中文值:"+rset.getString(2));
rset.close();

stmt.close();
}

publicvoidtestGetValuesUTF8()throwsException...{
Statementstmt
=conn.createStatement();
try...{
stmt.executeUpdate(
"droptablet12345");
}
catch(Exceptione)...{

}

stmt.executeUpdate(
"createtablet12345(idintprimarykey,namevarchar(32))");
//Stringsz=newString("中文".getBytes("gbk"),"UTF8");
stmt.executeUpdate("insertintot12345values(1,'中文')");
ResultSetrset
=stmt.executeQuery("select*fromt12345");
rset.next();
System.out.println(
"测试中文值:"+rset.getString(2));
rset.close();

stmt.close();
}

publicvoiddisconnect()throwsSQLException...{
if(conn!=null)conn.close();
}

publicstaticvoidmain(String[]args)...{
TestCharsett
=newTestCharset();
try...{
t.connect();
t.getCharset();
if(t.charset.equals("ISO8859_1"))
t.testGetValuesISO8859_1();
elseif(t.charset.equals("GBK"))
t.testGetValuesGBK();
elseif(t.charset.equals("UTF-8"))
t.testGetValuesUTF8();
}
catch(Exceptione)...{
//System.out.println(e.getMessage());
e.printStackTrace();
}
finally...{
try...{
t.disconnect();
}
catch(Exceptione2)...{
}

}

}

}

有什么问题,欢迎来讨论。
<script type="text/javascript"><!-- google_ad_client = "ca-pub-7104628658411459"; /* wide1 */ google_ad_slot = "8564482570"; google_ad_width = 728; google_ad_height = 90; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值