在PL/SQL中執行作業系統命令

llnnmc發表於2017-06-28

首先建立java source

createor replace and compile java source named execsyscmd as

import java.io.*;

publicclass ExecSysCmd

{

public static String exec_cmd(String args){

Runtime run = Runtime.getRuntime();

Process process = null;

try {

process = run.exec(args);

return "OK";

} catch (Exception e) {

return e.getMessage();

}

}

public static void main(String[] args)

{

System.out.println("No command linearguments");

}

}

/

其後建立函式呼叫該java soure

createor replace function exec_cmd(cmd string) return varchar2 is

language java name 'ExecSysCmd.exec_cmd(java.lang.String) returnjava.lang.String';

/

可以透過SQL查詢來呼叫

selectexec_cmd('d:\1.bat') from dual;

EXEC_CMD('D:\1.BAT')

-------------------------------------------------------------------

OK

也可以在PL/SQL程式碼中呼叫

setserveroutput on

declare

my_resultvarchar2(20);

begin

select exec_cmd('d:\1.bat') into my_resultfrom dual;

end;

/

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28974745/viewspace-2141346/,如需轉載,請註明出處,否則將追究法律責任。

相關文章