标签:
--打印hello world create or replace procedure sayhelloworld as --说明部分 begin dbms_output.put_line(‘hello world‘); end; /
编译后:
--连接数据库 C:\WINDOWS\system32>sqlplus scott/tiger@192.168.56.101:1521/orcl SQL>--调用方式一 SQL> set serveroutput on SQL> exec sayhelloworld; hello world PL/SQL 过程已成功完成。 SQL> --调用方式二: SQL> begin 2 sayhelloworld(); 3 sayhelloworld(); 4 end; 5 / hello world hello world PL/SQL 过程已成功完成。
带参数的存储过程:
--给指定员工薪水涨100,并且打印涨前和涨后的薪水 create or replace procedure raiseSalary(eno in number) --in为输入参数 as --说明部分 psal emp.sal%type; begin --得到涨前的薪水 select sal into psal from emp where empno=eno; update emp set sal=sal+100 where empno=eno; --要不要commit? --为保证在同一事务中,commit由谁调用谁提交 dbms_output.put_line(‘涨前:‘||psal||‘ 涨后:‘||(psal+100)); 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; /
测试:
create or replace procedure queryEmpInfo(eno in number, pname out varchar2, psal out number, pjob out varchar2) as begin select ename,sal,empjob into pname,psal,pjob from emp where empno=eno; end;
测试
使用java程序调用存储过程
/* * 存储过程 * create or replace procedure queryEmpInfo(eno in number, * pename out varchar2, * psal out number, * pjob out varchar2) */ @Test public void testProcedure() { // {call <procedure-name>[(<arg1>,<arg2>, ...)]} String sql = "{call queryEmpInfo(?,?,?,?)}"; CallableStatement call = null; Connection connection = JDBCUtils.getConnection(); try { call = connection.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(); //取出结果 String name = call.getString(2); double sal = call.getDouble(3); String job = call.getString(4); System.out.println(name + "\t" + sal + "\t" + job); } catch (SQLException e) { e.printStackTrace(); }finally{ JDBCUtils.release(connection, call, null); } }
使用java程序调用存储函数
/* * 存储函数 * create or replace function queryEmpIncome(eno in number) return number */ @Test public void testFunction() { // {?= call <procedure-name>[(<arg1>,<arg2>, ...)]} String sql = "{?=call queryEmpIncome(?)}"; Connection conn = null; CallableStatement call = null; try { conn = JDBCUtils.getConnection(); call = conn.prepareCall(sql); //对于out参数,赋值 call.registerOutParameter(1, OracleTypes.NUMBER); //对于in参数,赋值 call.setInt(2, 7839); //执行 call.execute(); //取出数据 double income = call.getDouble(1); System.out.println(income); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.release(conn, call, null); } }
CREATE OR REPLACE PACKAGE MYPACKAGE AS type empcursor is ref cursor; --创建存储过程,输出参数为自定义类型 procedure queryEmpList(dno in number,empList out empcursor); END MYPACKAGE;
2、创建包体(实现)
CREATE OR REPLACE PACKAGE BODY MYPACKAGE AS procedure queryEmpList(dno in number,empList out empcursor) AS BEGIN --实现 open empList for select * from emp where deptno=dno; END queryEmpList; END MYPACKAGE;
public void testCursor() { // {call <procedure-name>[(<arg1>,<arg2>, ...)]} String sql = "{call MYPACKAGE.queryEmpList(?,?)}"; Connection conn = null; CallableStatement call = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); call = conn.prepareCall(sql); //对于in参数,赋值? call.setInt(1, 20); //对于out参数,赋值 call.registerOutParameter(2, OracleTypes.CURSOR); //执行 call.execute(); // 取出结果 rs = ((OracleCallableStatement)call).getCursor(2); while(rs.next()){ String name = rs.getString("ename"); double sal = rs.getDouble("sal"); System.out.println(name+"\t"+sal); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.release(conn, call, rs); } }
此案例光标没有关闭,原因:当resultSet关闭的时候 光标就close了
标签:
原文地址:http://www.cnblogs.com/dooor/p/5599351.html