【发布时间】:2014-05-14 10:58:24
【问题描述】:
我在 oracle 数据库中有一个相当简单的 Java 存储过程。预期目的是读取驻留在 Oracle 服务器上的文件夹的内容。如果它遇到一个文件夹,它将进入该文件夹并将内容的名称写入一个全局临时表,然后移动到下一个文件夹。 Java 过程编译良好,并毫无问题地提交到数据库中。当它被存储的 Oracle 过程调用时,它也会成功运行。但不会在全局临时表中产生任何结果。我正在使用 TOAD,但我不确定如何在运行时休息或查看变量,所以我有点盲目。诚然,我不是一个很棒的 java。
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED BALT_CHECK."WebDirList" AS
import java.io.*;
import java.sql.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class WebDirList
{
public static void getList(String rootdirectory) throws SQLException
{
File path = new File( rootdirectory );
String[] rootDirList = path.list();
String element;
for( int x = 0; x < rootDirList.length; x++)
{
element = rootDirList[x];
String newPath = rootdirectory + "/" + rootDirList[x] ;
File f = new File(newPath);
if (f.isFile()){
/* Do Nothing */
} else {
/*if it is a folder than load the subDirPath variable with the newPath variable */
File subDirPath = new File( newPath+"/");
String[] subDirList = subDirPath.list();
String efileName;
for(int i = 0; i < subDirList.length; i++)
{
efileName = subDirList[i];
String fpath = subDirPath + "/" + subDirList[i];
File nf = new File(fpath);
long len;
Date date;
String ftype;
String sqlDate;
SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss");
if (f.isFile()) {
len = f.length();
date = new Date(f.lastModified());
sqlDate = df.format(date);
#sql { INSERT INTO WEB_DIRLIST (FILENAME, LENGTH, CREATEDATE)
VALUES (:efileName, :len, to_date(:sqlDate, 'YYYY-MM-DD HH24:MI:SS')) };
}else{
/* Do nothing */
}
}
}
}
}
}
/
过程被创建为
CREATE OR REPLACE procedure BALT_CHECK.get_webdir_list( p_directory in varchar2)
as language java
name 'WebDirList.getList( java.lang.String )';
/
过程被称为
exec get_webdir_list( '/transfer_edi/hs122/');
在文件夹 /transfer/edi/hs122/ 中有 10 个子目录,每个子目录在任何给定时间都有 1 到 100 个项目。
【问题讨论】:
-
sqlj包导入了吗?
-
只导入顶部列出的包。 sqlj应该导入吗?
-
Oracle 对目录树有读取权限吗?当您说它在存储的过程之外正确编译/运行/插入时,我不确定您的意思-您是指作为操作系统中的独立程序,还是仍在数据库中?
-
@AlexPoole 是的,所有权限都在那里。我通过 DBA 和该代码的较小版本确认了这一点,该代码采用了文件夹的完整路径('/transfer_edi/hs122/Acctg'),而不是遍历根目录('/transfer_edi/hs122/')并且它有效按照预期将信息写入临时表。当我添加第一个 for 循环以获取文件夹名称时,它是否停止写入表。
标签: java oracle stored-procedures plsql oracle11g