Oracle calls external programs using Java source

Source: Internet
Author: User
Tags rar static class

Demand

Oracle calls third-party external programs. Oracle uses SQLULDR2 to quickly export large quantities of data and then use WinRAR to compress and send messages.

This document mainly implements the first two steps, the sending mail program is no longer explained here.

Original code

Authorized

Begin Dbms_java.grant_permission (' SCOTT ', ' SYS:java.io.FilePermission ', ' <<all files>> ', ' Read,write, Execute,delete '); End

Begin Dbms_java.grant_permission (' SCOTT ', ' java.lang.RuntimePermission ', ' * ', ' writefiledescriptor '); End

Java source

Create or replace and compile Java source named Jv_run_extpro as

Import java.io.*;

Import java.lang.*;

Import java.util.*;

Import java.sql.*;

Import oracle.sql.*;

public class Jv_run_extpro

{

public static void Run (String cmd) throws IOException

{

Process p=runtime.getruntime (). exec (CMD);

Streamgobbler Errorgobbler = new Streamgobbler (P.geterrorstream (), "Error");

Streamgobbler Outputgobbler = new Streamgobbler (P.getinputstream (), "Output");

Errorgobbler.start ();

Outputgobbler.start ();

Try

{

P.waitfor ();

}

catch (Interruptedexception IE)

{

System.out.println (IE);

}

}

public static class Streamgobbler extends Thread {

?

InputStream is;

String type;

?

Public Streamgobbler (InputStream is, String type) {

This.is = is;

This.type = type;

}

?

public void Run () {

try {

InputStreamReader ISR = new InputStreamReader (IS);

BufferedReader br = new BufferedReader (ISR);

String line = null;

while (line = Br.readline ()) = null) {

if (Type.equals ("Error")) {

System.out.println ("Error:" + line);

} else {

System.out.println ("Debug:" + line);

}

}

} catch (IOException IoE) {

Ioe.printstacktrace ();

}

}

}

}?

Description

Streamgobbler This class cannot be used for asynchronous reading of output from a command.

Stored Procedures

Create or Replace procedure Pro_jv_run_extpro (P_cmd varchar2) as

Language Java name ' Jv_run_extpro.run (java.lang.String) ';

Call

Begin

Pro_jv_run_extpro (' Sqluldr264.exe scott/[email protected] query= "SELECT * from emp" field=, Head=yes file=d:\ Desktop\tmp\sqluldr2\out2. TXT ');

Pro_jv_run_extpro (' "D:\Program files\winrar\rar.exe" a-ep-df "D:\Desktop\tmp\sqluldr2\20160916.rar" "D:\Desktop\ Tmp\sqluldr2\out2. TXT "');

End

Parameter description:

A: Compress files

-EP: The compression path does not appear in the package

-DF: Delete the original file after compression

Summarize

Program execution requires authorization, and if you need to view the execution log, you can do it first under Sqlplus:

Set Serveroutput on

Exec Dbms_java.set_output (5000);

Oracle calls external programs using Java source

Related Article

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.