先创建包和包体
CREATE OR REPLACE PACKAGE PKG_TEST
IS
TYPE T_CURSOR IS REF CURSOR;
PROCEDURE pro_test(cur OUT T_CURSOR);
FUNCTION fun_test return T_CURSOR;
END;
IS
TYPE T_CURSOR IS REF CURSOR;
PROCEDURE pro_test(cur OUT T_CURSOR);
FUNCTION fun_test return T_CURSOR;
END;
包体
CREATE OR REPLACE PACKAGE BODY PKG_TEST
AS
PROCEDURE pro_test
(cur OUT T_CURSOR)
IS
V_CURSOR T_CURSOR;
BEGIN
insert into tmp_table values(111);
insert into tmp_table values(222);
OPEN V_CURSOR FOR
select * from tmp_table;
cur := V_CURSOR;
commit;
END;
AS
PROCEDURE pro_test
(cur OUT T_CURSOR)
IS
V_CURSOR T_CURSOR;
BEGIN
insert into tmp_table values(111);
insert into tmp_table values(222);
OPEN V_CURSOR FOR
select * from tmp_table;
cur := V_CURSOR;
commit;
END;
function fun_test return T_CURSOR
IS
V_CURSOR T_CURSOR;
BEGIN
OPEN V_CURSOR FOR
select * from tmp_table;
return V_CURSOR;
END;
END;
IS
V_CURSOR T_CURSOR;
BEGIN
OPEN V_CURSOR FOR
select * from tmp_table;
return V_CURSOR;
END;
END;
java代码片段
Connection conn = ....
conn.setAutoCommit(false);
CallableStatement proc = conn.prepareCall("{call ? := pkg_test.fun_test}");
conn.setAutoCommit(false);
CallableStatement proc = conn.prepareCall("{call ? := pkg_test.fun_test}");
proc.registerOutParameter(1, OracleTypes.CURSOR);
proc.execute();
ResultSet rs = (ResultSet) proc.getObject(1);
while(rs.next())
{
proc.execute();
ResultSet rs = (ResultSet) proc.getObject(1);
while(rs.next())
{
//取得结果
}
}
本文介绍了一个Oracle数据库包的创建与使用示例,包括包定义、包体实现及通过Java调用的过程。示例中包含插入操作、游标的打开与返回,以及Java端的调用方式。
731

被折叠的 条评论
为什么被折叠?



