In Java, a REF CURSOR
is known as a ResultSet
object and can be accessed as shown below:
import oracle.jdbc.*; ... CallableStatement cstmt; ResultSet cursor; // Use a PL/SQL block to open the cursor cstmt = conn.prepareCall ("begin open ? for select sname from student; end;"); cstmt.registerOutParameter(1, OracleTypes.CURSOR); cstmt.execute(); cursor = ((OracleCallableStatement)cstmt).getCursor(1); // Use the cursor like a normal ResultSet while (cursor.next ()) {System.out.println (cursor.getString(1));}
In the above example:
- A CallableStatement object is created by using the
prepareCall
method of the connection class. - The callable statement implements a PL/SQL procedure that returns a
REF CURSOR
. - The output parameter of the callable statement must be registered to define its type. Use the type code
OracleTypes.CURSOR
for aREF CURSOR
. - The callable statement is run, returning the
REF CURSOR
. - The
CallableStatement
object is cast toOracleCallableStatement
to use thegetCursor
method, which is an Oracle extension to the standard JDBC API, and returns theREF CURSOR
into aResultSet
object.