MySQL中的字符集涵义及使用方法总结(二)
生活随笔
收集整理的這篇文章主要介紹了
MySQL中的字符集涵义及使用方法总结(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
五.亂碼的避免
最好讓上述9個(gè)字符集變量值保持一致,或者至少“兼容”,同時(shí)也要考慮到OS中l(wèi)ocale的值。
當(dāng)然:character_set_system例外,它是存儲和表示元信息使用的字符集,一般都是ascii串,使用utf8和使用latin1基本一樣,但是,如果使用中文,可能就另當(dāng)別論了。下邊說的全部變量是指除了character_set_system以外的其它變量。
這里推薦三個(gè)方案:
1. 全部使用latin1
但是在java程序中,它擔(dān)著一定的風(fēng)險(xiǎn),即在入庫之前,需要將字符串從gbk轉(zhuǎn)換到iso8859_1,出庫以后,獲取結(jié)果時(shí),再從iso8859_1轉(zhuǎn)到gbk.
否則會出現(xiàn)亂碼。
這種方式比較適合于C代碼,顯示依賴于操作系統(tǒng)的locale.一般都不用轉(zhuǎn)換。
2. 全中文支持,全部設(shè)置成gbk.
方法:
??? 在my.ini中修改添加:(這個(gè)是必須的)
??? [mysqld]
??? default-character-set=gbk
??? 在java程序里邊使用"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK"這樣的url,表明使用GBK進(jìn)行編碼。
? ? ??
3. utf8字符集支持.
方法:
??? 在my.ini中修改添加:
??? [mysqld]
??? default-character-set=utf8???
??? 在java程序里邊使用"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"這樣的url,表明使用GBK進(jìn)行編碼。???
??? 注意utf8與UTF-8的分別.
utf8的好處是java虛擬機(jī)可以自動將它與gbk進(jìn)行轉(zhuǎn)換,因而顯示都不會有亂碼。可是在控制臺下(cmd),顯示就有問題了。??? ???
六.使用java代碼顯示字符集變量及測試字符集的顯示
因?yàn)橹皇亲鳒y試用,所以沒加修飾。測試時(shí),只需要按照上述三個(gè)方法修改字符集即可。
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)?...{
??????}
????}
??}
}
有什么問題,歡迎來討論。
?
最好讓上述9個(gè)字符集變量值保持一致,或者至少“兼容”,同時(shí)也要考慮到OS中l(wèi)ocale的值。
當(dāng)然:character_set_system例外,它是存儲和表示元信息使用的字符集,一般都是ascii串,使用utf8和使用latin1基本一樣,但是,如果使用中文,可能就另當(dāng)別論了。下邊說的全部變量是指除了character_set_system以外的其它變量。
這里推薦三個(gè)方案:
1. 全部使用latin1
但是在java程序中,它擔(dān)著一定的風(fēng)險(xiǎn),即在入庫之前,需要將字符串從gbk轉(zhuǎn)換到iso8859_1,出庫以后,獲取結(jié)果時(shí),再從iso8859_1轉(zhuǎn)到gbk.
否則會出現(xiàn)亂碼。
這種方式比較適合于C代碼,顯示依賴于操作系統(tǒng)的locale.一般都不用轉(zhuǎn)換。
2. 全中文支持,全部設(shè)置成gbk.
方法:
??? 在my.ini中修改添加:(這個(gè)是必須的)
??? [mysqld]
??? default-character-set=gbk
??? 在java程序里邊使用"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK"這樣的url,表明使用GBK進(jìn)行編碼。
? ? ??
3. utf8字符集支持.
方法:
??? 在my.ini中修改添加:
??? [mysqld]
??? default-character-set=utf8???
??? 在java程序里邊使用"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"這樣的url,表明使用GBK進(jìn)行編碼。???
??? 注意utf8與UTF-8的分別.
utf8的好處是java虛擬機(jī)可以自動將它與gbk進(jìn)行轉(zhuǎn)換,因而顯示都不會有亂碼。可是在控制臺下(cmd),顯示就有問題了。??? ???
六.使用java代碼顯示字符集變量及測試字符集的顯示
因?yàn)橹皇亲鳒y試用,所以沒加修飾。測試時(shí),只需要按照上述三個(gè)方法修改字符集即可。
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)?...{
??????}
????}
??}
}
有什么問題,歡迎來討論。
?
轉(zhuǎn)載于:https://www.cnblogs.com/mixer/archive/2006/09/20/2448962.html
總結(jié)
以上是生活随笔為你收集整理的MySQL中的字符集涵义及使用方法总结(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: poi ppt 作者属性 修改_POI之
- 下一篇: mysql 保留5位小数_小猿圈分享-M