Inilah yang saya lakukan untuk mengatasi masalah ini. Ini tidak cantik, tapi berhasil.
Saya menambahkan metode di kelas saya yang mengimplementasikan SQLData
yang menerima java.sql.Connection
dan menyiapkan java.sql.ARRAY
yang sesuai objek.
Sesuatu seperti ini:
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());
}
}
Itu bagian pertama.
Kemudian, SEBELUM menggunakan objek itu dalam panggilan prosedur, aktifkan setupArrays
metode pada objek tersebut. Contoh:
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;
}
}
Tentu saja, setelah koneksi, Anda harus mendaftarkan tipe Anda dengan benar:
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);
Semoga membantu.