Oracle calls Java

Source: Internet
Author: User

Function calls in other languages

Java function call

 

Create a Java resource in the Oracle database, or use the loadjava command to load other Java classes or jar

 

Create or replace and compile Java sourcenamed mytestjava
Public class factorial {
Public static int calcfactorial (int n ){
If (n = 1) return 1;
Else return N * calcfactorial (n-1 );
}
}

 

Create a ing Function

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

 

This is an extremely simple example, but with this function, you can call any external commands or communicate with any other external database data files.

 

 

The following example shows how to call any external commands in the database.

 

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Host" ASimport 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;  }};

 

 

Before calling an external program, you must grant the corresponding permissions to the database user.

 

-- Created on 2007-9-13 by tiwen

Declare

Begin

Dbms_java.grant_permission ('tiwen', 'java. Io. filepermission', '<all files>', 'read, write, execute, delete ');

Dbms_java.grant_permission ('tiwen', 'sys: Java. Lang. runtimepermission', 'writefiledescriptor ','');

Dbms_java.grant_permission ('tiwen', 'sys: Java. Lang. runtimepermission', 'readfiledescriptor ','');

End;

 

Ing Process

Create or replace procedurehost_command (p_command in varchar2)

As languagejava

Name'host.exe cutecommand (Java. Lang. String )';

 

Test

Declare

Rochelle output dbms_output.chararr;

Rochelle lines INTEGER: = 1000;

Begin

Dbms_output.enable (1000000 );

Dbms_java.set_output (1000000 );

Host_command ('dir G: \ '); -- execute the command to display the Directory

Dbms_output.get_lines (l_output, l_lines );

For I in 1 .. l_lines Loop

Dbms_output.put_line (l_output (I ));

NULL;

End loop;

End;

 

 Original article, if reproduced, please mark the author: Tian Wen csdn address: http://blog.csdn.net/tiwen818

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.