""字的显示问题

系统环境:
    Oracle 10.2 字符集SIMPLIFIED CHINESE_CHINA.ZHS16GBK
    jvm 字符集:GBK
   该程序在SUN的JRE下运行正常,在IBM JRE下运行结果不正确“”显示为“?”
/**
 * Create on 2010-7-14
 * Copyright 2009 startech. All rights reserved.
 */
package com;

import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author lb
 *
 */
public class TestJdbc {

/**
* @param args
*/
public static void main(String[] args) {
TestJdbc test = new TestJdbc();
try {
test.testJdbc();
} catch (Exception e) {
e.printStackTrace();
}

}
private void testJdbc() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@10.40.0.15:1521:sxyldev";
Connection connection = DriverManager.getConnection(url, "cp3test",
"cp3test");
Statement statement = connection.createStatement();
ResultSet resultSet = statement
.executeQuery("select name from users where id = '1010900166'");
while (resultSet.next()) {
String nameString = resultSet.getString("name");
System.out.println(nameString);
byte[] bytes = nameString.getBytes();
for (byte b : bytes) {
System.out.print(b);
}
System.out.println();
}
System.out.println("当前JVM的默认字符集:" + Charset.defaultCharset());
}
}

运行结果:
dbc
张?
-43-5963
当前JVM的默认字符集:GBK

oracle数据库记录:
SQL> select *from users;

  ID NAME
----------- --------------------
1010900166 张

“”在JVM中有两个编码19886和59491,19886可以在页面显示,但保存到数据库为乱码,59491可以保存到数据库中,但显示为乱码。且如果把页面字符集改为gb18030,则页面会随机出现乱码。

你可能感兴趣的:(java,jvm,oracle,sql,jdbc)