Define in PL/SQL Java source named JDeleteFile for Java class JDelete:
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "JDeleteFile" AS
import java.io.File;
public class JDelete {
public static int delete (String fileName) {
File myFile = new File (fileName);
boolean retval = myFile.delete();
if (retval) return 1; else return 0;
}
}
/
Create PL/SQL function fDelete as wrapper of Java JDelete.delete method
CREATE FUNCTION fDelete (file IN VARCHAR2)
RETURN NUMBER
AS LANGUAGE JAVA NAME 'JDelete.delete (java.lang.String) return int';
Example of using:
SQL> EXEC DBMS_OUTPUT.PUT_LINE (fdelete('/u01/app/oracle/admin/orcl/adump/ora_some_file.aud'));
Before use fDelete function to avoid file permissions errors like:
ERROR at line 1:
ORA-29532: Java call terminated by uncaught Java exception: java.security.
AccessControlException: the
Permission (java.io.FilePermission /u01/app/oracle/admin/orcl/adump/ora_some_file.aud delete) has not been
granted to SCOTT. The PL/SQL
to grant this is dbms_java.grant_permission( 'SCOTT', 'SYS:java.io.FilePermission',
'/u01/app/oracle/admin/orcl/adump/ora_some_file.aud', 'delete' )
need grants permission to access files in a required directory:
CALL DBMS_JAVA.grant_permission(
'SYSTEM',
'SYS:java.io.FilePermission',
'/u01/app/oracle/admin/orcl/adump/*',
'read,write,delete'
);
fDelete function can delete just concrete specified file name (names which you know), if you want delete all files *.aud in directory
then need use FILE_LIST_API.list described in
List Files in a Directory From PL/SQL and SQL using Java class FileListHandler to get all file names list:
DECLARE
res pls_integer;
BEGIN
FOR rec in (SELECT * FROM table(FILE_LIST_API.list ('/u01/app/oracle/admin/orcl/adump/')) where rownum <= 10000)
LOOP
res := fdelete(rec.column_value);
/*
if res = 1 then
DBMS_OUTPUT.PUT_LINE (rec.column_value || ' is ' || 'deleted');
else
DBMS_OUTPUT.PUT_LINE (rec.column_value || ' ' || 'not found/error');
end if;
*/
END LOOP;
END;
/
Note: Oracle9i Database Release 2 introduced an enhanced version of the
UTL_FILE package that, among other things, allows you to delete a file
using the UTL_FILE.FREMOVE procedure. It also supports file copying
(FCOPY) and file renaming (FRENAME).
Комментариев нет:
Отправить комментарий