create global temporary table crm.dirlist( filename varchar2(255), filesize number, filedate date) on commit delete rows; /
创建临时表保存文件列表信息。此处使用临时表的好处是:循环取文件列表时无需对表进行清空处理。
2.创建JAVA SOURCE过程
此过程用户访问操作系统目录并读取文件列表.
create or replace and compile java source named DirList as import java.io.*; import java.sql.*; public class DirList { public static void getList(String directory) throws SQLException { File path=new File(directory); String[] fileList=path.list(); String fileName; long fileSize; long fileDate; for (int i=0;i<fileList.length; i++) { fileName=fileList[i]; File fpath=new File(directory+'/'+fileName); fileSize=fpath.length(); fileDate=fpath.lastModified(); #sql{insert into crm.dirlist(filename, filesize, filedate) values(:fileName,:fileSize,to_date('01/01/1970','mm/dd/yyyy')+:fileDate/(24*60*60*1000))}; } } }
3.创建ORACLE存储过程
此过程用于调用和执行JAVA存储过程(DirList)
CREATE OR REPLACE PROCEDURE CRM.p_get_dir_list(i_directory IN VARCHAR2) AS LANGUAGE JAVA NAME 'DirList.getList(java.lang.String)';
4.执行p_get_dir_list过程获取数据
SQL> exec crm.p_get_dir_list('E:/割接/10_Transition_SVN_NEW/1_C网在用提取脚本/2.三户'); begin crm.p_get_dir_list('E:/割接/10_Transition_SVN_NEW/1_C网在用提取脚本/2.三户'); end; ORA-29532: Java 调用被未捕获的 Java 异常错误终止: java.security.AccessControlException: the Permission (java.io.FilePermission E:/割接/10_Transition_SVN_NEW/1_C网在用提取脚本/2.三户 read) has not been granted to CRM. The PL/SQL to grant this is dbms_java.grant_permission( 'CRM', 'SYS:java.io.FilePermission', 'E:/割接/10_Transition_SVN_NEW/1_C网在用提取脚本/2.三户', 'read' ) ORA-06512: 在 "CRM.GET_DIR_LIST", line 1 ORA-06512: 在 line 2