Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to retreive and present an image from a Database? 2

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

I have an SQL table that contains a column with type "image". How do I retreive and present it using the ResultSet Object?
 
have you tried
recordset.getBlob() or recordset.getBinaryStream() methods?
 
Is an image a standard database type?

If not, you'll probably need to use Blob.

Cheers,
Dian
 
There is no ANSI SQL database type named 'image' ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
It depends on what are you doing.

I'll give you a trivial answer until you give more details: save it to a file and the go to your broser: File>Open and choose yout file.

Cheers,
Dian
 
sedj: Well, in my SQL there is a Data Type called "image"...
Diancecht: That's an idea but how can I do it without the file mediator? Let's say I have a Servlet, can it's PrinterOut object write this image as part of it's HTML rendering?
 
I've never heard of an image directly rendered into the html. But that doesn't mean it's impossible.

What's wrong with saving it to a file?

Cheers,
Dian
 
I think the image type uses more resources then the file will.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
So storing images on a DB and retrieve them for each request is a way of saving resources?

What about storing the images as files in the Web Server (they will get cached), save in the database just the name of the image (will save network traffic) and then populate the html?

Cheers,
Dian
 
So lets imagine that you have managed to get the raw byte data of the image from the db into a byte[] object.

In a servlet - and it has to be a servlet, not a JSP, then you would do this :

- set the content type as image/png (or whatever image format it is)
- then get the output stream from the response object (response.getOutputStream())
- then write out the data ( outputStream.write(byteData) )

The client html/jsp page would then call the servlet like this :

<img src="/ImageServlet?imageid=whatever"/>

This would then display the image (where imageid parameter is the info needed to extract the image from the database)

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
That's an idea sedj, I'll check it.
And Diancecht, regarding saving resoucrs: It is not something I build from scratch, the DB already exists based on another application, I need to retreive this data from there so putting it there was not my idea to beging with...
 
@thelord Well, if you had told that from the beginning, everything would have been a lot more straightforward.

@sedj Had no idea that was possible. Nice workaround.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top