java 调用 oracle 存储过程

	private static String url ="jdbc:oracle:thin:@ip:port:inst";
private static String user="xxx";
private static String password="xxx";
static Connection conn;
static CallableStatement cstat;
@Before
public void init(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn= DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* 只有入参
* a.测试表
* create table test_a(
* name varchar2(32)
* )
* b.测试存储过程
* create or replace procedure pro_a(p1 varchar2)
as
begin
insert into test_a(name)values(p1);
end;
c.执行后查询test_a表有一条数据
* @date 2014-2-8 下午1:53:45
* @author bird
*/
@Test
public void method1(){
String sql ="call pro_a(?)";
try {
cstat = conn.prepareCall(sql);
cstat.setString(1, "bird");
cstat.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally{
destroy();
}
}

/**
* 输出参数
* 》存储过程定义
* create or replace procedure pro_b(p1 in varchar2,p2 out number)
as
begin
select count(name) into p2 from test_a where name = p1;
end;
* @date 2014-2-8 下午2:04:27
* @author bird
*/
@Test
public void method2(){
String sql ="call pro_b(?,?)";
try {
cstat = conn.prepareCall(sql);
cstat.setString(1, "bird");
cstat.registerOutParameter(2, Types.INTEGER);
cstat.execute();
System.out.println(cstat.getInt(2));
} catch (SQLException e) {
e.printStackTrace();
}finally{
destroy();
}
}

/**
* 返回多行记录
* 》存储过程定义
* create or replace procedure pro_c(r_cs out sys_refcursor)
as
begin
open r_cs for select name from test_a;
end;
* @date 2014-2-8 下午2:25:08
* @author bird
*/
@Test
public void method3(){
String sql ="call pro_c(?)";
try {
cstat = conn.prepareCall(sql);
cstat.registerOutParameter(1,OracleTypes.CURSOR);
cstat.execute();
ResultSet rs = (ResultSet) cstat.getObject(1);
while(rs.next()){
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
destroy();
}
}


private void destroy(){
try {
cstat.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值