Minimum Deletion Cost to Avoid Repeating LettersMinimum Deletion Cost to Avoid Repeating Letters

How to access REF CURSOR Data in Java ?

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:

  1. A CallableStatement object is created by using the prepareCall method of the connection class.
  2. The callable statement implements a PL/SQL procedure that returns a REF CURSOR.
  3. The output parameter of the callable statement must be registered to define its type. Use the type code OracleTypes.CURSOR for a REF CURSOR.
  4. The callable statement is run, returning the REF CURSOR.
  5. The CallableStatement object is cast to OracleCallableStatement to use the getCursor  method, which is an Oracle extension to the standard JDBC API, and returns the REF CURSOR into a ResultSet object.

Join Discussion

This site uses Akismet to reduce spam. Learn how your comment data is processed.