Abhishek Singh
Abhishek Singh

Reputation: 53

Retrieve image from oracle database and show image in jsp

I want to retrieve image from table named Image and column named image with type blob, but while running the code its giving the blob code like this-

"oracle.sql.BLOB@193242d"

Please see my code for fetching and show the image on jsp page/browser:-

ResultSet resultset = 
     statement.executeQuery("select * from image where id = '" + id + "'") ; 

Browser page:-

    <TABLE border="1">
        <TR>
           <TH>ID</TH>
           <TH>picture</TH>
       </TR>
       <TR>
           <TD> <%= resultset.getString(1) %> </TD>
           <TD> <%= resultset.getBlob(2) %> </TD>
       </TR>
   </TABLE>
   <BR>

Upvotes: 1

Views: 2466

Answers (2)

user5266804
user5266804

Reputation:

First:

oracle.sql.BLOB@193242d is the response of toString() method, not the image.

Second:

ResultSet resultset = 
     statement.executeQuery("select * from image where id = '" + id + "'") ; 

For sake of SQL Injection, please!

Finally
You cannot response image with text data in heart! Image must be retrieved via a separated cgi/servlet where the content type is set to image, so for your case.

<TD> <%= resultset.getString(1) %> </TD>
<TD> <img src="/get_img?id="<%=id%>/> </TD>

And later you need a servlet/cgi to respond the image as get_img

@WebServlet(name = "img", urlPatterns = {"/get_img"})
public class get_img extends HttpServlet{
doGet(HttpServletRequest req, HttpServletResponse resp){
...
 resp.setContentType("image/jpg");//or image/whatever
  try(ResultSet rs=ps.executeQuery()){//where ps is a PreparedStatement
   //set the content length, and respond image data
   //example: http://stackoverflow.com/a/4332975/5266804
  }
...
}
}

Upvotes: 1

Jozef Chocholacek
Jozef Chocholacek

Reputation: 2924

You can either create a servlet to serve the images from DB, see How to retrieve images from database and place on JSP?

Or you can embed the image data directly into the src attribute of an image tag: I want to retrieve image from mysql database using servlet and show that image along with that user details in table - although this is recommendable only for small images (icons etc.).

Upvotes: 0

Related Questions