File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Retreiving an image using JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Retreiving an image using JDBC" Watch "Retreiving an image using JDBC" New topic

Retreiving an image using JDBC

maher dabbas

Joined: Dec 02, 2001
Posts: 26
Hi all
I'm trying to retreive an image from Oracle database,stored as BLOB, and to display this image on an Html page genarated by JSP.
first i've wrote a Bean to retreive the image , the code is as follows:

Blob a1=res.getBlob("item_pic");
long len = a1.length();
int len2=(int)len;
byte[] data= a1.getBytes(1,len2);
return data.
then in the jsp, i've wrote a code to print the byte array as follows:

for (int i = 0; i <data.length;i++)
But the result is a large amount of numbers printed on the screen ,not the image!!!
Can any body help me on this please?
Maher Dabbas.
Bonnie Beach-Morin

Joined: Jun 13, 2002
Posts: 13
The out variable is a javax.servlet.jsp.JspWriter, which extends Writer classes print/write character representations of data. So, out.print(data[i]) is printing a character representation of each byte in the BLOB.
In order to display an image in a page, the browser is going to look for an <img> tag with the src attribute pointing to a file on a server or URL, or as a download link.
If you really, really want to display the image in a page along with other text, the only way I can think of is to extract the BLOB and write it to an intermediate file, then reference that intermediate file in the src attribute.
If you want get the InputStream from the BLOB by calling blob.getBinaryStream() (make sure your jdbc drivers support BLOBs well), and feeding it to the ServletOutputStream of the response variable, it should allow a user to either download the image or view it in a browser window.

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

I have a java bean where I gets a byte[] from the database:

In my Servlet I then write to the output:

In my JSP page I call my Servlet and gets the image:

[ June 13, 2002: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hello guys. Why is this so difficult to achieve..?? . This is my situation:
1) I have stored an image in a blob field in mysql (ITEM_IMAGE).
2) Now, my index page is generated dynamically. So what I do is select all the items that are 'special', create a bean for every item and put them in a vector, something like this:

and this is makeBean:

SO it is a bit different of what Rene does. Actually, I haven't figured out how to display the image in my jsp page, coz in the way I'm doing it the servlet would need to get the blob from the DB,transform it some way ( ) and put it in the bean together with the other fields, so it can be used in the JSP page.
do you guys have any suggestion as how I can achieve that?
I'd appreciate any help.

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
I agree. Here's the link:
subject: Retreiving an image using JDBC