oracle执⾏java代码--需要确认数据库系统已经选择安装了JAVA虚拟机组件
--1、登陆SYS⽤户,执⾏以下代码
begin
Dbms_Java.Grant_Permission('PSIID','java.io.FilePermission', '<>','read ,write, execute, delete');
ant_permission('PSIID', 'SYS:java.io.FilePermission', '<>','read ,write, execute, delete');
Dbms_Java.Grant_Permission('PSIID', 'java.io.FilePermission', 'd:a.bat','read ,write, execute, delete');
ant_permission('PSIID', 'java.lang.RuntimePermission','*','writeFileDescriptor' );
end;
/
--2、登陆psiid⽤户创建java程序资源
create or replace and compile
java source named "Util"
as
import java.io.*;
import java.lang.*;
public class Util extends Object
{
public static int RunThis(String args)
{
Runtime rt = Runtime();
int rc = -1;
try
{
Process p = rt.exec(args);
int bufSize = 4096;
BufferedInputStream bis =
new InputStream(), bufSize);
int len;
byte buffer[] = new byte[bufSize];
// Echo back what the program spit out
while ((len = ad(buffer, 0, bufSize)) != -1)
System.out.write(buffer, 0, len);
rc = p.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
rc = -1;
}
finally
{
return rc;
}
}
}
/
--3、创建调⽤Java资源的函数
create or replace function RUN_CMD(p_cmd in varchar2) return number
as
language java name 'Util.RunThis(java.lang.String) return integer';
/
--4、建⽴⼀过程调⽤存储过程
create or replace procedure RUN(p_cmd in varchar2) as
x number;
begin
x := run_cmd(p_cmd);
end;
/
------------------------------java replace方法
------- 执⾏例⼦
-------------------------------
--d:a.bat ⽂件
cd d:
rename %1 %2
SQL> exec rc('d:a.bat mytest.sql b.sql') ;
D:oracleora92DATABASE>cd d:
D:>rename mytest.sql b.sql
exec :x := RUN_CMD('ipconfig');
variable x number;
exec dbms_java.set_output(100000);
exec :x := RUN_CMD('ipconfig');
exec :x := RUN_CMD('d:a.bat') ;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。