其它语言的函数的调用java函数调用在Oracle数据库建立一个java资源,也可以用loadjava命令装载其它的java类或者jarcreate 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;
- }
- };
Oracle权限集合全集Oracle Java JDBC编程注意事项相关资讯 Oracle基础教程
- Oracle块编程返回结果集详解 (11/10/2013 10:45:58)
- Oracle基础教程之设置系统全局区 (08/22/2013 14:24:00)
- Oracle基础教程知识点总结 (06/18/2013 07:43:32)
| - Oracle基础教程之tkprof程序详解 (10/22/2013 11:49:50)
- Oracle基础教程之sqlplus汉字乱码 (07/18/2013 16:30:00)
- Oracle 管理之 Linux 网络基础 (02/16/2013 18:37:35)
|
本文评论 查看全部评论 (0)