jdbc调用pl/sql存储过程

来源:互联网 发布:xampp打不开apache 编辑:程序博客网 时间:2024/05/29 04:39

建立程序包的包头:

Create or replace Package WesleyPackage
   as
     Type Test_CURSOR IS REF CURSOR;
     Procedure SelectCustAcctLogByCustId( CustId in acct.cust_acct_log.cust_id%type , p_Cursor out WesleyPackage.Test_CURSOR );
End WesleyPackage;

 

建立程序包体:

--带有out参数的存储过程必须通过java jdbc的CallableStatement调用方式打印输出结果
--存储过程的参数不能为 ibatis中的 po对象

Create or replace Package Body WesleyPackage as
   Procedure SelectCustAcctLogByCustId( CustId in acct.cust_acct_log.cust_id%type , p_Cursor out WesleyPackage.Test_CURSOR ) as  --注意没有Create  
   Begin
      open p_Cursor for select * from acct.viw_cust_acct_log where cust_id = CustId ;
   End SelectCustAcctLogByCustId;  --结束查询过程
End  WesleyPackage;--结束包

 

以下存储过程为方便调试而建:

create or replace procedure acct.SelectCustAcctLogByCustId( CustId in acct.cust_acct_log.cust_id%type )
as
cursor ret_list is select * from acct.viw_cust_acct_log where cust_id = CustId;

begin
  for S in ret_list loop
 
    dbms_output.put_line( '账务日期['||S.acct_date||'] 账务流水号['||S.acct_seq_id||']' );
 
  end loop;
end;

调用上面的存储过程

set serveroutput on;
declare
cust_id acct.cust_acct_log.cust_id%type;
begin
    cust_id := '000001006583';
    acct.SelectCustAcctLogByCustId(cust_id);
end;

 

 

 

通过jdbc方式调用存储过程:

   /** 调用存储过程 开始 */

   Connection con = getConnection('m');
   CallableStatement proc = null;
   proc = con.prepareCall("{ call acct.WesleyPackage.SelectCustAcctLogByCustId(?,?) }");
   
   String custId = "000001006583";
   proc.setString(1, custId);
   proc.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
   proc.execute();
   rs = (ResultSet) proc.getObject(2);
   /** 调用存储过程 结束 */
   
   // Iterate through the data in the result set and display it.
   while (rs.next())
   {            
    log.info(rs.getString(1) + " " + rs.getString(2));         
   }

 

 

 

      在大多数情况下,存储过程中的所有输入参数(in 参数)可能仅属于一个vo 对象;基于封装与简化的思想, 所以我想找到一种java语言下的java Bean对象与PL/SQL中的数据类型对应的方案,可惜没有找到. 如果哪位大牛知道请一定告诉我.

原创粉丝点击