java程序调用存储过程一(返回特定值)

 
使用非常经典的用户scott用户做例子。

根据empno得到员工的工资是多少,使用存储过程实现。
 
存储过程
create or replace procedure getsalbyid(p_empno in emp.empno%type,p_sal out emp.sal%type) is
v_sal emp.sal%type;
begin
  select sal into v_sal from emp where empno=p_empno;
     if sql%found then
        p_sal:=v_sal;
     else
        v_sal:=-1;
        p_sal:=v_sal;
     end if;
end getsalbyid;

 

 

Java代码:

1.连接Oracle数据库。
public Connection getConnection(){
		Connection conn=null;
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:cissst","scott","tiger");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}

 

2.调用存储过程:
public  void getCallableStatement5(){
		CallableStatement cs=null;
		Connection conn=this.getConnection();
		String sql="{call getsalbyid(?,?)}";
		try {
			conn.setAutoCommit(false);
			cs=conn.prepareCall(sql);
			cs.setInt(1, 7788);
			cs.registerOutParameter(2,Types.INTEGER);
			cs.executeUpdate();
			int id=cs.getInt(2);
			conn.commit();
			System.out.println(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值