Quantcast

Problem storing an image in a Blob field in Java DB.

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Problem storing an image in a Blob field in Java DB.

Dinesh Bajaj
Hi All,

I am using Java DB 10.5, and am experiencing problem in storing an image in a Blob field. I am using the below command to insert the image:

preparedStatement.setBinaryStream(5, inputStream, file.length());

The Derby throws the following exception on using the above-mentioned command:

Error code : 0
SQL State = XJ001
Message = Java exception: 'Input stream did not have exact amount of data as the requested length.: org.apache.derby.iapi.services.io.DerbyIOException'.

If I use the method variant of setBinaryStream that doesn't take the 3rd argement, then the command executes without any error, but perhaps no date is stored in the database, as when the data is retrieved in a Blob variable, its length is reported to be zero.

Thanks for any help that you can offer.

Regards,
Dinesh


The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem storing an image in a Blob field in Java DB.

Dinesh Bajaj

I am much disappointed that I have not got a single reply to my post. Surely, what I am trying to accomplish is a very simple task. Aren't Blob fields supposed to store binary objects like images; then, why the database can't perform this simple task?

-Dinesh

--- On Wed, 10/2/10, Dinesh Bajaj <[hidden email]> wrote:

From: Dinesh Bajaj <[hidden email]>
Subject: Problem storing an image in a Blob field in Java DB.
To: [hidden email]
Date: Wednesday, 10 February, 2010, 11:14 PM

Hi All,

I am using Java DB 10.5, and am experiencing problem in storing an image in a Blob field. I am using the below command to insert the image:

preparedStatement.setBinaryStream(5, inputStream, file.length());

The Derby throws the following exception on using the above-mentioned command:

Error code : 0
SQL State = XJ001
Message = Java exception: 'Input stream did not have exact amount of data as the requested length.: org.apache.derby.iapi.services.io.DerbyIOException'.

If I use the method variant of setBinaryStream that doesn't take the 3rd argement, then the command executes without any error, but perhaps no date is stored in the database, as when the data is retrieved in a Blob variable, its length is reported to be zero.

Thanks for any help that you can offer.

Regards,
Dinesh


The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.


Your Mail works best with the New Yahoo Optimized IE8. Get it NOW!.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem storing an image in a Blob field in Java DB.

Erin Drummond
Odd, I am able to put a file into the database like so:

File f = new File("image.jpg");
BufferedInputStream data = new BufferedInputStream(new FileInputStream(f));
PreparedStatement ps = getPreparedStatement("INSERT INTO Files(RowID, Data) VALUES (?,?)");
ps.setString(1, f.getName());
ps.setBinaryStream(2, data);
ps.execute();
data.close();

And then retrieve it like so:

ResultSet rs = Database.executeQuery("SELECT Data FROM Files WHERE RowID='image.jpg'");
BufferedInputStream bi = null;
if (rs.next()) {
    bi = new BufferedInputStream(rs.getBinaryStream("Data"));
}
//do something with bi

Hope this helps,
Erin


On Fri, Feb 12, 2010 at 6:07 PM, Dinesh Bajaj <[hidden email]> wrote:

I am much disappointed that I have not got a single reply to my post. Surely, what I am trying to accomplish is a very simple task. Aren't Blob fields supposed to store binary objects like images; then, why the database can't perform this simple task?

-Dinesh

--- On Wed, 10/2/10, Dinesh Bajaj <[hidden email]> wrote:

From: Dinesh Bajaj <[hidden email]>
Subject: Problem storing an image in a Blob field in Java DB.
To: [hidden email]
Date: Wednesday, 10 February, 2010, 11:14 PM


Hi All,

I am using Java DB 10.5, and am experiencing problem in storing an image in a Blob field. I am using the below command to insert the image:

preparedStatement.setBinaryStream(5, inputStream, file.length());

The Derby throws the following exception on using the above-mentioned command:

Error code : 0
SQL State = XJ001
Message = Java exception: 'Input stream did not have exact amount of data as the requested length.: org.apache.derby.iapi.services.io.DerbyIOException'.

If I use the method variant of setBinaryStream that doesn't take the 3rd argement, then the command executes without any error, but perhaps no date is stored in the database, as when the data is retrieved in a Blob variable, its length is reported to be zero.

Thanks for any help that you can offer.

Regards,
Dinesh


The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.


Your Mail works best with the New Yahoo Optimized IE8. Get it NOW!.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem storing an image in a Blob field in Java DB.

Kristian Waagan-4
On 12.02.10 06:15, Erin Drummond wrote:
> Odd, I am able to put a file into the database like so:
>    

Hi Erin and Dinesh,

First, I never saw the original messages before Erin's answer. Not sure
what's going on, because the messages are in the archives...

