1.無參數無返回值存儲過程調用
--存儲過程
create or replace procedure addEmp_pro
as
begin
insert into emp values(emp_seq.nextval,'Tom',1000);
commit;
end;
//JDBC調用
@Test
public void test0() throws Exception {
Connection conn = getConn();
CallableStatement call = conn.prepareCall("{CALL addEmp_pro()}");
call.execute();
}
2.無參數有返回值
--存儲過程
create or replace procedure addEmp_pro1(eid out emp.id%type)
as
begin
select emp_seq.nextval into eid from dual;
insert into emp values(eid,'Tom',1000);
commit;
end;
//JDBC調用
@Test
public void test0() throws Exception {
Connection conn = getConn();
CallableStatement call = conn.prepareCall("{CALL addEmp_pro1(?)}");
//必須註冊返回值類型
call.registerOutParameter(1, OracleTypes.NUMBER);
call.execute();
int id = call.getInt(1);
System.out.println("添加成功,編號是:"+id);
}
3.無參數返回值爲遊標類型
--存儲過程
create or replace procedure findAllEmp_pro( cur_emp OUT sys_refcursor)
as
begin
open cur_emp for select * from emp;
end;
//JDBC調用
@Test
public void test1() throws Exception {
Connection conn = getConn();
CallableStatement call = conn.prepareCall("{CALL findAllEmp_pro(?)}");
//必須註冊返回值類型
call.registerOutParameter(1, OracleTypes.CURSOR);
call.execute();
//結果集處理
OracleCallableStatement oracleCallableStatement = (OracleCallableStatement) call;
ResultSet rs = oracleCallableStatement.getCursor(1);
List<Emp> emps = new ArrayList<>();
while (rs.next()) {
Emp emp = new Emp();
emp.setId(rs.getInt(1));
emp.setName(rs.getString(2));
emp.setSalary(rs.getDouble(3));
emps.add(emp);
}
for (Emp emp : emps) {
System.out.println(emp);
}
}
4.有參數無返回值
--存儲過程
create or replace procedure addEmp_pro3(ename emp.name%type,esalary emp.salary%type)
as
begin
insert into emp values(emp_seq.nextval,ename,esalary);
commit;
end;
//JDBC調用
@Test
public void test2() throws Exception {
Connection conn = getConn();
CallableStatement call = conn.prepareCall("{CALL addEmp_pro3(?,?)}");
call.setString(1,"Jerry");
call.setDouble(2,2000.0);
call.execute();
}
5.有參數有返回值
--存儲過程
create or replace procedure findEmpById_pro(eid in out number,ename out varchar2,esalary out number)
as
begin
select * into eid,ename,esalary from emp where id=eid;
end;
//JDBC調用
@Test
public void test3() throws Exception {
Connection conn = getConn();
CallableStatement call = conn.prepareCall("{CALL findEmpById_pro(?,?,?)}");
//必須註冊返回值類型
call.registerOutParameter(1, OracleTypes.NUMBER);
call.registerOutParameter(2, OracleTypes.VARCHAR);
call.registerOutParameter(3, OracleTypes.NUMBER);
call.setInt(1,100);
call.execute();
int id = call.getInt(1);
String name = call.getString(2);
double salary = call.getDouble(3);
System.out.println(id+"\t"+name+"\t"+salary);
}
6.有參數返回值爲遊標類型
--存儲過程
create or replace procedure findEmpBySalary_pro(esalary number,cur_emp OUT sys_refcursor)
as
begin
open cur_emp for select * from emp where salary=esalary;
end;
//JDBC調用
@Test
public void test4() throws Exception {
Connection conn = getConn();
CallableStatement call = conn.prepareCall("{CALL findEmpBySalary_pro(?,?)}");
call.setDouble(1,10000.0);
//必須註冊返回值類型
call.registerOutParameter(2, OracleTypes.CURSOR);
call.execute();
//結果集處理
OracleCallableStatement oracleCallableStatement = (OracleCallableStatement) call;
ResultSet rs = oracleCallableStatement.getCursor(2);
List<Emp> emps = new ArrayList<>();
while (rs.next()) {
Emp emp = new Emp();
emp.setId(rs.getInt(1));
emp.setName(rs.getString(2));
emp.setSalary(rs.getDouble(3));
emps.add(emp);
}
for (Emp emp : emps) {
System.out.println(emp);
}
}
附測試數據
drop table emp;
create table emp(
id number(9) primary key,
name varchar2(50),
salary number(9,3)
);
insert into emp select employee_id,first_name,salary from employees;
commit;
select * from emp;