当前位置:数据库 > Oracle >>

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
 
这是一个极其简单的例子,但是有了这样的功能,你可以调用外部的任何外部命令,也可以与其他任何外部数据库数据文件进行通讯了
 
 
下面一个例子,实现在数据库内调用任何外部命令的功能
 

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Host" AS 
import java.io.*; 
public class Host { 
  public static void executeCommand(String command) { 
    try { 
      String[] finalCommand; 
      if (isWindows()) { 
        finalCommand = new String[4]; 
        // Use the appropriate path for your windows version. 
        finalCommand[0] = "C:\\windows\\system32\\cmd.exe";  // Windows XP/2003 
        //finalCommand[0] = "C:\\winnt\\system32\\cmd.exe";  // Windows NT/2000 
        finalCommand[1] = "/y"; 
        finalCommand[2] = "/c"; 
        finalCommand[3] = command; 
      } 
      else { 
        finalCommand = new String[3]; 
        finalCommand[0] = "/bin/sh"; 
        finalCommand[1] = "-c"; 
        finalCommand[2] = command; 
      } 
   
      final Process pr = Runtime.getRuntime().exec(finalCommand); 
      pr.waitFor(); 
      new Thread(new Runnable(){ 
        public void run() { 
          BufferedReader br_in = null; 
          try { 
            br_in = new BufferedReader(new InputStreamReader(pr.getInputStream())); 
            String buff = null; 
            while ((buff = br_in.readLine()) != null) { 
              System.out.println("Process out :" + buff); 
              try {Thread.sleep(100); } catch(Exception e) {} 
            } 
            br_in.close(); 
          } 
          catch (IOException ioe) { 
            System.out.println("Exception caught printing process output."); 
            ioe.printStackTrace(); 
          } 
          finally { 
            try { 
              br_in.close(); 
            } catch (Exception ex) {} 
          } 
        } 
      }).start(); 
   
      new Thread(new Runnable(){ 
        public void run() { 
          BufferedReader br_err = null; 
          try { 
            br_err = new BufferedReader(new InputStreamReader(pr.getErrorStream())); 
            String buff = null; 
            while ((buff = br_err.readLine()) != null) { 
              System.out.println("Process err :" + buff); 
              try {Thread.sleep(100); } catch(Exception e) {} 
            } 
            br_err.close(); 
          } 
          catch (IOException ioe) { 
            System.out.println("Exception caught printing process error."); 
            ioe.printStackTrace(); 
          } 
          finally { 
            try { 
              br_err.close(); 
            } catch (Exception ex) {} 
          } 
        } 
      }).start(); 
    } 
    catch (Exception ex) { 
      System.out.println(ex.getLocalizedMessage()); 
    } 
  } 
   
  public static boolean isWindows() { 
    if (System.getProperty("os.name").toLowerCase().indexOf("windows") != -1) 
      return true; 
    else 
      return false; 
  } 
}; 

www.zzzyk.com
 
 
在调用外部程序之前,必须授权给数据库用户相应的权限
 
-- Created on 2007-9-13 by Tiwen
declare
begin
DBMS_JAVA.grant_permission('TIWEN','java.io.FilePermission', '&

补充:软件开发 , Java ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,