如何将pl / sql的输出关联数组传递给java?

时间:2011-08-11 06:11:15

标签: java jdbc plsql

对于嵌套表我是以后续方式完成的吗? 这是pl / sql存储过程。

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++;
        }

那么如果我想从EMPLOYEE中选择多个列并传递给我的javaCode ......我的javaCode和Pl / Sql会是什么样子怎么样?

1 个答案:

答案 0 :(得分: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);
        }

这里是获取行的第一个循环和获取列值的嵌套循环 此值存储在col列表对象中,并且每次都需要创建新对象