如何在Jdbc中检索Oracle集合类型。我搜索了很多,但没有找到答案。下面是我的存储过程正常工作:
create or replace PACKAGE SHOW_SUP AS
TYPE SUP_COF_REC IS RECORD (
SUPPLIER_NAME VARCHAR2(32),
COFFEE_NAME VARCHAR(32)
);
TYPE_RECORD SUP_COF_REC;
TYPE SUP_COF_TAB IS TABLE OF TYPE_RECORD%TYPE;
PROCEDURE SHOW_SUPPLIERS (SUP_COF_DET OUT SUP_COF_TAB);
END SHOW_SUP;
create or replace PACKAGE BODY SHOW_SUP AS
PROCEDURE SHOW_SUPPLIERS (SUP_COF_DET OUT SUP_COF_TAB) AS
STR VARCHAR2(2000);
BEGIN
STR:='SELECT SUP_NAME, COF_NAME FROM SUPPLIERS S, COFFEES C WHERE
S.SUP_ID=C.SUP_ID ORDER BY SUP_NAME';
EXECUTE IMMEDIATE(STR) BULK COLLECT INTO SUP_COF_DET;
END SHOW_SUPPLIERS;
END SHOW_SUP;
下面是运行存储的proc的Java代码:
String sql="{call SHOW_SUP.SHOW_SUPPLIERS(?)}";
CallableStatement cs = con.prepareCall(sql);
cs.registerOutParameter(1, java.sql.Types.ARRAY, "SUP_COF_TAB"); //LINE 3
//cs.registerOutParameter(1, java.sql.Types.ARRAY, "SHOW_SUP.SUP_COF_TAB"); //LINE 4
cs.execute();
我在3/4行收到错误:
SQLException : invalid name pattern: SCOTT.SUP_COF_TAB
java.sql.SQLException: invalid name pattern: SCOTT.SUP_COF_TAB
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:263)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:271)
at oracle.jdbc.oracore.OracleTypeADT.initMetadata(OracleTypeADT.java:543)
at oracle.jdbc.oracore.OracleTypeADT.init(OracleTypeADT.java:462)
at oracle.sql.ArrayDescriptor.initPickler(ArrayDescriptor.java:1678)
at oracle.sql.ArrayDescriptor.<init>(ArrayDescriptor.java:291)
at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:206)
at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:175)
at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:158)
at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:125)
at oracle.jdbc.driver.NamedTypeAccessor.otypeFromName(NamedTypeAccessor.java:84)
at oracle.jdbc.driver.TypeAccessor.initMetadata(TypeAccessor.java:89)
at oracle.jdbc.driver.T4CCallableStatement.allocateAccessor(T4CCallableStatement.java:629)
at oracle.jdbc.driver.OracleCallableStatement.registerOutParameterInternal(OracleCallableStatement.java:166)
at oracle.jdbc.driver.OracleCallableStatement.registerOutParameter(OracleCallableStatement.java:207)
at oracle.jdbc.driver.OracleCallableStatementWrapper.registerOutParameter(OracleCallableStatementWrapper.java:1584)
at com.jdbc.callstatement.CallableStatementSample.main(CallableStatementSample.java:48)
我还尝试运行没有RECORD类型的新存储过程 ,但是存在相同的错误:
create or replace PACKAGE SHOW_COF AS
TYPE COF_TAB IS TABLE OF COFFEES%ROWTYPE INDEX BY PLS_INTEGER;
PROCEDURE SHOW_COFFEES (COF_DET OUT COF_TAB);
END SHOW_COF;
我在某处发现可以使用Array描述符或struct描述符,但是如何在registerOutParameter中使用它,没有任何文档/站点对此进行了解释。我被困在这里。
答案 0 :(得分:1)
使用SQL结构化类型的标准名称,如下所示:
cs.registerOutParameter(1, java.sql.Types.ARRAY, "SHOW_SUP.SUP_COF_TAB"); //LINE 3.
要读取out参数,请在您的Java代码中执行以下操作:
// Get the out parameter as java.sql.Array
java.sql.Array ntab = (java.sql.Array) cStmt.getObject(1);
// Now get the rows from the nested table
Object[] rows = (Object[])ntab.getArray();
// For each row
for (Object row: rows) {
// Get the columns
Object[] cols = ((Struct) row).getAttributes();
String supplierName = cols[0].toString();
String coffeeName = cols[1].toString();
}
答案 1 :(得分:0)
这是不可能的。您只能在jdbc(UDT,collections)中使用sql级对象。但是在您的情况下,您可以考虑使用sys_refcursors。 在java中,sys_refcursor由普通结果集表示。
以下概念:
ORACE:
PROCEDURE SHOW_SUPPLIERS (SUP_COF_DET OUT SYS_REFCURSOR);
..
PROCEDURE SHOW_SUPPLIERS (SUP_COF_DET OUT SYS_REFCURSOR) AS
c_cursor SYS_REFCURSOR
BEGIN
open c_cursor for 'SELECT SUP_NAME, COF_NAME FROM SUPPLIERS S, COFFEES C WHERE
S.SUP_ID=C.SUP_ID ORDER BY SUP_NAME';
SUP_COF_DET := c_cursor
END SHOW_SUPPLIERS;
Java:
String sql="{call SHOW_SUP.SHOW_SUPPLIERS(?)}";
CallableStatement cs = con.prepareCall(sql);
cs.registerOutParameter(1, OracleTypes.CURSOR);
cs.executeUpdate();
ResultSet rs = (ResultSet) cs.getObject(1);