Urgent - pls help - Problem while inserting binary file into Oracle DB

Hi,

I am trying to insert binary files into a Blob column in a Oracle 10G table.

The binary files would be uploaded by the web users and hence come as multipart request. I use apache commons upload streaming API to handle it. Finally i am getting a input stream of the uploaded file.

The JDBC code is

PreparedStatement ps=conn.prepareStatement("insert into bincontent_table values(?)");

ps.setBinaryStream(1,inStream,length);

My problem starts when i try to find the length of the stream. available() method of inputstream does not return the full length of the stream. so i put a loop to read thru the stream and find the length as shown below

int length=0;

while((v=inStream.read())!=-1)

{

length++;

}

Now, though i got the length, my stream pointer has reached the end and i cant reset it(it throws an error if i try).

So i copied the stream content to a byte array and created an ByteArrayInputStream like this.

tempByteArray=new byte[length];

stream.read(tempByteArray,0,length);

ByteArrayInputStream bais=new ByteArrayInputStream(tempByteArray);

Now if i pass this bytearray input stream instead of the normal input stream to the prepared statement's setBinaryStream() method it throws an error as

"ORA-01460: unimplemented or unreasonable conversion requested".

Now how to solve this?

My doubts are ,

1) preparedStatement.setBinaryStream(int parameterIndex, InputStream x, int length) expects an inputstream and its length. if i have the stream how to find its length with out reading the stream?

2) Also as the length parameter is a integer, what if i have a large binary file whose length runs more than the capacity of integer

3) Alternatively there is a setBlob(int i, Blob x) in prepared statement. But how to instantiate a Blob object and set it here

4) Is there any better way to do this.

Thanks in advance

[1981 byte] By [smrsrajaa] at [2007-11-27 7:08:53]
# 1
Raja,I just answered this same question you posted to the OTN forums: http://forums.oracle.com/forums/thread.jspa?threadID=518851Small Internet, eh?Good Luck,Avi.
abramiaa at 2007-7-12 19:00:18 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
Avi,Thats not the answer i am looking for.Thanks,Raja
smrsrajaa at 2007-7-12 19:00:18 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> "ORA-01460: unimplemented or unreasonable conversion

> requested".

When the setBinaryStream method is used, the driver may have to do extra work to determine whether the parameter data should be sent to the server as a LONGVARBINARY or a BLOB (reference: javadoc)

> 1) preparedStatement.setBinaryStream(int parameterIndex,

> InputStream x, int length) expects an inputstream and its length. if i

> have the stream how to find its length with out reading the stream?

no. stream may have no specified length. i think you have wrong understanding about stream.

> 2) Also as the length parameter is a integer, what if i have a large

> binary file whose length runs more than the capacity of integer

> 3) Alternatively there is a setBlob(int i, Blob x) in prepared statement.

> But how to instantiate a Blob object and set it here

> 4) Is there any better way to do this.

use ps.setBlob(1, instream) instead

j_shadinataa at 2007-7-12 19:00:18 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...