• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Retreiving an image using JDBC

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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++)
{
out.print(data[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?
Regards
Maher Dabbas.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The out variable is a javax.servlet.jsp.JspWriter, which extends java.io.Writer. 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
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
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:

/Rene
[ June 13, 2002: Message edited by: Rene Larsen ]
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
thanks
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic