Result VS Cursor


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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值