
如下,可以查到DBMS_OUTPUT程序包的内容
也可以在命令行中使用desc关键字直接查看程序包的结构 如下,这种方法相当于简化的文档
下面是PL/SQL实现这个功能
而PL/SQL相对JDBC实现这个更能更有效率 开发模式:SpringMVC+存储过程(PL/SQL)
Hello World
这个dbms_output是一个程序包(类似API)

示例:
赋值有两种方法,":="和"into"关键字
示例:
注意elsif这个写法与Java else if不一样 示例:
-- 判断用户从键盘输入的数字并打印相应语句 -- 接收键盘输入 -- accept addr prompt ‘xxx‘ -- addr 是一个地址值,在该地址上保持了输入的值 accept num prompt ‘请输入一个数字‘; declare pnum number := # -- 注意:取出存在在这个地址值的值要使用‘&‘符号相当于c的指针 begin if pnum = 0 then dbms_output.put_line(‘you input 0‘); else if pnum = 1 then dbms_output.put_line(‘you input 1‘); else then dbms_output.put_line(‘you input other number‘); end if; end;注意,这段正确的代码无法在PL/SQL Developer和命令行中运行,但可以在 Oracle SQL Developer(自行下载)中运行
示例:输入数字1到10
fetch关键字的作用是将光标的一个值赋到变量中并将指针往后移动一位(游标的指针默认在第一个值上) 示例:
示例:
示例:
declare --部门 cursor cdept is select deptno from dept; pdeptno dept.deptno%type; --部门中员工的薪水 cursor cemp(dno number) is select sal from emp where deptno=dno; psal emp.sal%type; --每个段的人数 count1 number; count2 number; count3 number; --部门的工资总额 salTotal number := 0; begin --部门 open cdept; loop --取一个部门 fetch cdept into pdeptno; exit when cdept%notfound; --初始化 count1:=0; count2:=0; count3:=0; --得到部门的工资总额 select sum(sal) into salTotal from emp where deptno=pdeptno; --取部门的中员工薪水 open cemp(pdeptno); loop --取一个员工的薪水 fetch cemp into psal; exit when cemp%notfound; --判断 if psal < 3000 then count1:=count1+1; elsif psal >=3000 and psal<6000 then count2:=count2+1; else count3:=count3+1; end if; end loop; close cemp; --保存结果 insert into msg values(pdeptno,count1,count2,count3,nvl(saltotal,0)); end loop; close cdept; commit; dbms_output.put_line(‘完成‘); end;
编写PL/SQL程序的一般步骤
示例:
导入项目如下
编写工具类
package demo; import java.sql.*; public class JDBCUtils { private static String driver = "oracle.jdbc.OracleDriver"; private static String url = "jdbc:oracle:thin:@169.254.35.157:1521/orcl"; private static String user = "scott"; private static String password = "tiger"; 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 release(Connection conn, Statement st, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally{ rs = null; // 置空作为垃圾回收 } } if (st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } finally{ st = null; } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } finally{ conn = null; } } } }Statement接口的子接口CallableStatement就是专门用于执行SQL存储过程的接口
其中红框内是调用存储过程的标准方式 编写测试类分别测试存储过程和存储函数
测试代码如下
/* procedure queryEmpInformation(eno in number, pename out varchar2, psal out number, pjob out varchar2) */ @Test public void testProcedure() { String sql = "{call queryEmpInformation(?,?,?,?)}"; Connection conn = null; CallableStatement call = null; try { conn = JDBCUtils.getConnection(); call = conn.prepareCall(sql); // 对于in参数,需要对其赋值 call.setInt(1, 7839); // 对于out参数,需要对其声明 call.registerOutParameter(2, OracleTypes.VARCHAR); call.registerOutParameter(3, OracleTypes.NUMBER); call.registerOutParameter(4, OracleTypes.VARCHAR); // 执行调用存储过程 call.execute(); // 从CallableStatement对象中取出输出out参数 String name = call.getString(2); Double sal = call.getDouble(3); String job = call.getString(4); System.out.println("name:" + name + " sal:" + sal + " job:" + job); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.release(conn, call, null); } }输出如下
测试代码如下
/* function queryEmpIncome(eno in number) return number */ @Test public void testFunction() { String sql = "{?= call queryEmpIncome(?)}"; Connection conn = null; CallableStatement call = null; try { conn = JDBCUtils.getConnection(); call = conn.prepareCall(sql); // 存储函数的输出参数即返回值,是第一个问号,对其注册 call.registerOutParameter(1, OracleTypes.NUMBER); // 赋值输入参数 call.setInt(2, 7839); call.execute(); double annIncome = call.getDouble(1); System.out.println("annual income:" + annIncome); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.release(conn, call, null); } }
输出如下
创建包体 在包体中需要实现包头中定义的所有存储过程和存储函数
具体测试代码如下
@Test public void testCursor() { String sql = "{call mypackage.queryemplist(?,?)}"; Connection conn = null; CallableStatement call = null; try { conn = JDBCUtils.getConnection(); call = conn.prepareCall(sql); call.setInt(1, 10); call.registerOutParameter(2, OracleTypes.CURSOR); call.execute(); // CallableStatement是一个通用的接口,获取时获得Oracle适用的实现类对象,可以将其强制转换成Oracle适用的接口 ResultSet cursor = ((OracleCallableStatement) call).getCursor(2); while (cursor.next()) { System.out.println("name:"+cursor.getString("ename")); System.out.println("hiredate:"+cursor.getDate("hiredate")); } } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.release(conn, call, cursor); // 此处关闭ResultSet时同时关闭了这个光标 } }
触发器
数据库触发器是一个 与表相关联的、 存储的PL/SQL程序。每当一个 特定的数据操作语句(Insert,update和delete不包括select)在指定的表上发出时, Oracle自动地执行触发器中定义的语句序列。
示例: 限制非工作时间向数据库插入数据
当非工作时间插入员工执行时会报错
示例2: 确认数据(检查emp表中sal 的修改值不低于原值)
运行效果
S