栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 数据库 > Oracle

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

Oracle 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

oracle如何使用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:Desktoptmpsqluldr2OUT2.TXT'); 
pro_jv_run_extpro('"D:Program FilesWinRARRar.exe" a -ep -df "D:Desktoptmpsqluldr220160916.rar" "D:Desktoptmpsqluldr2OUT2.TXT"');
 
end; 

总结

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/171556.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号