java 調用存儲過程 傳入數組

創建一個表
create table test(
    id number(10)
    );

 


創建一個與test的id同類型的不定長數組的定義

create or replace type T_NUMBER as table of number(10);

 

 

創建一個存儲過程


create or replace procedure transferData(autoTypeIds t_number) is

        
         for i in 1..autoTypeIds.COUNT loop

        insert into test values(autoTypeIds(i) );
       
    end loop;

 
     
end transferData;

 

判斷數據爲空

if autoTypeIds .COUNT = 0 then

 

或者

if autoTypeIds .First is null then

 

 

部分Java代碼


 private void dealARRAY(int autoTypeIds) {


 final String T_NUMBER = "T_NUMBER";


  Connection conn = getConnection();


  CallableStatement cstmt = null;


  Session session = getSession();


  Transaction tx = null;


  boolean finished = false;


  String procedure = "{call transferData(?) }";

 

  ArrayDescriptor varchar2Desc = null;


  try {


   tx = session.beginTransaction();


   cstmt = conn.prepareCall(procedure);


   varchar2Desc = ArrayDescriptor.createDescriptor(T_NUMBER, conn);


   // 將字符串數組轉換爲oralce能識別的數組


   ARRAY vArray = new ARRAY(varchar2Desc, conn, ids);


   cstmt.setArray(1, vArray);


   cstmt.executeUpdate();

 

   session.flush();


   tx.commit();


   conn.commit();


   finished = true;


  } catch (SQLException e) {


   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  finally {

   if (!finished) {
    if (tx != null) {
     tx.rollback();
    }

    if (conn != null) {
     try {
      conn.rollback();
     } catch (SQLException e1) {
     }
    }

   }
   try {
    if (cstmt != null)
     cstmt.close();
   } catch (SQLException x) {
   }

   closeConnection(conn);

  }

 

發佈了35 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章