Java Stored Procedure em Oracle

Caros, uma dica interessante: Como criar uma Java Stored Procedure em Oracle que acessa o Unix local.

Para da grant (com SYS ou DBA):

[code]begin
dbms_java.grant_permission
(‘ICD’,
‘java.io.FilePermission’,
‘/usr/bin/ps’,
‘execute’);

    dbms_java.grant_permission
    ('ICD',
    'java.lang.RuntimePermission',
    '*',
    'writeFileDescriptor' );

end;
/[/code]

Então criar com o usuário owner que vc vai executar.

[code]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.getRuntime();
int rc = -1;
try {
Process p = rt.exec(args[0]);

    int bufSize = 4096;
    BufferedInputStream bis =
     new BufferedInputStream(p.getInputStream(), bufSize);
    int len;
    byte buffer[] = new byte[bufSize];

    // Echo back what the program spit out
    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;
 }
 }

}
/[/code]

[code]create or replace
function RUN_CMD( p_cmd in varchar2) return number
as
language java
name ‘Util.RunThis(java.lang.String[]) return integer’;
/

create or replace procedure RC(p_cmd in varchar2)
as
x number;
begin
x := run_cmd(p_cmd);
end;
/[/code]

E para executar:

set serveroutput on size 1000000 exec dbms_java.set_output(1000000) exec rc('/usr/bin/pwd');

Mas infos: http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:952229840241

Legal a dica :D, pena só funcionar a partir do 10

A partir do 8i funciona.

Vcs sabem se tem problemas de incompatibilidade entre a versão 9 e 10 nos java stored procedures?