---
title: "java调用存储过程（返回游标变量，利用索引表，临时表）"
author: "springking"
date: "2009-06-09T18:29:18Z"
tags: []
slug: "java"
draft: false
lastmod: "2009-06-09T18:31:25Z"
id: 405415
url: "https://springking.iteye.com/blog/405415"
---

/\*创建临时表及存储过程\*/

Create Global Temporary Table test1  
(name varchar2(20),age int)  
On Commit Delete Rows;

create or replace package addnum  
is  
  type emp\_table\_type is table of varchar2(200)  
  index by binary\_integer;  
  type emp\_cur is ref cursor;  
end;  
/

create or replace procedure test2  
(abc in out addnum.emp\_table\_type)  
is  
begin  
   abc(0) := 'aaaaa';  
   abc(1) := 20;  
   abc(3) := 'bbbbb';  
   abc(4) := 30;  
end;  
/  
create or replace procedure test3  
(ccc in out addnum.emp\_cur)  
is  
   cba addnum.emp\_table\_type;  
begin  
   test2(cba);  
   insert into test1 values(cba(0),cba(1));  
   insert into test1 values(cba(3),cba(4));  
end;  
/

create or replace procedure test4  
(e\_cur in out addnum.emp\_cur)  
is  
begin  
   test3(e\_cur);  
   open e\_cur for select * from test1;  
end;  
/

 

 

 

 

 

/\*java调用\*/

  Connection conn = DaoUtil.getConnection();  
  conn.setAutoCommit(false);  
    
  CallableStatement call = conn.prepareCall("call test4(?)");  
  call.registerOutParameter(1, OracleTypes.CURSOR);  
  call.execute();  
  ResultSet rs = (ResultSet) call.getObject(1);   
  while(rs.next()){  
   System.out.println(rs.getString(1) + "," + rs.getInt(2));  
  }  
  conn.commit();  
  rs.close();  
  call.close();  
  conn.close();