I have implemented java.sql.SQLData in order to bind UDT objects to prepared statements using ojdbc6. Now, some of my UDT’s contain arrays. 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. 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. Also, for reasons that are hard to explain in a concise question, I cannot maintain a connection reference in MyType. Can I somehow extract that connection from 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
SQLDatathat receives ajava.sql.Connectionand setups the correspondingjava.sql.ARRAYobjects.Something like this:
That’s the first part.
Then, BEFORE using that object in a procedure call, invoke
setupArraysmethod on that object. Example:Of course, upon connection, you need to register your types properly:
Hope it helps.