簡體   English   中英

使用簡單的jdbc調用將數組作為輸入參數傳遞給oracle存儲過程

[英]Pass array as input parameter to an oracle stored procedure using simple jdbc call

這是我的oracle程序規范

CREATE OR REPLACE PACKAGE PKG_RE_FI AS

  PROCEDURE PRC_RE_FI_DETAILS(P_FAN_NO       IN VARCHAR2,
                              P_REF_ID       IN TY_APP_REF_ID,
                              P_COMMENTS     IN VARCHAR2,
                              P_BILLING_FLAG IN VARCHAR2,
                              P_STATUS       OUT VARCHAR2);
END PKG_RE_FI;

TY_APP_REF_ID為

CREATE OR REPLACE TYPE ty_app_REF_ID as varray(500) of obj_array_ref_id

CREATE OR REPLACE TYPE obj_array_ref_id  AS OBJECT(
app_ref_id VARCHAR2(100)
)

我正在使用Spring JDBC Framework(SimpleJdbcCall對象)執行上述過程。 以下是我在其中聲明的代碼段

      this.reFIJdbcCall =  new SimpleJdbcCall(dataSource).withCatalogName("PKG_RE_FI").
              withProcedureName("PRC_RE_FI_DETAILS").declareParameters(new SqlParameter("P_FAN_NO", Types.VARCHAR),
                        new SqlParameter("P_REF_ID", Types.ARRAY),
                        new SqlParameter("P_COMMENTS", Types.VARCHAR),
                        new SqlParameter("P_BILLING_FLAG", Types.VARCHAR),
                        new SqlOutParameter("P_STATUS", Types.VARCHAR)
              );

我應該如何將數組傳遞給

new SqlParameter("P_REF_ID", Types.ARRAY),

到MapSqlParameterSource

 MapSqlParameterSource in = new MapSqlParameterSource();

Spring Data JDBC Extensions項目提供了一些支持,使這變得更加容易。 查看有關傳遞Oracle ARRAY類型參考手冊

PeudoCode同樣是如何實現的。

    # 1.You will require a structDescriptor object for an object equivalent in pl sql like :

    StructDescriptor structDes= new StructDescriptor("<schemaname in caps>.<sql_object_name>", connectionObject);

    # 2. You will need to pass one object values such name, class, id to an object array in order and accordance to 'sql_object_name' object. 

    For exmaple:
    STRUCT[] structArray=new STRUCT[<ListObj>.size()];
    int index=0;
    for (a in ListObj){

    Object[] object=new Object[]{a.getName(),a.getId()};
    STRUCT struct=new STRUCT(structDes ,connectionObject,object);
               structArray[index]=struct;
               index++;

    }

    ArrayDescriptor arrayDes=ArrayDescriptor.createDescriptor(
        "<Schema name>.<table object from sql>", connectionObject);

    ARRAY array=new ARRAY(arrayDes,connectionObject, structArray);

   then pass it to proc 

   .declareParameters(
   new SqlInOutParameter("<parameter to proc name>",OracleTypes.ARRAY,"
   <schema name>.<sql_array_or_table_obj>"))

   like 
   Hashmap<String, Object> map= new HashMap<>();
   map.put("<parameter to proc name>",array);
   psStatement.execute(map);

希望能幫助到你。 該順序可能會根據使用的sql數據庫的要求和類型而有所不同,但是基礎是相同的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM