[英]How to write a java.sql.Array to a java.sql.SQLOutput within SQLData.writeSQL() for Oracle
I have implemented java.sql.SQLData
in order to bind UDT objects to prepared statements using ojdbc6. 我已经实现了java.sql.SQLData
,以便使用ojdbc6将UDT对象绑定到准备好的语句。 Now, some of my UDT's contain arrays. 现在,我的一些UDT包含数组。 What I need to do now is this: 我现在需要做的是:
class MyType implements SQLData {
public void writeSQL(SQLOutput stream) throws SQLException {
Array array = //...
stream.writeArray(array);
}
}
In order to construct Oracle arrays, I need a JDBC Connection. 为了构造Oracle阵列,我需要一个JDBC连接。 Typically, this is done as such: 通常,这样做是这样的:
OracleConnection conn = // ...
Array array = conn.createARRAY("MY_ARRAY_TYPE", new Integer[] { 1, 2, 3 });
However, in that writeSQL(SQLOutput)
method, I do not have a connection. 但是,在该writeSQL(SQLOutput)
方法中,我没有连接。 Also, for reasons that are hard to explain in a concise question, I cannot maintain a connection reference in MyType
. 另外,由于在一个简洁的问题中难以解释的原因,我无法在MyType
维护连接引用。 Can I somehow extract that connection from SQLOutput
? 我可以以某种方式从SQLOutput
提取该连接吗? I'd like to avoid using instable constructs like this: 我想避免使用如下不稳定的构造:
// 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);
Any ideas? 有任何想法吗? Alternatives? 备择方案?
Here's what I did to workaround this issue. 这是解决此问题的方法。 It's not pretty, but it works. 它不漂亮,但是可以用。
I added a method in my class implementing SQLData
that receives a java.sql.Connection
and setups the corresponding java.sql.ARRAY
objects. 我在实现SQLData
类中添加了一个方法,该方法接收一个java.sql.Connection
并设置相应的java.sql.ARRAY
对象。
Something like this: 像这样:
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());
}
}
That's the first part. 这是第一部分。
Then, BEFORE using that object in a procedure call, invoke setupArrays
method on that object. 然后,在过程调用中使用该对象之前,请对该对象调用setupArrays
方法。 Example: 例:
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;
}
}
Of course, upon connection, you need to register your types properly: 当然,连接后,您需要正确注册类型:
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);
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.