zoukankan      html  css  js  c++  java
  • java plsql 调用oracle数组类型

    首先当然是在oracle中建立type

    CREATE OR REPLACE TYPE cux_proxy_bid_award_rec IS OBJECT
    (
      trading_partner_id NUMBER,
      bid_price          NUMBER,
      bid_publish_date   DATE,
      bid_award_flag     VARCHAR2(10)
    );

    再建立引用type

    CREATE OR REPLACE TYPE cux_proxy_bid_award_tbl IS TABLE OF CUX_PROXY_BID_AWARD_REC;

    java中调用

    import java.util.ArrayList;
    import java.sql.SQLException;
    
    import oracle.sql.ARRAY;
    import oracle.sql.ArrayDescriptor;
    import oracle.sql.STRUCT;
    import oracle.sql.StructDescriptor;
    
    import oracle.jdbc.OracleCallableStatement;
    import oracle.jdbc.OracleConnection;
    
    
    
        public void crtSuppResponseAndBidAward(String auctionHeaderId){
             java.util.ArrayList  array = new  java.util.ArrayList();
            
            PonSourceProxyBidVOImpl vo  = this.getPonSourceProxyBidVO1();
            vo.first();
            vo.previous();
            while(vo.hasNext()){
                Row row = vo.next();
                Object[] obj = new Object[4];
                obj[0] = (Number)row.getAttribute("TradingPartnerId");
                obj[1] = (Number)row.getAttribute("BidPrice");
                obj[2] = (Date)row.getAttribute("BidPublishDate");
                obj[3] = (String)row.getAttribute("BidAwardFlag")!=null?row.getAttribute("BidAwardFlag"):"N";
                //构造ArrayList
                array.add(obj);
            }
            
            OracleCallableStatement statement = null;
            OracleConnection oracleConnection = (OracleConnection)this.getOADBTransaction().getJdbcConnection();
            try{
                //将arraylist解析为STRUCT
                STRUCT[] arrayOfSTRUCT = createMyRows(oracleConnection, array);
                
                //此处使用引用的TYPE
    //不能调用在PACKAGE中定义的TYPE
    ArrayDescriptor tableOfBidDescriptor = ArrayDescriptor.createDescriptor("CUX_PROXY_BID_AWARD_TBL", oracleConnection); ARRAY localARRAY = new ARRAY(tableOfBidDescriptor, oracleConnection, arrayOfSTRUCT); OracleCallableStatement stmt = (OracleCallableStatement)getOADBTransaction().createCallableStatement("{ call CUX_PON_SOURCING_PUB.AUTO_CRT_BID_AWARD(:1, :2, :3, :4, :5) }", -1); String aucHeaderIdEncrypt = SourcingServerUtil.URLEncrypt(getOADBTransaction(), auctionHeaderId ); stmt.setString(1, aucHeaderIdEncrypt); stmt.setString(2, auctionHeaderId); stmt.setARRAY(3, localARRAY); stmt.registerOutParameter(4, Types.NUMERIC); stmt.registerOutParameter(5, Types.VARCHAR); stmt.execute(); oracle.sql.NUMBER retState = stmt.getNUMBER(4); retState.toString(); System.out.println("stmt.getNUMBER(4) "+stmt.getNUMBER(4)); LogUtil.of(" String.valueOf(stmt.getNUMBER(4)) "+String.valueOf(stmt.getNUMBER(4))+" retState "+retState.stringValue(), this).print(this); ModelUtil.commit(this, true); String retStatus = stmt.getNUMBER(4).stringValue() ; String retMsg = stmt.getString(5); if( "0".equals(retStatus)){ OAException dialogMsg = new OAException("决标完成!", OAException.CONFIRMATION); this.getOADBTransaction().putDialogMessage(dialogMsg); }else{ throw new OAException(retMsg); } }catch(SQLException e){ throw OAException.wrapperException(e); } } ////将arraylist解析为STRUCT private STRUCT[] createMyRows(Connection paramConnection, java.util.ArrayList paramArrayList) { int j = paramArrayList.size(); STRUCT[] arrayOfSTRUCT; try { //此处使用定义的原始TYPE OBJECT,或者TABLE StructDescriptor localStructDescriptor = StructDescriptor.createDescriptor("CUX_PROXY_BID_AWARD_REC", paramConnection); int k; arrayOfSTRUCT = new STRUCT[j]; for (k = 0; k < j; k++) { arrayOfSTRUCT[k] = new STRUCT(localStructDescriptor, paramConnection, (Object[])paramArrayList.get(k)); } } catch (Exception localException) { throw OAException.wrapperException(localException); } return arrayOfSTRUCT; }

     参考:

    oracle存储过程输入输出数组对象和java调用情况

    如何把java中的arrayList转化为oracle中的数组(array)

    在PL/SQL中直接使用ORACLE数组

    DECLARE
    
      l_rec cux_proxy_bid_award_tbl := cux_proxy_bid_award_tbl();
    BEGIN
      FOR i IN 1 .. 5 LOOP
        l_rec.extend; -- 必须指定,否则会报指针越界
        l_rec(i) := cux_proxy_bid_award_rec(i, 100, SYSDATE, 'Y'); -- 如果是record也可以直接赋值cux_proxy_bid_award_rec,record的方式仅限于在PACKAGE中定义的TYPE,独立的TYPE只能是OBJECT
      END LOOP;
    
      dbms_output.put_line(l_rec.count);
      dbms_output.put_line(l_rec(3).trading_partner_id);
    
    END;

    参考:

    ORA-06531: 引用未初始化的收集的问题解决

  • 相关阅读:
    Sql 复习(1)
    记录一次git issue
    JWT自校验框架
    分布式事务分布式锁的常用解决方式
    SpringBoot开发文档
    SpringCloud的使用以及五大核心组件
    SpringMVC
    关于开发中使用AOP的坑
    SpringCloud使用feign远程调用服务注入映射接口失败问题
    springBoot使用Restful跳转路径回显异常问题
  • 原文地址:https://www.cnblogs.com/huanghongbo/p/7827862.html
Copyright © 2011-2022 走看看