Accessing the Custom Object Return type from ojdbc6 JDBC Thin Drivers

巧了我就是萌 提交于 2019-12-12 01:33:28

问题


I'm writing some JDBC code which calls a Oracle 11g PL/SQL procdedure which has a Custom Object return type. I can get the code to call the procedure, but how do I access the returned Custom Object to obtain it's contained values?. An example of my code calling the procedure is below:

PLSQL Code:

Procedure GetDataSummary (p_my_key    IN    KEYS.MY_KEY%TYPE,
                          p_recordset OUT   data_summary_tab,
                          p_status    OUT   VARCHAR2);

Java Code:

String query = "begin manageroleviewdata.getdatasummary(?, ?, ?); end;");
CallableStatement stmt = conn.prepareCall(query);

// Single IN parameter
stmt.setInt(1, 83);

// Two OUT parameters, one a Custom Object, the other a VARCHAR
stmt.registerOutParameter(2, OracleTypes.ARRAY, "DATA_SUMMARY_TAB");
stmt.registerOutParameter(3, OracleTypes.VARCHAR);

stmt.execute(stmt);

How do I get the result back fron this?


回答1:


We've cracked it:

oracle.sql.ARRAY result2 = (oracle.sql.ARRAY) stmt.getObject(2);
ResultSet rs = result2.getResultSet();
oracle.sql.STRUCT elements = (oracle.sql.STRUCT) rs.getObject(2);
String result = null;
if (elements != null) {
    Object[] objs = elements.getAttributes();
    result = objs[2];
}
System.out.println("Result: " + result);

In our case this prints the third element in our Custom Object type.



来源:https://stackoverflow.com/questions/2965788/accessing-the-custom-object-return-type-from-ojdbc6-jdbc-thin-drivers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!