Oracle Use Java source Calling external programs
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.
Original code
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.*;
Publicclass Jv_run_extpro
{
Publicstaticvoid 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);
}
}
Publicstaticclass Streamgobbler extends Thread {
?
InputStream is;
String type;
?
Public Streamgobbler (InputStream is, String type) {
This. is = is;
This. Type = type;
}
?
Publicvoid 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 ();
}
}
}
}
?
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
Summarize
Java Source Streamgobbler This class is not small, for asynchronous read command output.
Oracle calls external programs using Java source