Using Java Class in Oracle Database



1. create and compile a Java Class in Oracle Database

Create the MyTest class. MyTest has two public static methods. 
--------------------------------------------
create or replace and compile java source named test as 
  public class MyTest{ 
    public static void myProc(int a,int b,int[] ret){
      ret[0]=a+b;
    }
    public static int myFunc(int a,int b){
      return a+b; 
    }
  }
/ 
--------------------------------------------

2. Create a procedure/function in Oracle database
2.1 Create a procedure

----------------------------------------------
CREATE OR REPLACE PROCEDURE myProc(a in number, b in number, ret out number) AS
   LANGUAGE java NAME 'MyTest.myProc(int,int,int[])';
 /
-----------------------------------------------
2.2 Create a function
-----------------------------------------------
CREATE OR REPLACE FUNCTION myFunc(a in number, b in number) RETURN NUMBER IS
    LANGUAGE java NAME 'MyTest.myFunc(int,int) return int';
 /
-----------------------------------------------
 3. Invoke
-----------------------------------------------
3.1 Invoke a procedure 
----------------------------------------
set serveroutput on

DECLARE a INTEGER;
BEGIN
   myProc(1, 2, a);
  DBMS_OUTPUT.PUT_LINE(a);
END; 
/
----------------------------------------
 3.2 Invoke a function
---------------------------------------  
select myFunc(1,2) from dual;
---------------------------------------

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE