欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

oracle如何使用java source调用外部程序

程序员文章站 2023-02-22 13:49:49
需求 oracle调用第三方外部程序。oracle使用sqluldr2快速导出大批量数据,然后用winrar压缩后发送邮件。 源码 java source...

需求

oracle调用第三方外部程序。oracle使用sqluldr2快速导出大批量数据,然后用winrar压缩后发送邮件。

源码

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(); 

} 

} 

} 

} 

 

存储过程

create or replace procedure pro_jv_run_extpro(p_cmd varchar2) as

language java name 'jv_run_extpro.run(java.lang.string)';

调用

begin 

pro_jv_run_extpro('sqluldr264.exe scott/hh@pdborcl 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; 

总结

java source里streamgobbler这个类不能少,用于异步读取命令的输出。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。