aspose file tools*
The moose likes JDBC and the fly likes Fetching Image Data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Fetching Image Data" Watch "Fetching Image Data" New topic
Author

Fetching Image Data

Smitesh Patel
Greenhorn

Joined: Aug 08, 2005
Posts: 7
Hello,

I would like to know how to get the image data of data-type "image" stored in Microsoft's MS Sql Server Database using which JDBC method .

I tried using getBinary() but throws an exception.

And if i am in a wrong direction then which datatype available in Microsoft's MS Sql Server Database should i use for storing images & which method can i use to access that data using JDBC??

Thanks
[ September 11, 2005: Message edited by: Bear Bibeault ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42278
    
  64
I tried using getBinary() but throws an exception.

Why don't you tell us what kind of exception that is? There are any number of reasons why this could fail.

I would hope that the image datatype is not something MS-specific, but just a binary representation of a common file format. What do the SqlServer docs say about the format of the image type?


Ping & DNS - my free Android networking tools app
Smitesh Patel
Greenhorn

Joined: Aug 08, 2005
Posts: 7
Hi,

The exception is java.lang.NullPointerException.
Also if it the image data is null then is it possible that it can give this NullPointerException??

Thanks
Dave Salter
Ranch Hand

Joined: Jul 20, 2005
Posts: 293

You should be able to store binary data in an image column in SQL Server. Can you give an example of where the code is giving a NPE?
[ September 12, 2005: Message edited by: Dave Salter ]
Smitesh Patel
Greenhorn

Joined: Aug 08, 2005
Posts: 7
Hi there

I have found out where the problem is occuring.
Actually I have the image data which i have converted to bytes & sending it to my servlet in small chunks .Then on the servlet I am opening an input stream & writing all the data from that inputstream to ByteArrayOutputStream till read() doesn't = -1 .
Then i am converting the data written to the ByteArrayOutputStream to byteArr[] & then inserting that byte[] into the database.

Here the problem is occuriing the whole byte[] is not getting properly inserted into the database & thats why i am facing difficulty in fetching that data.

So kindly guide me how to write a byte[] into Sql Server database having datatype "image" & how to retrieve the same.

If possible provide me some code.

Thanks
Dave Salter
Ranch Hand

Joined: Jul 20, 2005
Posts: 293

Here's an article that explains how to serialize objects to the database. It includes sample code that should help you.

Cheers,

Dave.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Fetching Image Data