2011-08-11 78 views
0

對於嵌套表我按照以下方式完成了它? 這是pl/sql存儲過程。如何將pl/sql的輸出關聯數組傳遞給java?

CREATE or REPLACE PROCEDURE TEST(
activationStartDate IN DATE, 
activationEndDate IN DATE, 
deActivationStartDate IN DATE, 
deActivationEndDate IN DATE 
Out_Entity OUT TEST1.RefCsr 
) 
AS 
FirstNameListTable LIST_TABLE; 

{--COMMENT :LIST_TABLE is nested table :create or replace TYPE "LIST_TABLE" as table  of varchar2(20);-----Nested Table Declaration 
/
} 

totalSameFirstName NUMBER; 
j NUMBER := 1; 
BEGIN 



SELECT first_name BULK COLLECT INTO FirstNameListTable FROM Employee where start_date between activationStartDate AND activationEndDate 
MINUS 
SELECT first_name FROM Employee where start_date between deActivationStartDate AND deActivationEndDate 

OPEN Out_Entity FOR SELECT * FROM TABLE(
              CAST (
               FirstNameListTable AS LIST_TABLE 
              ) 
           ) Nos; 

    END; 
/

我JavaCode是

--First getConnection 
--prepare sql string : sql = "{ Call Test(?,?,?,?,?) } "; 
--Use prepareCall function on Connection object and passed this sql string and retrived CallableStatement class object. 
stmt.setTimestamp(1,activationStartDate); 
stmt.setTimestamp(2,activationEndDate); 
stmt.setTimestamp(3,deActivationStartDate); 
stmt.setTimestamp(4,deActivationEndDate); 
stmt.registerOutParameter(5,-10); 
stmt.execute(); 
List result = new ArrayList(); 
     ResultSet rs = (ResultSet)stmt.getObject(5); 
     int i=0; 
     while (rs.next()) 
     { 
      System.out.println(i+". "+rs.getString(1)); 
      i++; 
     } 

那麼如果我想從員工選擇多列,並傳遞給我的javaCode ......如何我javaCode和PL/SQL會看喜歡?

+0

此問題似乎是你也是。 http://stackoverflow.com/questions/457933/creating-package-level-associative-array-in-java – Ollie

回答

1

你需要在結果集中使用嵌套循環

例如得到列計數值

,並嘗試這樣

List<List> result = new ArrayList()<List>; 
List col = new ArrayList(); 

ResultSet rs = (ResultSet)stmt.getObject(5); 
ResultSetMetaData rsMetaData = rs.getMetaData(); 

     int numberOfColumns = rsMetaData.getColumnCount(); 
     while (rs.next()) 
     { 
      col = new ArrayList(); 
      for(int j=1;j<numberOfColumns;j++) 
       col.add(rs.getString(j); 

      result.add(col); 
     } 

這裏第一個循環用於獲取行和嵌套循環獲取列值 此值存儲在列表對象中,並且需要每次創建新對象

相關問題