标签:
/*
第一个存储过程
调用存储过程:
方式一:exec sayHello();
方式二:begin
sayHello();
end;
*/
create or replace procedure sayHello
as
--变量的说明
begin
--程序体
dbms_output.put_line(‘Hello World‘);
end;
/*
存储过程示例:为指定的职工在原工资的基础上涨10%的工资,并打印涨工资前和涨工资后的工资
需要指明传递的参数:为输入参数还是输出参数?
一般不再存储过程或者存储函数中 commit rollback
*/
create or replace procedure raiseSalary(eno in number)
as
--记录该员工的涨前的工资
psal emp.sal%type;
begin
--得到给员工的工资
select sal into psal from emp where empno=eno;
--涨工资
update emp set sal=sal*1.1 where empno=eno;
--打印
dbms_output.put_line(‘涨前工资:‘||psal||‘涨后工资:‘||(psal*1.1));
end;
/*
CREATE [OR REPLACE]FUNCTION 函数名(参数列表)
RETURN 函数值类型
AS
PLSQL子程序体;
存储函数示例:查询某个员工的薪水
调用存储函数
declare
psal emp.sal%type;
begin
psal:=queryEmpSalary(7839);
dbms_output.put_line(psal);
dbms_output.put_line(queryEnpSalary(7839));
end;
*/
create or replace function queryEmpSalary(eno in number)
return number
as
--记录该员工的薪水
psal emp.sal%type;
begin
select sal into psal from emp where empno=eno;
retrun psal;
end;
/*
查询某职工的年收入
*/
create or replace function queryEmpIncome(eno in number)
return number
as
psal emp.sal%type;
pcomm emp.comm%type;
begin
select sal,comm into psal,pcomm from emp where
empno=eno;
return psal*12 + nvl(pcomm,0);
end;
/*
out参数:查询某个员工的姓名,月薪和职位使用存储函数完成;
*/
create or replace procedure queryEmpInfo(
eno in number,
pename out varchar2,
psal out number,
pjob out varchar2
)
as
begin
select ename,sal,empjob into pename,psal,pjob from
emp where empno=eno;
end;
/*
JDBCUtill
*/
public class JDBCUtill{
private static String driver="oracle.jdbc.OracleDriver";
private Static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static String user="";
private static String password = "";
static{
try{
Class.forName(driver);
}catch(ClassNotFoundException e){
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection(){
try{
return DriverManager.getConnection(url,user,password);
}catch(SQLException e){
e.printStackTrace();
}
return null;
}
//关闭
public static void ......
}
/*
oracleCallableStatement
*/
public class TestDemo{
/*
调用存储过程
procedure queryEmpInfo(eno in number,pename out varchar2,psal out number,pjob out varchar2)
*/
@Test
public void testProcedure(){
String sql = "{call queryEmpInfo(?,?,?,?)}";
Connection conn = null;
CallableStatement call = null;
try{
conn = JDBCUtils.getConnection();
call = conn.prepareCall(sal);
//赋值
call.setInt(1,7839);
//对于输出参数,不需要赋值,但需要声明
call.registerOutParameter(2,OracleTypes.VARCHAR);
call.registerOutParameter(3,OracleTypes.NUMBER);
call.registerOutParameter(4,OracleTypes.VARCHAR);
//执行
call.execute();
//取出结果
String name = call.getString(2);
double sal = call.getDouble(3);
String job = call.getString(4);
...
}catch(exception){
e.printStackTrace();
}finally{
//关闭
}
}
/*
调用存储函数
*/
@Test
public void testFunction(){
String sql = "{?=call queryEmpIncome(?)}";
Connection conn = null;
tyr{
conn = JDBCUtills.getConnection();
call = conn.prepareCall(sql);
//赋值
call.registerOutParameter(1,OracleTypes.NUMBER);
call.setInt(2,7839);
//执行
call.execute();
double income = call.getDouble();
}
...
}
//调用out参数中有光标的存储过程
@Test
public void testCursor(){
...
tResultSet rs = nell;
String sql = "{call mypackage.queryEmpList(?,?)}";
call=conn.prepareCall(sql);
//赋值
call.setInt(1,20);
call.registerOutParameter(2,OracleTypes.CURSOR);
//执行
call.execute();
//如何取结果
rs =((OracleCallableStatement)call).getCursor(2);
while(rs.next()){
String ename = rs.getString("ename");
double sal = rs.getDouble("sal");
.....
}
}
}
--第一个触发器:对标进行insert时,触发打印HELLO WORLD
create or replace trigger sayHelloWorld
after insert
on emp
begin
dbms_output.put_line(‘HELLO WORLD‘);
end;
/*
触发器的应用场景一(语句触发器):实施复杂的安全性检查
限制非工作时间向数据库插入数据
非工作时间:
周末:to_char(sysdate,‘day‘)in(‘星期六‘,‘星期日‘)
<9点或者>18点:to_number(to_char(sysdate,‘hh24‘)) not
between 9 and 18
*/
create or replace trigger securityEmp
before insert
on emp
begin
if(to_char(sysdate,‘day‘)in(‘星期六‘,‘星期日‘))or (to_number(to_char(sysdate,‘hh24‘)) not betwween 9 and 18) then
raise_application_error(-20001,‘不能在非工作时间插入数据‘);
end if;
end;
/*
触发器的应用场景二(行级触发器):数据的确认
涨工资不能越涨越少
*/
create or replace trigger checkSal
before update
on emp
for each now
begin
if:new.dal<:old.sal then
raise_application_error(-20002,‘涨后的工资不能比涨前少,涨前:‘||:old.sal||‘涨后‘||:new.sal);
end if;
end;
标签:
原文地址:http://www.cnblogs.com/chentaiyan/p/4450431.html