Oracle调用Java


其它语言的函数的调用

java函数调用

在Oracle数据库建立一个java资源,也可以用loadjava命令装载其它的java类或者jar

create or replace and compile java sourcenamed mytestjava as
public class Factorial {
public static int calcFactorial (int n) {
if (n == 1) return 1;
else return n * calcFactorial (n - 1) ;
}
}

 

建立一个映射函数

CREATE OR REPLACE FUNCTION plstojavafac_fun
(N NUMBER)
RETURN NUMBER
AS
LANGUAGE JAVA
NAME 'Factorial.calcFactorial (int) return int';

 

selectplstojavafac_fun(4) from dual

----

24 

这是一个极其简单的例子,但是有了这样的功能,你可以调用外部的任何外部命令,也可以与其他任何外部数据库数据文件进行通讯了

下面一个例子,实现在数据库内调用任何外部命令的功能 

 
  1. CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Host" AS  
  2. import java.io.*;  
  3. public class Host {  
  4.   public static void executeCommand(String command) {  
  5.     try {  
  6.       String[] finalCommand;  
  7.       if (isWindows()) {  
  8.         finalCommand = new String[4];  
  9.         // Use the appropriate path for your windows version.   
  10.         finalCommand[0] = "C:\\windows\\system32\\cmd.exe";  // Windows XP/2003   
  11.         //finalCommand[0] = "C:\\winnt\\system32\\cmd.exe";  // Windows NT/2000   
  12.         finalCommand[1] = "/y";  
  13.         finalCommand[2] = "/c";  
  14.         finalCommand[3] = command;  
  15.       }  
  16.       else {  
  17.         finalCommand = new String[3];  
  18.         finalCommand[0] = "/bin/sh";  
  19.         finalCommand[1] = "-c";  
  20.         finalCommand[2] = command;  
  21.       }  
  22.     
  23.       final Process pr = Runtime.getRuntime().exec(finalCommand);  
  24.       pr.waitFor();  
  25.       new Thread(new Runnable(){  
  26.         public void run() {  
  27.           BufferedReader br_in = null;  
  28.           try {  
  29.             br_in = new BufferedReader(new InputStreamReader(pr.getInputStream()));  
  30.             String buff = null;  
  31.             while ((buff = br_in.readLine()) != null) {  
  32.               System.out.println("Process out :" + buff);  
  33.               try {Thread.sleep(100); } catch(Exception e) {}  
  34.             }  
  35.             br_in.close();  
  36.           }  
  37.           catch (IOException ioe) {  
  38.             System.out.println("Exception caught printing process output.");  
  39.             ioe.printStackTrace();  
  40.           }  
  41.           finally {  
  42.             try {  
  43.               br_in.close();  
  44.             } catch (Exception ex) {}  
  45.           }  
  46.         }  
  47.       }).start();  
  48.     
  49.       new Thread(new Runnable(){  
  50.         public void run() {  
  51.           BufferedReader br_err = null;  
  52.           try {  
  53.             br_err = new BufferedReader(new InputStreamReader(pr.getErrorStream()));  
  54.             String buff = null;  
  55.             while ((buff = br_err.readLine()) != null) {  
  56.               System.out.println("Process err :" + buff);  
  57.               try {Thread.sleep(100); } catch(Exception e) {}  
  58.             }  
  59.             br_err.close();  
  60.           }  
  61.           catch (IOException ioe) {  
  62.             System.out.println("Exception caught printing process error.");  
  63.             ioe.printStackTrace();  
  64.           }  
  65.           finally {  
  66.             try {  
  67.               br_err.close();  
  68.             } catch (Exception ex) {}  
  69.           }  
  70.         }  
  71.       }).start();  
  72.     }  
  73.     catch (Exception ex) {  
  74.       System.out.println(ex.getLocalizedMessage());  
  75.     }  
  76.   }  
  77.     
  78.   public static boolean isWindows() {  
  79.     if (System.getProperty("os.name").toLowerCase().indexOf("windows") != -1)  
  80.       return true;  
  81.     else  
  82.       return false;  
  83.   }  
  84. };   
  • 1
  • 2
  • 下一页

相关内容