5

我已经实现java.sql.SQLData了使用 ojdbc6 将 UDT 对象绑定到准备好的语句。现在,我的一些 UDT 包含数组。我现在需要做的是:

class MyType implements SQLData {
  public void writeSQL(SQLOutput stream) throws SQLException {
    Array array = //...
    stream.writeArray(array);
  }
}

为了构造 Oracle 数组,我需要一个 JDBC 连接。通常,这样做是这样的:

OracleConnection conn = // ...
Array array = conn.createARRAY("MY_ARRAY_TYPE", new Integer[] { 1, 2, 3 });

但是,在那种writeSQL(SQLOutput)方法中,我没有联系。此外,由于难以在简明问题中解释的原因,我无法在MyType. 我可以以某种方式从中提取该连接SQLOutput吗?我想避免使用这样的不稳定结构:

// In ojdbc6, I have observed a private "conn" member in OracleSQLOutput:
Field field = stream.getClass().getDeclaredField("conn");
field.setAccessible(true);
OracleConnection conn = (OracleConnection) field.get(stream);

有任何想法吗?备择方案?

4

2 回答 2

6

这是我为解决此问题所做的工作。它不漂亮,但它有效。

我在我的类实现SQLData中添加了一个方法,该方法接收 ajava.sql.Connection并设置相应的java.sql.ARRAY对象。

像这样的东西:

public class MyObject01 implements SQLData {
   private String value;
   private MyObject02[] details; // do note that details is a java array
   // ... also added getters and setters for these two properties

   private Array detailsArray;

   public void setupArrays(oracle.jdbc.OracleConnection oconn)
      throws SQLException
   {
       detailsArrays = oconn.createARRAY(MyObject02.ORACLE_OBJECT_ARRAY_NAME, getDetails());
       // MyObject02.ORACLE_OBJECT_ARRAY_NAME must be the name of the oracle "table of" type name
       // Also note that in Oracle you can't use JDBC's default createArray
       // since it's not supported. That's why you need to get a OracleConnection
       // instance here. 
   }       

   @Override
   public void writeSQL(Stream stream) throws SQLException {
       stream.writeString(getValue());
       stream.writeArray(detailsArray); // that's it
   }

   @Override
   public void readSQL(Stream stream) throws SQLException {
       setValue(stream.readString());
       Array array = stream.readArray();
       if (array != null) {
           setDetails((MyObject02[])array.getArray());
       }
   }

这是第一部分。

然后,在过程调用中使用该对象之前,调用setupArrays 该对象的方法。例子:

public class DB {
    public static String executeProc(Connection conn, MyObject01 obj)
        throws SQLException
    {
        CalllableStatement cs = conn.prepareCall(" { ? = call sch.proc(?) }");
        cs.registerOutParameter(1, Types.VARCHAR);
        obj.setupArrays((oracle.jdbc.Connection)conn);
        cs.setObject(2, obj, Types.STRUCT);
        cs.executeUpdate();
        String ret = cs.getString(1);
        cs.close();
        return ret;
    }
}

当然,在连接时,您需要正确注册您的类型:

Connection conn = DriverManager.getConnection("jdbc:oracle://localhost:1521/XE", "scott", "tiger" );
conn.getTypeMap().put(MyObject01.ORACLE_OBJECT_NAME, MyObject01.class);
conn.getTypeMap().put(MyObject02.ORACLE_OBJECT_NAME, MyObject02.class);
conn.getTypeMap().put(MyObject02.ORACLE_OBJECT_ARRAY_NAME, MyObject02[].class);

希望能帮助到你。

于 2012-07-27T14:47:01.800 回答
0

看看这个SO 答案。可以从以下位置获取连接SQLOutput

    final OracleSQLOutput out = (OracleSQLOutput) sqlOutput;
    OracleConnection conn = (OracleConnection) out.getSTRUCT().getJavaSqlConnection();
于 2019-10-04T11:27:50.873 回答