java 处理oracle 存储过程返回的cursor

本文介绍了一个Java程序示例,展示如何使用CallableStatement来调用Oracle数据库中的存储过程,并处理返回的游标参数。该示例展示了设置输入参数、注册输出游标参数、执行存储过程以及遍历结果集的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.lance.util;


import java.sql.*;


import oracle.jdbc.OracleTypes;


public class QueryProc {
public void callOracleStoredProcCURSORParameter() throws SQLException {


Connection dbConnection = null;
CallableStatement callableStatement = null;
ResultSet rs = null;


String getTRACKED_PROC = "{call TRACKED_PROC(?,?)}";


try {
dbConnection =  OracleDbUtil.init();
callableStatement = dbConnection.prepareCall(getTRACKED_PROC);


callableStatement.setString(1, "mkyong");
callableStatement.registerOutParameter(2, OracleTypes.CURSOR);
// callableStatement.registerOutParameter(1, OracleTypes.CURSOR);


// execute getDBUSERCursor store procedure
callableStatement.executeUpdate();


// get cursor and cast it to ResultSet
rs = (ResultSet) callableStatement.getObject(2);


while (rs.next()) {
String userid = rs.getString("No");
String userName = rs.getString("tobj_status_key");
String createdBy = rs.getString("tobj_type");
String createdDate = rs.getString("status");


System.out.println("No : " + userid);
System.out.println("tobj_status_key : " + userName);
System.out.println("tobj_type : " + createdBy);
System.out.println("status : " + createdDate);
}
System.out.println("nothing");
} catch (SQLException e) {


System.out.println(e.getMessage());


} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {


if (rs != null) {
rs.close();
}


if (callableStatement != null) {
callableStatement.close();
}


if (dbConnection != null) {
dbConnection.close();
}


}


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值