我的Oracle服务器端字符集为AMEROCAN_AMERICA.US7ASCII,这个是已经存在的运行数据库,数据库的字符集已经不能改变.
请问JDBC连接这种字符集的情况下,Java调用端的JVM或者JDBC的链接怎么设置,才能保证存入的和取出的都不会乱码.

解决方案 »

  1.   

    int oracleId = oracle.sql.CharacterSet.UTF8_CHARSET;
    oracle.sql.CharacterSet mycharset = oracle.sql.CharacterSet.make(oracleId);
      

  2.   

    写全了
    CREATE TYPE num_varray AS VARRAY(10) OF VARCHAR2(12)
    /
    CREATE TABLE varray_table (col1 num_varray);
    INSERT INTO varray_table VALUES (num_varray('你好', 'abc'));select * from varray_table;*/import java.sql.*;
    import java.math.*;
    import oracle.jdbc.driver.*;
    import oracle.sql.*;class Array1
    {public static void main(String args[]) throws Exception
    {
      int oracleId = CharacterSet.ZHS16GBK_CHARSET;
      CharacterSet dbCharset = CharacterSet.make(oracleId);  DriverManager.registerDriver
                    (new oracle.jdbc.driver.OracleDriver());  Connection conn =
          DriverManager.getConnection
                           ("jdbc:oracle:thin:@10.9.200.58:1521:db01",
                            "mytest",
                            "mytest");  Statement stmt = conn.createStatement();  ResultSet rs = stmt.executeQuery("SELECT * FROM varray_table");  while (rs.next()) {
        ARRAY my_array = ((OracleResultSet)rs).getARRAY(1);    // return the SQL type names, integer codes,
        // and lengths of the columns
        System.out.println ("Array is of type " + my_array.getSQLTypeName());
        System.out.println ("Array element is of typecode " + my_array.getBaseType());
        System.out.println ("Array is of length " + my_array.length());    // get Array elements
        String[] values = (String[]) my_array.getArray();
        for (int i = 0; i < values.length; i++)
        {
           oracle.sql.CHAR out_value = new oracle.sql.CHAR(values[i], dbCharset);
           System.out.println(">> index " + i + " = " + out_value);
        }
      }  rs.close();
      stmt.close();
      conn.close();
      }
    }
      

  3.   

    主要是这个包oracle.sql.*;oracle官方网站上有的下
      

  4.   

    我是用hibernate作为O/R mapping访问数据库,所以上述方式在hibernate访问数据库的情况下不大可行啊.有没有什么简便的方法在hibernate里面设置?
      

  5.   

    <property name="hibernate.connection.url">
        jdbc:oracle:thin:@10.110.1.12:1521:ORA9
    </property>
    <property name="hibernate.connection.driver_class">
        oracle.jdbc.driver.OracleDriver
    </property>
    <property name="connection.useUnicode">true</property>
    <property name="connection.characterEncoding">UTF-8</property>
    <!--<property name="hibernate.connection.defaultNChar">true</property>-->
    <property name="hibernate.connection.username">user</property>
    <property name="hibernate.connection.password">123456</property>
    <!-- property name="hibernate.connection.pool_size"></property -->
    <!-- dialect for Oracle 9 -->
    <property name="dialect">
               net.sf.hibernate.dialect.Oracle9Dialect
    </property>
      

  6.   

    cenlmmx(学海无涯苦作舟),你有试过酱紫可行吗
    <property name="connection.useUnicode">true</property>
    <property name="connection.characterEncoding">UTF-8</property>
    针对Oracle好像不起作用..
      

  7.   

    可以考虑在get set 方法里面加进去装码的代码, 而实体的get set方法可以用代码生成器自动生成.