码迷,mamicode.com
首页 > 数据库 > 详细

java调用oracle数组类型

时间:2017-11-13 21:37:27      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:调用   import   不能   oid   nec   message   ted   .exe   ref   

首先当然是在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: 引用未初始化的收集的问题解决

java调用oracle数组类型

标签:调用   import   不能   oid   nec   message   ted   .exe   ref   

原文地址:http://www.cnblogs.com/huanghongbo/p/7827862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!