熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

在Oracle中使用Java存儲過程

2022-06-13   來源: Java核心技術 

  Java存儲過程今後在各大數據庫廠商中越來越流行功能也越來越強大這裡以Oracle為例介紹一下java存儲過程的具體用法
   如何創建java存儲過程?
    通常有三種方法來創建java存儲過程
    使用oracle的sql語句來創建
    eg 使用create or replace and compile java source named <name> as
    後邊跟上java源程序要求類的方法必須是public static的才能用於存儲過程
    SQL> create or replace and compile java source named javademo
        as
        import javasql*;
        public class JavaDemo
        {
        public static void main(String[] argv)
        {
        Systemoutprintln(hello java demo
        }
      }
      /
   
    Java 已創建
   
    SQL> show errors java source javademo
    沒有錯誤
   
    SQL> create or replace procedure javademo
        as
        language java name JavaDemomain(javalangString[]);
        /
   
    過程已創建
   
    SQL> set serveroutput on
    SQL> call javademo()
   
    調用完成
   
    SQL> call dbms_javaset_output(
   
    調用完成
   
    SQL> call javademo()
    hello java demo
   
    調用完成
   
    SQL> call javademo()
    hello java demo
    調用完成


   使用外部class文件來裝載創建
    eg 這裡既然用到了外部文件必然要將class文件放到oracle Server的某一目錄下邊
    public class OracleJavaProc
    {
        public static void main(String[] argv)
        {
            Systemoutprintln(Its a Java Oracle procedure
        }
    }
   
   
    SQL> grant create any directory to scott;
   
    授權成功
   
    SQL> conn scott/tiger@iiherooracledb
    已連接
    SQL> create or   replace   directory   test_dir   as  d:/oracle;
   
    目錄已創建
   
    SQL> create or replace java class using bfile(test_dir OracleJavaProcCLASS
        /
   
    Java 已創建
   
    SQL> create or replace procedure testjavaproc as language java name OracleJavaProcmain(javalangString[]);
        /
   
    過程已創建
   
    SQL> call testjavaproc()
   
    調用完成
   
    SQL> execute testjavaproc;
   
    PL/SQL 過程已成功完成
   
    SQL> set serveroutput on size
    SQL> call dbms_javaset_output(
   
    調用完成
   
    SQL> execute testjavaproc;
    Its a Java Oracle procedure


我推薦的一種方法直接使用loadjava命令遠程裝載並創建
    先創建一個類 eg
    import javasql*;
    import oraclejdbc*;
   
    public class OracleJavaProc {
   
       //Add a salgrade to the database
       public static void addSalGrade(int grade int losal int hisal) {
   
          Systemoutprintln(Creating new salgrade for EMPLOYEE…
   
          try {
             Connection conn =
                DriverManagergetConnection(jdbc:default:connection:
   
             String sql =
                INSERT INTO salgrade +
                (GRADELOSALHISAL) +
                VALUES(??);
             PreparedStatement pstmt = connprepareStatement(sql)
             pstmtsetInt(grade)
             pstmtsetInt(losal)
             pstmtsetInt(hisal)
             pstmtexecuteUpdate()
             pstmtclose()
             }
          catch(SQLException e) {
             Systemerrprintln(ERROR! Adding Salgrade:
               + egetMessage())
             }
       }
    }
    使用loadjava命令將其裝載到服務器端並編譯
    D:eclipseworkspacedbtest>loadjava u scott/tiger@iiherooracledb v resolve Or
    acleJavaProcjava
    arguments: u scott/tiger@iiherooracledb v resolve OracleJavaProcjava
    creating : source OracleJavaProc
    loading  : source OracleJavaProc
    resolving: source OracleJavaProc
查詢一下狀態
    連接到
    Oraclei Enterprise Edition Release Production
    With the Partitioning OLAP and Oracle Data Mining options
    JServer Release Production
   
    SQL> SELECT object_name object_type status FROM user_objects WHERE object_type LIKE JAVA%;
   
    OBJECT_NAME
   
   
    OBJECT_TYPE                          STATUS
   
    OracleJavaProc
    JAVA CLASS                           VALID
   
    OracleJavaProc
    JAVA SOURCE                          VALID
    測試一下存儲過程
    SQL> create or replace procedure add_salgrade(id number losal number hisal num
    ber) as language java name OracleJavaProcaddSalGrade(int int int);
        /
   
    過程已創建
   
    SQL> set serveroutput on size
    SQL> call dbms_javaset_output(
   
    調用完成
   
    SQL> execute add_salgrade(
    Creating new salgrade for EMPLOYEE…
   
    PL/SQL 過程已成功完成
   
    SQL> select * from salgrade where grade=;
   
         GRADE      LOSAL      HISAL
   
                        


如何更新你已經編寫的java存儲過程?
    假如要往類OracleJavaProc裡添加一個存儲過程方法如何開發?
    正確的步驟應該是先dropjava 改程序再loadjava
    eg修改OracleJavaProc類內容如下
    import javasql*;
    import oraclejdbc*;
   
    public class OracleJavaProc {
   
       // Add a salgrade to the database
       public static void addSalGrade(int grade int losal int hisal) {
   
          Systemoutprintln(Creating new salgrade for EMPLOYEE…
   
          try {
             Connection conn =
                DriverManagergetConnection(jdbc:default:connection:
   
             String sql =
                INSERT INTO salgrade +
                (GRADELOSALHISAL) +
                VALUES(??);
             PreparedStatement pstmt = connprepareStatement(sql)
             pstmtsetInt(grade)
             pstmtsetInt(losal)
             pstmtsetInt(hisal)
             pstmtexecuteUpdate()
             pstmtclose()
             }
          catch(SQLException e) {
             Systemerrprintln(ERROR! Adding Salgrade:
               + egetMessage())
             }
       }
   
       public static int getHiSal(int grade)
       {
        try {
            Connection conn =
              DriverManagergetConnection(jdbc:default:connection:
            String sql = SELECT hisal FROM salgrade WHERE grade = ?;
            PreparedStatement pstmt = connprepareStatement(sql)pstmtsetInt( grade)
            ResultSet rset = pstmtexecuteQuery()
            int res = ;
            if (rsetnext())
            {
                res = rsetgetInt(
            }
            rsetclose()
            return res;
           }
        catch (SQLException e)
        {
            Systemerrprintln(ERROR! Querying Salgrade:
               + egetMessage())
              return ;
        }
       }
   
    }
    如何更新呢?
    D:eclipseworkspacedbtest>dropjava u scott v OracleJavaProc
   
    D:/tiger@iiherooracledbeclipseworkspacedbtest>loadjava u scott v resolve Or
    acleJavaProc/tiger@iiherooracledbjava
    arguments: u scott/tiger@iiherooracledb v resolve OracleJavaProcjava
    creating : source OracleJavaProc
    loading  : source OracleJavaProc
    resolving: source OracleJavaProc
    後邊的應用示例
    SQL> create or replace function query_hisal(grade number) return number as langu
    age java name OracleJavaProcgetHiSal(int) return int;
        /
   
    函數已創建
   
    SQL> set serveroutput on size
    SQL> call dbms_javaset_output(
   
    調用完成
    SQL> select query_hisal() from dual;
   
    QUERY_HISAL(
   
             


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25916.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.