Java中是如何调用存储过程

//存储过程create or replace Procedure countBySal(
p_sal emp.sal%type,
p_count OUT number
)as
begin
select count(*) into p_count from emp where sal >= p_sql;
end countBySal; //调用步奏import java.sql.CallableStatement; //带哦用存储过程所必须的语句借口
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Types;public class EmpUtil {

public static int countBySal(double sal) throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:test";
Connection cn=DriverManager.getConnection(url, "scott", "tiger");
String sql="{call countBySal(?,?)}";//调用存储过程的语句,call后面的就是存储过程名和需要传入的参数
CallableStatement cst=cn.prepareCall(sql);
cst.setDouble(1, sal);//设置in参数的值
cst.registerOutParameter(2, Types.INTEGER);//注册out参数的类型
cst.execute();
int result = cst.getInt(2);
cst.close();
cn.close();
return result;
}

public static void main(String[] args) {
int count;
try {
count = EmpUtil.countBySal(3000);
System.out.println("工资在3000元以上的人数为:"+count);
} catch (Exception e) {
e.printStackTrace();
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值