> File f = new File("image.jpg");
> BufferedInputStream data = new BufferedInputStream(new FileInputStream(f));
> PreparedStatement ps = getPreparedStatement("INSERT INTO Files(RowID, Data)
> VALUES (?,?)");
> ps.setString(1, f.getName());
> ps.setBinaryStream(2, data);
> ps.execute();
> data.close();
>    

While the two-argument setXStream methods are convenient, it is
(currently) in general more efficient to use the three-argument version.
It doesn't matter during insert, but you might get a performance penalty
when accessing the LOB afterwards (depends on what you do, the devil is
in the details; for instance getting the length will be more costly,
simply reading the stream should be fine).

Dinesh, are you sure the stream you specified actually returns data?
The error message you posted suggests that the stream is shorter (or
possibly longer) than what file.length() returns.
Does the input stream belong to the file object used to obtain the length?

To rule things out, you could start by inserting data from a stream
constructed on top of a byte array, make sure you can read it back out,
and then try reading data from the file.


Regards,
--
Kristian


> And then retrieve it like so:
>
> ResultSet rs = Database.executeQuery("SELECT Data FROM Files WHERE
> RowID='image.jpg'");
> BufferedInputStream bi = null;
> if (rs.next()) {
>      bi = new BufferedInputStream(rs.getBinaryStream("Data"));
> }
> //do something with bi
>
> Hope this helps,
> Erin
>
>
> On Fri, Feb 12, 2010 at 6:07 PM, Dinesh Bajaj<[hidden email]>wrote:
>
>    
>> I am much disappointed that I have not got a single reply to my post.
>> Surely, what I am trying to accomplish is a very simple task. Aren't Blob
>> fields supposed to store binary objects like images; then, why the database
>> can't perform this simple task?
>>
>> -Dinesh
>>
>> --- On *Wed, 10/2/10, Dinesh Bajaj<[hidden email]>* wrote:
>>
>>
>> From: Dinesh Bajaj<[hidden email]>
>> Subject: Problem storing an image in a Blob field in Java DB.
>> To: [hidden email]
>> Date: Wednesday, 10 February, 2010, 11:14 PM
>>
>>
>> Hi All,
>>
>> I am using Java DB 10.5, and am experiencing problem in storing an image in
>> a Blob field. I am using the below command to insert the image:
>>
>> preparedStatement.setBinaryStream(5, inputStream, file.length());
>>
>> The Derby throws the following exception on using the above-mentioned
>> command:
>>
>> Error code : 0
>> SQL State = XJ001
>> Message = Java exception: 'Input stream did not have exact amount of data
>> as the requested length.:
>> org.apache.derby.iapi.services.io.DerbyIOException'.
>>
>> If I use the method variant of setBinaryStream that doesn't take the 3rd
>> argement, then the command executes without any error, but perhaps no date
>> is stored in the database, as when the data is retrieved in a Blob variable,
>> its length is reported to be zero.
>>
>> Thanks for any help that you can offer.
>>
>> Regards,
>> Dinesh
>>
>> ------------------------------
>> The INTERNET now has a personality. YOURS! See your Yahoo! Homepage<http://in.rd.yahoo.com/tagline_yyi_1/*http://in.yahoo.com/>
>> .
>>
>>
>> ------------------------------
>> Your Mail works best with the New Yahoo Optimized IE8. Get it NOW!<http://in.rd.yahoo.com/tagline_ie8_new/*http://downloads.yahoo.com/in/internetexplorer/>
>> .
>>      
>    

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem storing an image in a Blob field in Java DB.

Bryan Pendleton
In reply to this post by Dinesh Bajaj
Dinesh Bajaj wrote:
>     preparedStatement.setBinaryStream(5, inputStream, file.length());

>     Message = Java exception: 'Input stream did not have exact amount of
>     data as the requested length.:

Can you show more of your actual code? It sounds like 'inputStream'
is not returning the data which is in 'file'.

Paste in a larger segment of your code, showing all the activity to
'inputStream' and 'file' since they were declared as variables.

thanks,

bryan

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem storing an image in a Blob field in Java DB.

Dinesh Bajaj

Hi Bryan, Kristian and Arin,

Many thanks for your replies.

The first post to this thread was also my first post to this forum, and I understand it was therefore moderated. It appears that due to a technical fault, it was not forwarded to the list members.

Coming back to the issue, I have now figured out the root cause of the problem.  The problem was arising as I was invoking the method ImageIO.read(fis) on the FileInputStream instance, before sending the same stream instance to the SetBinaryStream method.

I understand that reading the inputstream was causing the pointer to move to the end of the stream, and therefore no data was getting stored in the Derby database. I amended the code, and the problem is now fixed. What a great relieve as this problem was bugging me for the last 3-4 days, and I can't believe that I couldn't figure it out earlier.

Anyways, many thanks for all your input.

Regards,
Dinesh


--- On Fri, 12/2/10, Bryan Pendleton <[hidden email]> wrote:

From: Bryan Pendleton <[hidden email]>
Subject: Re: Problem storing an image in a Blob field in Java DB.
To: "Derby Discussion" <[hidden email]>
Date: Friday, 12 February, 2010, 8:57 PM

Dinesh Bajaj wrote:
>     preparedStatement.setBinaryStream(5, inputStream, file.length());

>     Message = Java exception: 'Input stream did not have exact amount of
>     data as the requested length.:

Can you show more of your actual code? It sounds like 'inputStream'
is not returning the data which is in 'file'.

Paste in a larger segment of your code, showing all the activity to
'inputStream' and 'file' since they were declared as variables.

thanks,

bryan



The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
Loading...