一、调用存储过程
二、设置存储过程需要的参数 包括输入和输出参数
三、注册输出参数 使用Typese指明类型
四、执行存储过程
五、获得返回值
create or replace procedure helloworld(a1 in number,a2 in number,sum out number,sub out number)
as
begin
sum:=a1+a2;
sub:=a1-a2;
end;
/
package test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
import utils.DBUtils;
public class TestCallProcedure {
public static void main(String[] args) throws Exception {
// 用jdbc调用存储过程
Connection con = DBUtils.getConnection();
//创建一个callablestatement 可调用的statement
CallableStatement ctmt = con.prepareCall("call helloworld(?,?,?,?)");
// 设置参数 输入参数
ctmt.setInt(1, 200);
ctmt.setInt(2, 50);
// 注册输出参数
ctmt.registerOutParameter(3, Types.INTEGER);
ctmt.registerOutParameter(4, Types.INTEGER);
ctmt.execute();
int sum = ctmt.getInt(3);//获得第一个输出参数
int sub = ctmt.getInt(4);//获得第二个输出参数
System.out.println(sum);
System.out.println(sub);
con.close();
}
}
转载于:https://blog.51cto.com/nobelking/1419996