中小企业网站建设教程,校园网网站建设费用,扬州网站开发公司,网站建设制作设计公司五.乱码的避免最好让上述9个字符集变量值保持一致#xff0c;或者至少“兼容”#xff0c;同时也要考虑到OS中locale的值。当然#xff1a;character_set_system例外#xff0c;它是存储和表示元信息使用的字符集#xff0c;一般都是ascii串#xff0c;使用utf8和使用lat…五.乱码的避免最好让上述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-setgbk 在java程序里边使用jdbc:mysql://localhost:3306/test?useUnicodetruecharacterEncodingGBK这样的url,表明使用GBK进行编码。 3. utf8字符集支持.方法 在my.ini中修改添加 [mysqld] default-character-setutf8 在java程序里边使用jdbc:mysql://localhost:3306/test?useUnicodetruecharacterEncodingUTF-8这样的url,表明使用GBK进行编码。 注意utf8与UTF-8的分别.utf8的好处是java虚拟机可以自动将它与gbk进行转换因而显示都不会有乱码。可是在控制台下(cmd)显示就有问题了。 六.使用java代码显示字符集变量及测试字符集的显示因为只是作测试用所以没加修饰。测试时只需要按照上述三个方法修改字符集即可。import java.sql.*;/** *//** * pTitle: /p * * pDescription: /p * * pCopyright: Copyright (c) 2006/p * * pCompany: /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?useUnicodetruecharacterEncodingUTF-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) ...{ } } }}有什么问题欢迎来讨论。 转载于:https://www.cnblogs.com/mixer/archive/2006/09/20/2448962.html