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

编程入门 行业动态 更新时间:2024-10-07 13:18:36

Oracle存储过程:运用<a href=https://www.elefans.com/category/jswz/34/1767796.html style=游标返回结果集的示例2"/>

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

最近调查了关于在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();
}
}
}

更多推荐

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

本文发布于:2024-02-14 09:18:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1762627.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:游标   示例   存储过程   Oracle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!