簡體   English   中英

Java存儲過程在Oracle數據庫中不返回任何內容

[英]Java stored procedure returns nothing in Oracle Database

我在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個項目。

  • 我不確定您如何檢查結果(是否有相同的會話)。 你在某處執行提交嗎? 全局臨時表有一些細節(可以選擇是否在提交后清除數據)。 在解決問題之前,您可能希望先嘗試使用永久性的解決方案。
  • 如果添加一些日志記錄(例如到另一個表),則可能會很有用。 例如, rootDirList.length可能是一個很好的檢查指標。

其他說明:

  • / *不執行* /在if語句中分支會增加額外的噪音。 很高興刪除它們。
  • 如果要檢查路徑是否為目錄(而不是isFile),則最好使用.isDirectory() )。

該代碼中存在一些錯誤,阻止了該錯誤寫入數據庫。 基於Yavor建議將String變量寫入臨時表的建議,我發現我已經在文件路徑(例如(/ transfer_edi / hs122 // Acctg))上復制了“ /”。 我還發現我也在寫的數據表中的某一列上的數據類型不正確。 我還切換到了常規表,而不是提交后要刪除的全局臨時表。 再次感謝Yavor。 無論如何,我最終都重寫了整個內容。 我意識到我需要遍歷目錄結構以獲取所有文件,因此這是對我有用的最終代碼。 再次,我不是一個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存儲過程

CREATE OR REPLACE procedure BALT_CHECK.getWebDocs( p_directory in varchar2)
as language java
name 'WebDocs.GetDocs( java.lang.String )';
/

調用存儲過程

exec getWebDocs( '/transfer_edi/hs122/');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM