-- 判斷用戶從鍵盤輸入的數字並打印相應語句 -- 接收鍵盤輸入 -- 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;
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;
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; } } } }
/* 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); } }
輸出以下java
@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時同時關閉了這個光標 } }
觸發器sql