Java调用Oracle带结果集返回的存储过程

公司项目中强制使用存储过程,顺势学习下如何在程序中调用过程,借javaeye点地方保存点代码片段..........

创建过程:
--创建一张测试的表
create table t1(a number,b number ,c number,d number);
--创建一个包定义游标类型
create or replace package types
as
type cursorType is ref cursor;
end;
--创建存储过程
create or replace procedure getemps( io_cursor in out types.cursorType)
as
begin
open io_cursor for select * from t1;
end;

借用Javaeye的地方保存点代码:

package org.sf.proc;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;

import oracle.jdbc.driver.OracleTypes;

public class Callprocedure {
public static Connection getConnection() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@172.16.29.39:1521:ORA10G", "fjh", "fjh");
return con;
}
public static void callProcedure(Connection con) throws Exception {

CallableStatement pstm = con.prepareCall("{call GETEMPS(?)}");
pstm.registerOutParameter(1, OracleTypes.CURSOR);
//pstm.setString(2, "30");
pstm.execute();
ResultSet rs = (ResultSet) pstm.getObject(1);

// while (rs.next()) {
// for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
// System.out.print(rs.getString(i) + " ");
// }
// System.out.println();
// }
pstm.close();
rs.close();
}
public static void callSQL(Connection con) throws Exception {
PreparedStatement ps=con.prepareStatement("select * from t1");
//ps.setString(1, "30");
ResultSet rs=ps.executeQuery();
// while (rs.next()) {
// for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
// System.out.print(rs.getString(i) + " ");
// }
// System.out.println();
// }
ps.close();
rs.close();
}
public static void main(String[] args) throws Exception {
Connection con=getConnection();

long s1=System.currentTimeMillis();
callProcedure(getConnection());
long e1=System.currentTimeMillis();

long s2=System.currentTimeMillis();
callSQL(getConnection());
long e2=System.currentTimeMillis();

System.out.println("过程调用耗时:"+(e1-s1));
System.out.println("SQL调用耗时:"+(e2-s2));

//test(con);
}

public static void test(Connection con) throws Exception {
PreparedStatement ps=con.prepareStatement("insert into t1 values(?,?,?,?)");
ps.setInt(1, 1);
ps.setInt(2, 2);
ps.setInt(3, 3);
ps.setInt(4, 4);
for (int i = 0; i < 50000; i++) {
ps.execute();
}
ps.close();
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值