Inserting BLOBS to Oracle using Java

Hi. I am very familiar with Oracle but very new to Java. I need some guidance as to where I should begin with the following requirement:

I need to insert logs residing on an NT file system (generated by the application server) into BLOB columns in an Oracle database using Java (I think i/o is what I will need, but I am unsure). The logs are constantly being generated, so I need a program that will also frequently update the table in the DB accordingly. Does anyone have any suggestions or sample code that might help me achieve this as quickly as possible? Any suggestions are appreciated. Thanks in advance.

[633 byte] By [Charissa] at [2007-9-26 1:43:20]
# 1

Hi Cherissa

Following code might help you to store the files in BLOB column in Oracle database

test is a table in which TEST is BLOB column

String url = "jdbc:oracle:thin:@.....";

String user = "user name";

String password = "password";

Connection conn = null;

PreparedStatement aQuery = null;

ResultSet rs = null;

try

{

Class.forName("oracle.jdbc.driver.OracleDriver");

conn = DriverManager.getConnection(url,user,password);

conn.setAutoCommit(false);

Statement stmt = conn.createStatement();

stmt.execute("insert into test values (EMPTY_BLOB())");

rs = stmt.executeQuery("select TEST from test");

rs.next();

BLOB blob =((OracleResultSet)rs).getBLOB(1);

File f = new File(filename);

FileInputStream io = new FileInputStream(f);

OutputStream os = blob.getBinaryOutputStream();

int size = io.available();

byte[] buffer = new byte[1024];

int length=0;

while((length=io.read(buffer)) != -1)

os.write(buffer,0,length);

io.close();

os.close();

stmt.execute("commit");

}

catch()

{}

For frequent update in dabase,write thread & from that thread call the above program.

From

Sachin

sachin_ch at 2007-6-29 2:37:18 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
Wow, thanks a lot for the prompt response. This is great!Thanks for your help.Charissa
Charissa at 2007-6-29 2:37:18 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
One more question:Will the line:File f = new File(filename); require a specific filename, or can I have it reference all files in a particular directory?
Charissa at 2007-6-29 2:37:19 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
It requires specific file name.Sachin
sachin_ch at 2007-6-29 2:37:19 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Thanks Sachin
Charissa at 2007-6-29 2:37:19 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...