Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

accessing a sys refcursor as a plsql out parameter

user4900730Oct 13 2009 — edited Mar 5 2012
The 11g jdbc guide gives and example of how to access a pl/sql function RETURNING a cursor. We have a stored proc that returns about 5 out parameters one of which is a SYS_REFCURSOR. This is different from RETURNING a ref cursor. Can someone please help me with how to pass a resultset object to the stored proc. I presume the same java declaration is required:

ResultSet cursor;

pass this as a parameter to the stored proc. I have listed the 11g JDBC sample code below:

import oracle.jdbc.*;
...
CallableStatement cstmt;
ResultSet cursor;

// Use a PL/SQL block to open the cursor
********** HIMANSHU reaplce this with the name of my stored proc dennis1.pkg_snapitall ****************
cstmt = conn.prepareCall
("begin open ? for select ename from emp; end;");

cstmt.registerOutParameter(1, OracleTypes.CURSOR);
cstmt.execute();
cursor = ((OracleCallableStatement)cstmt).getCursor(1);

// Use the cursor like a standard ResultSet
while (cursor.next ())
{System.out.println (cursor.getString(1));}
In the preceding 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.
• As always, the output parameter of the callable statement must be registered to define its type. Use the type code OracleTypes.CURSOR for a REF CURSOR.
• The callable statement is run, returning the REF CURSOR.
• 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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 2 2012
Added on Oct 13 2009
2 comments
3,075 views