create or replace procedure PrintStudents(p_staffName in xgj_test.username%type) as
cursor c_testData is
select t.sal, t.comm from xgj_test t where t.username = p_staffName;
begin
for v_info in c_testData loop
DBMS_OUTPUT.PUT_LINE(v_info.sal || ' ' || v_info.comm);
end loop;
end PrintStudents;
一旦创建了改程序并将其存储在数据库中,就可以使用如下的方式调用该过程
begin
PrintStudents('Computer Science');
PrintStudents('Match');
end;
/
create [ or replace] procedure procedure_name
[( argument [ {IN | OUT | IN OUT }] type,
......
argument [ {IN | OUT | IN OUT }] type ) ] { IS | AS}
procedure_body
无参的存储过程
/**
无参数的存过
打印hello world
调用存储过程:
1. exec sayhelloworld();
2 begin
sayhelloworld();
end;
/
*/
create or replace procedure sayhelloworld
as
--说明部分
begin
dbms_output.put_line('hello world');
end sayhelloworld;
调用过程:
SQL> set serveroutput on ;
SQL> exec sayhelloworld();
hello world
PL/SQL procedure successfully completed
SQL> begin
2 sayhelloworld();
3 sayhelloworld();
4 end;
5 /
hello world
hello world
PL/SQL procedure successfully completed
带参数的存储过程
/**
创建一个带参数的存储过程
给指定的员工增加工资,并打印增长前后的工资
*/
create or replace procedure addSalary(staffName in xgj_test.username%type )
as
--定义一个变量保存调整之前的薪水
oldSalary xgj_test.sal%type;
begin
--查询员工涨之前的薪水
select t.sal into oldSalary from xgj_test t where t.username=staffName;
--调整薪水
update xgj_test t set t.sal = sal+1000 where t.username=staffName ;
--输出
dbms_output.put_line('调整之前的薪水:'|| oldSalary || ' ,调整之后的薪水:' || (oldSalary + 1000));
end addSalary;
可以看到,update语句之后并没有commit的操作。
一般来讲为了保证事务的一致性,由调用者来提交比较合适,当然了是需要区分具体的业务需求的~
begin
addSalary('xiao');
addSalary('gong');
commit ;
end ;
/
存储函数 基本语法
create [ or replace] function function_name
[( argument [ {IN | OUT | IN OUT }] type,
......
argument [ {IN | OUT | IN OUT }] type ) ]
RETURN { IS | AS}
function_body
/**
查询员工的年薪 (月工资*12 + 奖金)
*/
create or replace function querySalaryInCome(staffName in varchar2)
return number as
--定义变量保存员工的工资和奖金
pSalary xgj_test.sal%type;
pComm xgj_test.comm%type;
begin
--查询员工的工资和奖金
select t.sal, t.comm
into pSalary, pComm
from xgj_test t
where t.username = staffName;
--直接返回年薪
return pSalary * 12 + pComm;
end querySalaryInCome;
create or replace function querySalaryInCome(staffName in varchar2)
return number as
--定义变量保存员工的工资和奖金
pSalary xgj_test.sal%type;
pComm xgj_test.comm%type;
begin
--查询员工的工资和奖金
select t.sal, t.comm
into pSalary, pComm
from xgj_test t
where t.username = staffName;
--直接返回年薪
return pSalary * 12 + nvl(pComm,0);
end querySalaryInCome;
/**
根据员工姓名,查询员工的全部信息
*/
create or replace procedure QueryStaffInfo(staffName in xgj_test.username%type,
pSal out number,
pComm out xgj_test.comm%type,
pJob out xgj_test.job%type)
is
begin
--查询该员工的薪资,奖金和职位
select t.sal,t.comm,t.job into pSal,pComm,pJob from xgj_test t where t.username=staffName;
end QueryStaffInfo;
classes12.jar (1,600,090 bytes) - for use with JDK 1.2 and JDK 1.3
classes12_g.jar (2,044,594 bytes) - same as classes12.jar, except that classes were compiled with “javac -g” and contain some tracing information.
classes12dms.jar (1,607,745 bytes) - same as classes12.jar, except that it contains additional code`to support Oracle Dynamic Monitoring Service.
classes12dms_g.jar (2,052,968 bytes) - same as classes12dms.jar except that classes were compiled with “javac -g” and contain some tracing information.
ojdbc14.jar (1,545,954 bytes) - classes for use with JDK 1.4 and 1.5
ojdbc14_g.jar (1,938,906 bytes) - same as ojdbc14.jar, except that classes were compiled with “javac -g” and contain some tracing information.
ojdbc14dms.jar (1,553,561 bytes) - same as ojdbc14.jar, except that it contains additional code`to support Oracle Dynamic Monitoring Service.
ojdbc14dms_g.jar (1,947,136 bytes) - same as ojdbc14dms.jar, except that classes were compiled with “javac -g” and contain some tracing information.
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import org.junit.Test;
import com.turing.oracle.dbutil.DBUtils;
import oracle.jdbc.OracleTypes;
public class TestProcedure {
@Test
public void callProcedure(){
// {call <procedure-name>[(<arg1>,<arg2>, ...)]}
Connection conn = null ;
CallableStatement callableStatement = null ;
/**
*
根据员工姓名,查询员工的全部信息
create or replace procedure QueryStaffInfo(staffName in xgj_test.username%type,
pSal out number,
pComm out xgj_test.comm%type,
pJob out xgj_test.job%type)
is
begin
--查询该员工的薪资,奖金和职位
select t.sal,t.comm,t.job into pSal,pComm,pJob from xgj_test t where t.username=staffName;
end QueryStaffInfo;
*/
// 我们可以看到该存过 4个参数 1个入参 3个出参
String sql = "{call QueryStaffInfo(?,?,?,?)}";
try {
// 获取连接
conn = DBUtils.getConnection();
// 通过连接获取到CallableStatement
callableStatement = conn.prepareCall(sql);
// 对于in 参数,需要赋值
callableStatement.setString(1, "xiao");
// 对于out 参数,需要声明
callableStatement.registerOutParameter(2, OracleTypes.NUMBER); // 第二个 ?
callableStatement.registerOutParameter(3, OracleTypes.NUMBER);// 第三个 ?
callableStatement.registerOutParameter(4, OracleTypes.VARCHAR);// 第四个 ?
// 执行调用
callableStatement.execute();
// 取出结果
int salary = callableStatement.getInt(2);
int comm = callableStatement.getInt(3);
String job = callableStatement.getString(3);
System.out.println(salary + "\t" + comm + "\t" + job);
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtils.cleanup(conn, callableStatement, null);
}
}
}
在应用程序中访问存储函数
根据官方提供的API,我们可以看到:
import java.sql.CallableStatement;
import java.sql.Connection;
import org.junit.Test;
import com.turing.oracle.dbutil.DBUtils;
import oracle.jdbc.OracleTypes;
public class TestFuction {
@Test
public void callFuction(){
//{?= call <procedure-name>[(<arg1>,<arg2>, ...)]}
Connection conn = null;
CallableStatement call = null;
/**
* create or replace function querySalaryInCome(staffName in varchar2)
return number as
--定义变量保存员工的工资和奖金
pSalary xgj_test.sal%type;
pComm xgj_test.comm%type;
begin
--查询员工的工资和奖金
select t.sal, t.comm
into pSalary, pComm
from xgj_test t
where t.username = staffName;
--直接返回年薪
return pSalary * 12 + nvl(pComm,0);
end querySalaryInCome;
*/
String sql = "{?=call querySalaryInCome(?)}";
try {
// 获取连接
conn = DBUtils.getConnection();
// 通过conn获取CallableStatement
call = conn.prepareCall(sql);
// out 参数,需要声明
call.registerOutParameter(1, OracleTypes.NUMBER);
// in 参数,需要赋值
call.setString(2, "gong");
// 执行
call.execute();
// 取出返回值 第一个?的值
double income = call.getDouble(1);
System.out.println("该员工的年收入:" + income);
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtils.cleanup(conn, call, null);
}
}
}
在out参数中访问光标 在out参数中使用光标
我们之前抛出的两个思考问题:
查询员工的所有信息–> out参数太多怎么办?
查询某个部门中所有员工的信息–> out中返回集合?
我们可以通过返回Cursor的方式来实现。
在out参数中使用光标 的步骤:
申明包结构
包头
包体
包头:
create or replace package MyPackage is
-- Author : ADMINISTRATOR
-- Created : 2016-6-4 18:10:42
-- Purpose :
-- 使用type关键字 is ref cursor说明是cursor类型
type staffCursor is ref cursor;
procedure queryStaffJob(pJob in xgj_test.job%type,
jobStaffList out staffCursor);
end MyPackage;
创建完包头之后,创建包体,包体需要实现包头中声明的所有方法。
包体
create or replace package body MyPackage is
procedure queryStaffJob(pJob in xgj_test.job%type,
jobStaffList out staffCursor)
as
begin
open jobStaffList for select * from xgj_test t where t.job=pJob;
end queryStaffJob;
end MyPackage;