歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 在Java語言中調用存儲函數

在Java語言中調用存儲函數

日期:2017/3/1 10:25:58   编辑:Linux編程

連接Oracle數據庫

  1. private static Connection conn;
  2. static{
  3. //第一步:加載驅動
  4. try {
  5. Class.forName("oracle.jdbc.driver.OracleDriver");
  6. //得到連接對象 conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","scott");
  7. } catch (ClassNotFoundException e) {
  8. // TODO Auto-generated catch block
  9. e.printStackTrace();
  10. } catch (SQLException e) {
  11. // TODO Auto-generated catch block
  12. e.printStackTrace();
  13. }
  14. }

實例一:

  1. create or replace function sumSal(emp_no number)--function(參數的值 必須有類型)
  2. --返回值類型
  3. return number--必須有返回值
  4. as
  5. --聲明變量
  6. emp_sal emp.sal%type;
  7. emp_comm emp.comm%type;
  8. total emp.sal%type;
  9. begin
  10. select sal,comm into emp_sal,emp_comm from emp where empno=emp_no;
  11. total:=emp_sal*12+nvl(emp_comm,0);
  12. return total;--必須返回 返回值類型一定相同
  13. end;
  14. public static void functionTest1() throws SQLException{
  15. //mypackage 存儲函數
  16. CallableStatement cas=conn.prepareCall("{?=call sumSal(?)}");
  17. //從1開始
  18. int index = 1;
  19. cas.registerOutParameter(index++, oracle.jdbc.OracleTypes.NUMBER);
  20. //為占位符賦值
  21. cas.setInt(index++,7369);
  22. boolean flag=cas.execute();
  23. System.out.println(flag);
  24. System.out.println(cas.getInt(1));
  25. }
Copyright © Linux教程網 All Rights Reserved