[英]Java stored procedure returns nothing in Oracle Database
I have a fairly simple stored java procedure in an oracle database. 我在oracle数据库中有一个相当简单的存储Java过程。 The intended purpose is to read the contents of a folder which resides on the Oracle server. 预期目的是读取驻留在Oracle服务器上的文件夹的内容。 If it encounters a folder it will step into the folder and write the name of the contents into a global temp table, and move on to the next folder. 如果遇到文件夹,它将进入该文件夹,并将内容名称写入全局临时表,然后继续下一个文件夹。 The Java procedure compiles fine and submits into the database with no issues. Java过程可以正常编译,并且可以毫无问题地提交到数据库中。 When it's called by a stored Oracle procedure it runs successfully as well. 当存储的Oracle过程调用它时,它也将成功运行。 But produces no results into the global temp table. 但是不会在全局临时表中产生任何结果。 I am using TOAD and i'm not sure how to put a break or view the variables during run time so i'm kind of flying blind. 我正在使用TOAD,我不确定如何在运行时休息一下或查看变量,所以我有点盲目。 And i'm admittedly not great a java. 而且我公认不是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 */
}
}
}
}
}
}
/
Procedure is created as 过程创建为
CREATE OR REPLACE procedure BALT_CHECK.get_webdir_list( p_directory in varchar2)
as language java
name 'WebDirList.getList( java.lang.String )';
/
Procedure is called as 程序称为
exec get_webdir_list( '/transfer_edi/hs122/');
in the folder /transfer/edi/hs122/ are 10 sub directories each have between 1 and 100 items in them at any given time. / transfer / edi / hs122 /文件夹中的10个子目录在任何给定时间都包含1到100个项目。
rootDirList.length
may be a good indicator to check. 例如, rootDirList.length
可能是一个很好的检查指标。 Some other remarks: 其他说明:
.isDirectory()
if you want to check if the paths is a directory (instead of isFile). 如果要检查路径是否为目录(而不是isFile),则最好使用.isDirectory()
)。 There were a few errors in this code that prevented it from writing to the database. 该代码中存在一些错误,阻止了该错误写入数据库。 Based on Yavor's suggestion of writing String variables to a temp table I was able to find that I had duplicated "/" on the file path eg (/transfer_edi/hs122//Acctg). 基于Yavor建议将String变量写入临时表的建议,我发现我已经在文件路径(例如(/ transfer_edi / hs122 // Acctg))上复制了“ /”。 I also found I had an incorrect data type on one of my columns in my data table that I was writing too. 我还发现我也在写的数据表中的某一列上的数据类型不正确。 I also switched to a regular table instead of a global temp table which was deleting after commit. 我还切换到了常规表,而不是提交后要删除的全局临时表。 Again thanks Yavor. 再次感谢Yavor。 Regardless of all that I ended up re-writing the entire thing. 无论如何,我最终都重写了整个内容。 I realized that I needed to traverse down the directory structure to get all the files so here is the final code that worked for me. 我意识到我需要遍历目录结构以获取所有文件,因此这是对我有用的最终代码。 Again i'm not a java guy so i'm sure this could be done better. 再次,我不是一个Java的家伙,所以我相信这可以做得更好。
This link helped me quite a bit http://rosettacode.org/wiki/Walk_a_directory/Recursively#Java 该链接对我很有帮助http://rosettacode.org/wiki/Walk_a_directory/Recursively#Java
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED BALT_CHECK."WebDocs" AS
import java.io.*;
import java.sql.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.lang.String;
public class WebDocs
{
public static long fileID;
public static void GetDocs(String rootdirectory) throws SQLException
{
stepinto(rootdirectory);
}
public static void stepinto(String rootdirectory) throws SQLException
{
File path = new File( rootdirectory );
String[] DirList = path.list();
for( int x = 0; x < DirList.length; x++)
{
String newPath = rootdirectory + DirList[x];
if (newPath != null) {
File f = new File(newPath);
if (f.isDirectory()) {
GetDocs(newPath +"/");
}
if (f.isFile()){
WriteFile(f);
}else{
}
}
}
}
public static void WriteFile(File file) throws SQLException
{
String fileName;
String filePath;
String elementID;
long len;
Date date;
String sqlDate;
SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss");
fileID = fileID + 1;
elementID = String.valueOf(fileID);
fileName = file.getName();
filePath = file.getPath();
len = file.length();
date = new Date(file.lastModified());
sqlDate = df.format(date);
#sql { INSERT INTO WEB_STATICDOCS (ID, FILE_NAME, FILE_SIZE, CREATE_DATE, FILE_PATH)
VALUES (:elementID, :fileName, :len, to_date(:sqlDate, 'YYYY-MM-DD HH24:MI:SS'), :filePath) };
}
}
/
Oracle Stored Procedure Oracle存储过程
CREATE OR REPLACE procedure BALT_CHECK.getWebDocs( p_directory in varchar2)
as language java
name 'WebDocs.GetDocs( java.lang.String )';
/
Calling the stored Procedure 调用存储过程
exec getWebDocs( '/transfer_edi/hs122/');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.