Oracle存储过程:运用游标返回结果集的示例2

本文介绍如何在Java中调用Oracle存储过程并获取记录集返回结果。通过具体实例展示了创建Oracle存储过程的方法及在Java中使用CallableStatement执行存储过程的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近调查了关于在Java中调用Oracle的存储过程,取得返回纪录集的情况,调查了半天作了如下一个例子,仅供今后或别人参考:

1)创建Function

create or replace package arsweb as
type refcursor is ref cursor;
function getresultset(key in varchar2) return refcursor;
end;
/
create or replace package body arsweb is
function getresultset (key in varchar2) return refcursor
is
v_temp refcursor;
begin
open v_temp for
select tname, tabtype, clusterid from tab where tname like '%'|| key;
return v_temp;
end getresultset;
end;
/

2)在Java中调用Oralce的存储过程:

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.jdbc.OracleTypes;
import oracle.jdbc.driver.OracleCallableStatement;


public class PLSQLFuncationCall {

public static void main(String[] args) throws Exception {
PLSQLFuncationCall.sendEarlyDeaths();
}
static void sendEarlyDeaths() throws Exception
{
Connection con = null;
CallableStatement toesUp = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:yochi", "system", "system");

con.setAutoCommit(false);

// Setup the call.
toesUp = con.prepareCall("{ ? = call ARSWEB.GetResultSet(?) }");
toesUp.setString(2, "T");
toesUp.registerOutParameter(1,OracleTypes.CURSOR);
toesUp.execute();
// Execute the Call

ResultSet rs = ((OracleCallableStatement)toesUp).getCursor(1);
while (rs.next())
{
String name = rs.getString(1);
String type = rs.getString(2);
System.out.println("Name="+name + "/tType=" + type);
}
rs.close();
}
catch (SQLException e)
{
// We should protect these calls.
e.printStackTrace();
toesUp.close();
con.close();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值