本站不再更新,欢迎光临 java开发技术网
随笔-230  评论-230  文章-8  trackbacks-0

oracle 存储过程返回数组的方法:
1.建立包
create or replace package test is
TYPE filename_array IS TABLE OF varchar2(1);
 filename filename_array;
end test;
2. 建立存储过程
create or replace procedure test_array(v_cfjg out test.filename_array ) is
begin DECLARE i number;
D_cfjg dic_cfjg%rowTYPE;
-- D_nr dic_cfjg%rowTYPE;
cursor c1 is SELECT * FROM dic_cfjg;
BEGIN
i:=0;
v_cfjg := test.filename_array(); --数组初始化

open c1;
LOOP fetch c1 into D_cfjg;
EXIT WHEN c1%NOTFOUND ;
i:=i+1;
v_cfjg.EXTEND;
-- DBMS_OUTPUT.PUT_LINE(TO_CHAR(D_cfjg.dm));
v_cfjg(v_cfjg.count):=D_cfjg.dm;
DBMS_OUTPUT.PUT_LINE(v_cfjg(v_cfjg.count));
-- 测试
-- FETCH C1 INTO D_cfjg;
-- EXIT WHEN c1%NOTFOUND ;
END LOOP;
end;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('TOO_MANY_ROWS');
WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(sqlerrm);
end test_array;

===========================

Java 调用PL/SQL Returning Object Array

上次写了oracle 存储过程返回数组的方法。这次实现在java调用返回值是数组的存储过程方法。

1. 数组:CYJ_ARRAY

CREATE OR REPLACE TYPE CYJ_ARRAY is VARRAY(20) OF integer

2. 存储过程:getTestArray()

CREATE OR REPLACE FUNCTION getTestArray RETURN CYJ_ARRAY
AS
  l_test CYJ_ARRAY :=CYJ_ARRAY();

   begin
    FOR i IN 1..10 LOOP
      l_test.extend;
      l_test(l_test.count) := i;
      dbms_output.put_line(l_test(l_test.count));
    END LOOP;
    RETURN  l_test;
  END;
3. 调用存储过程的java 类

public class TestOracleReturnArray  {
 

public static void main(String[] args) throws SQLException {
 Connection conn = null;
 CallableStatement cstmt = null;
 ResultSet rs = null;
 try {
  
  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ora","zzz","sss");
  
  conn.setAutoCommit(false);
  String procedure = "{? = call getTestArray() }";// 调用存储过程
  cstmt = conn.prepareCall(procedure);
  cstmt.registerOutParameter(1, OracleTypes.ARRAY,"CYJ_ARRAY"); //注:type name:区分大小,不是包中的type, 使用包中定义的type如何调用还不知道
  
  cstmt.execute();
  ARRAY array = (ARRAY)cstmt.getObject(1);
  Object[] items = (Object[])array.getArray();
  Object obj;
  
  for (int i = 0; i < items.length; i++) {
    obj = items[i];
   
   System.out.println(obj);
   
   } // for
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  //DbConnRestore.connRestore(rs, cstmt, null, conn);
 }
}

}

参考如下资料:

http://www.oracle.com/technology/global/cn/sample_code/tech/java/codesnippet/jdbc/varray/index.html

posted on 2006-11-03 11:24 有猫相伴的日子 阅读(2001) 评论(0)  编辑  收藏 所属分类: pl/sql

只有注册用户登录后才能发表评论。


网站导航:
 
本站不再更新,欢迎光临 java开发技术网