This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Retreiving an image using JDBC  RSS feed

Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!