如何调用与作为表类型OUT参数的过程从一个Java类(how to call procedure w

2019-06-26 12:12发布

我想调用这个过程get_data_Q1在包ult_pkgJava代码,并显示出看跌期权,可以在任何一个请帮助我。 提前致谢。

CREATE OR REPLACE PACKAGE  ult_pkg
AS
TYPE t_all_record is record (
    x_object_type_id        number,
    x_object_name           varchar2(100),
    x_object_id             varchar2(70),
    x_audit_timestamp       timestamp(6),
    x_payload               clob
);

--table type to hold table data after querying

type tt_all_tab is table of t_all_record index by binary_integer;

--declaration pocedures
procedure get_data_Q1(x_object_id in varchar2 , x_all_type out tt_all_tab );


end ult_pkg;

/

“ult_pkg”包的--body创建或AS更换包装体ult_pkg

--procedure taking 'object_id' as input parameter and gives out table of 't_all_record' type 
procedure get_data_Q1(x_object_id in varchar2 , x_all_type out tt_all_tab )
AS

    i number:=0;
begin

    for r in 
        ( 
            SELECT
            O.object_type_id,O.object_name,O.object_id,A.audit_timestamp,P.payload
            FROM
            APPLICATION APP, EXCEPTIONS E,MASTER_AUDIT A,MODULE_TYPE M,OBJECT_TYPE O,PAYLOAD P 
            WHERE 
            ( A.MODULE_TYPE_ID = M.MODULE_TYPE_ID ) AND ( M.APPLICATION_ID = APP.APPLICATION_ID ) AND ( A.OBJECT_TYPE_ID = O.OBJECT_TYPE_ID ) AND ( O.OBJECT_ID = x_object_id )
        )
    -- loop to asign the data from cursor 'r' to carasponding table type columns
        loop


        x_all_type(i).x_object_type_id:=r.object_type_id;
        x_all_type(i).x_object_name:=r.object_name;
        x_all_type(i).x_object_id:=r.object_id;
        x_all_type(i).x_audit_timestamp:=r.audit_timestamp;
        x_all_type(i).x_payload:=r.payload;
        i:=i+1;
        end loop;

    end get_data_Q1;

结束ult_pkg; /

Answer 1:

这不可能,见访问PL / SQL索引表 :

甲骨文JDBC不支持RAW,DATE和PL / SQL记录作为元素类型。

我可能会使用自定义的(全球性的,不包),对象类型,如下所示:

CREATE TYPE t_all_record AS OBJECT (
  x_object_type_id        number,
    x_object_name           varchar2(100),
    x_object_id             varchar2(70),
    x_audit_timestamp       timestamp(6),
    x_payload               clob
)
/
CREATE TYPE t_all_records IS TABLE OF t_all_record
/

引用类型的表在你的包(t_all_records而不是tt_all_tab)和填充它像这样

procedure get_data_Q1(x_object_id in varchar2 , x_all_type out t_all_records )
AS
begin
    SELECT t_all_record(O.object_type_id,O.object_name,O.object_id,A.audit_timestamp,P.payload)
    BULK COLLECT INTO x_all_type
    FROM APPLICATION APP, EXCEPTIONS E,MASTER_AUDIT A,MODULE_TYPE M,OBJECT_TYPE O,PAYLOAD P 
    WHERE ( A.MODULE_TYPE_ID = M.MODULE_TYPE_ID ) AND ( M.APPLICATION_ID = APP.APPLICATION_ID ) AND ( A.OBJECT_TYPE_ID = O.OBJECT_TYPE_ID ) AND ( O.OBJECT_ID = x_object_id )
end get_data_Q1;

结果将是一样所以Java可用:

package tests.jdbc;

import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import java.sql.Struct;
import java.sql.Types;

import oracle.sql.StructDescriptor;

public class OracleTableOfResult {
    public static void main(String...a) throws Exception {
        Class.forName("oracle.jdbc.OracleDriver");
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:<USER>/<PASS>@<DATABASEHOST>:1521:<SERVICE>");

        final String typeName = "T_ALL_RECORD";
        final String typeTableName = "T_ALL_RECORDS";

        // Get a description of your type (Oracle specific)
        final StructDescriptor structDescriptor = StructDescriptor.createDescriptor(typeName.toUpperCase(), connection);        
        final ResultSetMetaData metaData = structDescriptor.getMetaData();

        // Call the procedure (or whatever else) that returns the table of a custom type
        CallableStatement cs = connection.prepareCall("{call ult_pkg.get_data_Q1(?, ?)}");
        cs.setString(1, "the_id");
        // Result is an java.sql.Array...
        cs.registerOutParameter(2, Types.ARRAY, typeTableName);     
        cs.execute();

        // ...who's elements are java.sql.Structs
        Object[] data = (Object[]) ((Array) cs.getObject(2)).getArray();
        for(Object tmp : data) {
            Struct row = (Struct) tmp;
            // Attributes are index 1 based...
            int idx = 1;
            for(Object attribute : row.getAttributes()) {               
                System.out.println(metaData.getColumnName(idx) + " = " + attribute);                                            
                ++idx;
            }
            System.out.println("---");
        }
        cs.close();     
        connection.close();
    }
}

但最终,这是值得怀疑它是否值得时,你可以使用查询在一个普通的SQL语句作为一个事先准备好的声明的努力...



文章来源: how to call procedure with out parameter as table type from a java class