Home » Developer & Programmer » JDeveloper, Java & XML » problem while searching for files in a directory
problem while searching for files in a directory [message #348667] Wed, 17 September 2008 09:18 Go to next message
yairk30
Messages: 14
Registered: May 2006
Location: israel
Junior Member
hey,
i'm running a procedure that searched for a files in a given directory, and then insert its names to a temp table.
for doing it i compiled a java proc going like this:

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[] list = path.list();
String element;

for(int i = 0; i < list.length; i++)
{
element = list[i];
#sql { INSERT INTO DIR_LIST (FILENAME)
VALUES (:element) };

}
}

}

and a pl/sql goes like this:

procedure get_dir_list( p_directory in varchar2 )
as language java
name 'DirList.getList( java.lang.String )';

now ,the 3rd proc i used
running the get_dir_list proc. and select file names from the
DIR_LIST table.
after that i runnung anothe procs that obtain the dir and files names and insert the data from the files (txt and xml) to a tables.
my problem: if i locate more than one file inside the dir i get
ORA-08103 object no longer exists error ,
altough that i have all files and dir as predefined.

what is my problem here?

oops , i'm runnung it all on 9i db.

thanks!
Re: problem while searching for files in a directory [message #348668 is a reply to message #348667] Wed, 17 September 2008 09:21 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
ttp://www.orafaq.com/forum/t/88153/0/
Please read & FOLLOW the Posting Guidelines as stated in URL above

I wish I could provide more assistance, but I can't debug code I can not see.
Re: problem while searching for files in a directory [message #348681 is a reply to message #348667] Wed, 17 September 2008 10:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68728
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Always use SQL*Plus and copy and paste your session.

But before
please read OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code (See SQL Formatter).
Use the "Preview Message" button to verify.
Also always post your Oracle version (4 decimals).

Regards
Michel
Re: problem while searching for files in a directory [message #348698 is a reply to message #348681] Wed, 17 September 2008 11:53 Go to previous messageGo to next message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
This is my old test script
-- run from SCOTT
create table DIR_LIST (filename varchar2(255),lastmodified date, filesize number);

CREATE OR REPLACE AND RESOLVE 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);
       File[] FileList = path.listFiles();
       String TheFile;
       Timestamp ModiDate;
	   Long Siz;
       //#sql { DELETE FROM DIR_LIST};

       for(int i = 0; i < FileList.length; i++)
       {
           TheFile = FileList[i].getAbsolutePath();
           ModiDate = new Timestamp( FileList[i].lastModified() );
           Siz = new Long(FileList[i].length() );

           #sql { INSERT INTO DIR_LIST (FILENAME,LASTMODIFIED,FILESIZE)
                  VALUES (:TheFile, :ModiDate, :Siz) };
       }
   }
  }
/


create or replace procedure POP_dir_list( p_directory in varchar2 )
as language java
name 'DirList.getList( java.lang.String )';
/

Select * from DBA_JAVA_POLICY where type_name like 'java.io.%'
and grantee = 'SCOTT';
 
-- run from DBA account
begin
 for i in ( select value from v$parameter where name like '%dump_dest') loop
 --dbms_java.revoke_permission( 'SCOTT', 'SYS:java.io.FilePermission', i.value, 'read' );
 --dbms_java.revoke_permission( 'SCOTT', 'SYS:java.io.FilePermission', i.value||'/*', 'read' );
 -- the directory
 dbms_java.grant_permission( 'SCOTT', 'SYS:java.io.FilePermission', i.value, 'read' );
 -- directory contents
 dbms_java.grant_permission( 'SCOTT', 'SYS:java.io.FilePermission', i.value||'/*', 'read' );
 end loop;
 commit;
end;
/


--===============================================
-- Example
--===============================================
delete DIR_LIST;

begin dbms_java.set_output(100000); end;
/

begin
 for i in ( select value from v$parameter where name like '%dump_dest') loop
 pop_dir_list( i.value );
 end loop;
end;
/

select * from dir_list;

Based on code from Ask Tom site. Last tested on 8i or 9i.

[Updated on: Wed, 17 September 2008 11:54]

Report message to a moderator

Re: problem while searching for files in a directory [message #348843 is a reply to message #348667] Thu, 18 September 2008 01:45 Go to previous message
yairk30
Messages: 14
Registered: May 2006
Location: israel
Junior Member
guys!
first, thanks for your replies!
i will remember your comments, regarding code formating, at my future posts!

second,
i find a solution to my problem!
i had a "ORA-08103: object no longer exists" error ,
which derived from the fact that i executed a select from a tmp table dir_list which contained "on commit delete rows" clause ,
but i had actually do a commit in a sub proc. that i have ran (and which i didnt supply here..) , fact that i had , of course, didnt took under account at the first time.

now it all work..

cheers.
yair
Previous Topic: Bean / Java PJC
Next Topic: Help ! Extract data from XML result in PL/SQL
Goto Forum:
  


Current Time: Tue Dec 17 22:28:15 CST 2024