我正在使用返回REF CURSOR
的Java调用PL / SQL中的存储函数:
FUNCTION getApprovers RETURN approvers_cursor IS
approvers approvers_cursor;
BEGIN
OPEN approvers FOR
select * from role where role_name = 'APPROVER';
RETURN approvers;
END;
如果我关闭
RETURN approvers
之前的光标CLOSE approvers;
RETURN approvers;
我在Java中收到警告-
Cursor is Closed
。看来我无法在PL / SQL存储函数中关闭游标。那么在何处关闭光标?
最佳答案
实际上,ResultSet
是游标。
//JAVA //PL/SQL
PreparedStatement ps = con.prepareStatement(
"select * from role where role_name = ?");
ps.setString(1, "APPROVER");
ResultSet rs = ps.executeQuery(); //OPENS the cursor
while(rs.next()) { //FETCHES next row from cursor
//Handle resultset
}
rs.close(); //CLOSES the cursor
自Java 7起,您可以使用try-with-resource进行此操作,并隐藏close()调用,这是有原因的-但为了清楚起见,与此处编写的旧样式相同