0

如何在 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;

下面是运行存储过程的 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)

我还尝试运行没有记录类型的新存储过程,但存在相同的错误:

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;

我在某处发现可以使用数组描述符或结构描述符,但如何在 registerOutParameter 中使用它,没有任何文档/站点解释。我被困在这里。

4

2 回答 2

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();
}
于 2019-02-27T12:35:45.100 回答
0

这是不可能的。您只能在 jdbc(UDT、集合)中使用 sql 级别的对象。但在您的情况下,您可以考虑使用 sys_refcursors。在 java 中 sys_refcursor 由普通的结果集表示。

下面的概念:

甲骨文:

 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;

爪哇:

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);
于 2019-02-15T07:21:19.780 回答