problem while searching for files in a directory [message #348667] |
Wed, 17 September 2008 09:18 |
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 #348698 is a reply to message #348681] |
Wed, 17 September 2008 11:53 |
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 |
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
|
|
|