How to return JDBC resultset as REF cursor from the Java stored procedure
Date: January 1, March 3, 2003
After reading this document, you should be able:
Introduction
This document demonstrates howJDBC ResultSet
AsREF CURSOR
.JDBC ResultSet
Is a data table that represents a database. It is usually generated by executing a database query statement.REF CURSOR
YesPL/SQL
. The call specification of the Java stored procedure maps the resultsetREF CURSOR
. In oracle9IPreviously, it was impossible to directly returnResultSet
Because no form is definedResultSet->REF CURSOR
. Oracle9IAdded this ing to allow return from the functionResultSet
Or upload it as an out parameter to a process. However, it still does not support reverse ing (REF CURSOR->ResultSet
), So the current version of the database still does not support the in and in out parameters.
In this method guide, we have two Java stored procedures. Java stored proceduregetEmployees()
SetSCOTT
In ModeEMP
Mount all columns in the tableResultSet
And return it. Java stored proceduregetDepartments(ResultSet[] rout)
SetResultSet
Object as the out parameter andDEPT
All columns in the table are mounted to thisResultSet
Object.
Software requirements
Description
First, you must create a Java stored procedure. The following is a code snippet. Click here to view the complete Java code.
In the Java stored procedure, you can obtain the default server-side connection to the database. By default, statements created by the server-side JDBC driver cannot be convertedREF CURSOR
OfResultSet
Object. Use resultsetREF CURSOR
Returned, must be created in a special wayStatement
OrPreparedStatement
Before creating the statement, you mustConnection
CallsetCreateStatementAsRefCursur(true)
. If you cannot create a resultset and use itREF CURSOR
Call before returningsetCreateStatementAsRefCursor(true)
, The following error message is displayed when the Java stored procedure is executed:
The ORA-00932: inconsistent PES ypes.
OnceConnection
The object has been called.setCreateStatementAsRefCursor(true)
, All returned by the query for this connectionResultSet
Can be convertedREF CURSOR
.
Java stored procedure list 1
public static ResultSet getEmployees() { ..........................// Obtain default connection Connection conn = new OracleDriver().defaultConnection(); // Create any subsequent statements as a REF CURSOR ((OracleConnection)conn).setCreateStatementAsRefCursor(true);// Create the statementStatement stmt = conn.createStatement();// Query all columns from the EMP tableResultSet rset = stmt.executeQuery("select * from emp");// Return the ResultSet (as a REF CURSOR) return rset; ........... ........... |
|
In the preceding Java stored proceduresetCreateStatementAsRefCursor(true)
By default, the server connection is set to return the resultset that can be converted to ref cursor. Then QueryEMP
All rows in the table and return resultset.
Java stored procedure list 2
public static void getDepartments( ResultSet[] rout ) {........................// Obtain the default connection Connection conn = new OracleDriver().defaultConnection();// Create any subsequent statements as a REF CURSOR ((OracleConnection)conn).setCreateStatementAsRefCursor(true);// Create the statement Statement stmt = conn.createStatement();// do a simple query ResultSet rset = stmt.executeQuery("select * from dept");// return the ResultSet (as a REF CURSOR) rout[0] = rset; ........... ........... |
|
The above Java stored proceduregetDepartments( ResultSet[] rout )
QueryDEPT
All rows in the table are returned in the out parameter.ResultSet
.
To view the application running, load the Java class to your databaseSCOTT
Mode.
> Loadjava-thin-user Scott/tiger @
Where:
<Hostname> |
Host Name for Database Installation |
<Port> |
Database TNS Listener Port |
<Sid> |
Database Name |
For example:
> Loadjava-thin-user Scott/tiger@insn104a.idc.oracle.com: 1521: otn9idb-resolve-verbose refcursor. Java
Create a Java stored procedure call specification. ConnectSCOTT/TIGER
Run the following code at the SQL prompt:
create or replace package refcurpkg istype refcur_t is ref cursor;end refcurpkg;/create or replace function getemps return refcurpkg.refcur_t islanguage java name 'RefCursor.getEmployees() return java.sql.ResultSet'; /create or replace procedure getdepts(cur OUT refcurpkg.refcur_t) islanguage java name 'RefCursor.getDepartments(java.sql.ResultSet[])';/ |
|
In the preceding listrefcurpkg
Creates a new typeREF CURSOR
Type. Create a Java stored procedure call specification. ReturnREF CURSOR
Call Specificationgetemployees
Publish a Java stored proceduregetEmployees()。
Stored Procedure by JavagetEmployees()
ReturnedResultSet
MapREF CURSOR
.
Call Specificationgetdepts
Publish a Java stored proceduregetDepartments
And set the out ParameterREF CURSOR
MapResultSet
Now you can test the Java stored procedure by executing the following code at the SQL prompt:
SQL> variable X refcursor
SQL> execute: = getemps;
SQL> Print x
This operation will printSCOTT
In ModeEMP
Table content.
SQL> execute getdepts (: X );
SQL> Print x
This operation will printSCOTT
In ModeDEPT
Table content.
Resources
- The Java stored procedure example can be obtained at/global/CN/sample_code/Tech/Java/JSP/index.html.
- The White Paper on developing Java stored procedures can be obtained at/Tech/Java/JSP/pdf/stored_procs_in_java_twp.pdf.
Summary
This document explains how to createJDBC ResultSet
AsREF CURSOR
The returned Java stored procedure, and how to write calling specifications for the Java stored procedure.