create or replace procedure get_authors_cur(
cv_results in out sys_refcursor)
is
begin
open cv_results for
select id, first_name, last_name
from authors;
end;
这个oracle存储过程返回一个cursor类型变量。在下面的程序中,我们将从java中引用这个cursor(在java中是ResultSet)
package dev.cxz.oracle;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleCallableStatement;
public class CursorResultSet {
/**
* @param args
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws SQLException,
ClassNotFoundException {
new CursorResultSet().text();
}
private void text() throws SQLException, ClassNotFoundException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "plsql", "oracle");
String callProc = "begin get_authors_cur(?); end;";
CallableStatement call = conn.prepareCall(callProc);
call.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
call.execute();
ResultSet cursor = ((OracleCallableStatement) call).getCursor(1);
while (cursor.next()) {
System.out.println(cursor.getString("first_name") + ", "
+ cursor.getString("last_name"));
}
}
}
运行结果:
[quote]Marlene, Theriault
Rachel, Carmichael
James, Viscusi
Michael, Abbey
Michael, Corey
Gaja, Vaidyanatha
Kirtikumar, Deshpande
John, Kostelac
Ian, Abramson
Kenny, Smith
Stephan, Haisley
Lars, Bo
Dirk, Schepanek
Christopher, Allen
David, James
Graham, Seibert
Simon, Russell
Bastin, Gerald
Nigel, King
Dan, Natchek
Steve, Vandivier
Kelly, Cox
Venkat, Devraj
Peter, Koletzke
Paul, Dorsey
Scott, Urman
Kevin, Loney
Sumit, Sarin
George, Koch
Michael, Armstrong-Smith
Darlene, Armstrong-Smith
Rama, Velpuri
Anand, Adkoli
Aaron, Newman
Donald, Burleson
Brad, Brown
Jason, Price
Robert, Freeman
Matthew, Hart
Dave, Faulkner
Mark, Scardina
Ben, Chang
George, Hardman
Jinyu, Wang
Scott, Boudreaux
Wim, Coekaerts
Ron, Hardman
Mike, McLaughlin
David, Knox
Scott, Jesse
Tom, Doebler
Rich, Niemic
Joe, Trezzo
[/quote]