指存儲在數據庫中的供所有用戶程序帶哦用的子程序(PL/SQL)叫存儲過程(不能返回值)、存儲函數(可以通過return語句返回值)
1、存儲過程
為了完成特定功能的SQL語句集,經編譯后存儲在數據庫中。
(1)新建:
(2)書寫存儲過程的代碼:
create or replace procedure raiseSalary(eno in number) is psal emp.sal%type; begin select sal into psal from emp where empno=eno; update emp set sal= sal + 100 where empno = eno ; dbms_output.put_line('前:'||psal||'后:'||(psal+100)); end raiseSalary;
(3)編譯運行代碼:
(4)調用存儲過程:
2、存儲函數
存儲函數與存儲過程的結構類似,但是必須有一個return子句,用於返回函數值。
(1)創建一個存儲函數:
(2)書寫代碼:
create or replace function queryEmpIncome(eno in number) return number is 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 queryEmpIncome;
(3)右鍵選擇test:
3、存儲過程和存儲函數的OUT
(1)創建存儲過程:
(2)書寫程序:查詢員工的信息
create or replace procedure queryEmpInfeno(eno in number, pename out varchar2, psal out number, pjob out varchar2) is begin select ename,sal,job into pename,psal,pjob from emp where empno=eno; end queryEmpInfeno;
(3)測試結果:
4、java程序調用存儲過程和存儲函數
(1)先在虛擬機中找到需要導入的jar包並進行導入:
(2)書寫一個工具類:
package pers.zhb.utils; import java.sql.*; public class JDBCUtils { private static String driver = "oracle.jdbc.OracleDriver"; private static String url = "jdbc:oracle:thin:@192.168.125.129: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; } } } }
(3)創建測試類,調用存儲過程和存儲函數:
public class Test { public void testProcedure(){ String sql = "{call raiseSalary(?,?,?,?)}"; Connection conn = null; CallableStatement call = null; try { conn = JDBCUtils.getConnection(); call = conn.prepareCall(sql); 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); System.out.println(name+"\t"+sal+"\t"+job); } catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.release(conn, call, null); } } 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 income = call.getDouble(1); System.out.println(income); } catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.release(conn, call, null); } } public static void main(String [] args){ Test test=new Test(); test.testFunction(); } }