This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC 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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Retreiving an image using JDBC" Watch "Retreiving an image using JDBC" New topic
Author

Retreiving an image using JDBC

maher dabbas
Greenhorn

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++)
{
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.
Bonnie Beach-Morin
Greenhorn

Joined: Jun 13, 2002
Posts: 13
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.


SJCP2
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

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 ]

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.
thanks


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retreiving an image using JDBC
 
Similar Threads
Multiple File Save
java.sql.sqlexception data size bigger than max size for this type
retreiving images from the database and dispaly them on HTML file
Fetching image from database
how to retrieve image from database?