//以下运行在sql window
CREATE OR REPLACE AND compile JAVA SOURCE NAMED Utils AS
import java.io.*;
import java.lang.*;
public class Utils extends Object{
public static int RunThis(String args){
Runtime rt=Runtime.getRuntime();
int rc=-1;
try{
Process p=rt.exec(args);
int bufSize=4096;
BufferedInputStream bis=new BufferedInputStream(p.getInputStream(),bufSize);
int len;
byte buffer[]=new byte[bufSize];
while((len=bis.read(buffer,0,bufSize))!=-1)
System.out.write(buffer,0,len);
rc=p.waitFor();
}catch(Exception e){
e.printStackTrace();
rc=-1;
}
finally{
return rc;
}
}
}
//以下运行在sqlplus
create or replace function RUN_CMD(p_cmd in varchar2) return number as language java name 'Utils.RunThis(java.lang.String) return integer';
EXEC Dbms_Java.Grant_Permission('user','java.io.FilePermission', '*','read ,write, execute, delete');
EXEC Dbms_Java.Grant_Permission('user','java.io.FilePermission', 'c:\test.bat','read ,write, execute, delete');
EXEC Dbms_Java.Grant_Permission('user','java.io.FilePermission', 'D:\distrib\runpan.bat','read ,write, execute, delete');
EXEC dbms_java.grant_permission('user','java.lang.RuntimePermission','*','writeFileDescriptor' );
create or replace procedure RC(p_cmd in varchar2) as x number;
begin
x:=run_cmd(p_cmd);
if(x<>0)
then
raise program_error;
end if;
end;
/
variable x number;
set serveroutput on
exec dbms_java.set_output(100000);
//可以在触发器调用
exec :x := RUN_CMD('D:\distrib\runpan.bat');
阅读(1383) | 评论(0) | 转发(0) |