0

I can't display BLOB image that I have in mysql saved. I have beans, jsp. I use 3multy-tier architecture, I want to display all the products with the picture.

In acceesor:

 try {
        Connection cn = getVla().getConnection();
        String sql = "SELECT * FROM products";
        PreparedStatement pst = cn.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        ArrayList<products> ls = new ArrayList<products>();
        while(rs.next()) {
            products s = new products();
            s.setPk(rs.getLong("pk"));
            s.setName(rs.getString("name"));
            s.setPrice(rs.getDouble("price"));
            s.setPic(rs.getBlob("pic"));
            s.setComments(rs.getString("comments"));
            ls.add(s);
        }
        return ls;
    }

In products:

 public Blob getPic() {
        return pic;
    }

In main.jsp

<%=
List<products> product = bean.getproducts();

%>
<h1>Product: </h1>
<%  
for(products c : product) { 
%>
From <%= c.getName()%> <br/>
<%= c.getPic()%></b><br/>
<b><%= c.getPrice()%> </b><br/>
<%= c.getComments()%>
<hr/>
<%
}
%>

How I can display the picture? (Currently I am getting com.mysql.jdbc.Blob@2e5f6a64 in display)

Mohammad Ashfaq
  • 1,333
  • 2
  • 14
  • 38
marios
  • 261
  • 8
  • 26

2 Answers2

4

What you're seeing is the result of Blob.toString(). Since it's binary content, the JVM cannot really figure a nice representation.

What you should do is create a seperate Servlet that only retrieves the Blob from the database and streams its content to response.getOutputStream(). In your JSP, you add an <img> tag whose src-attribute points to the Servlet you just wrote.

The Servlet should read the image for one product at a time, so the query would be slightly different: it should be enough to have

String sql = "SELECT pic FROM products where pk = " + pk;

Note that you need to specify this pk variable using some request parameter. The above line of code is just an example to demonstrate the idea. It is very unsafe to literally copy a request URL into an SQL query. Google for "SQL Injection" to read more about that.

Using Blob.getInputStream() you can obtain an InputStream whose contents you could copy to response.getOutputStream() in order to write it back to the browser. Don't forget to set the appropriate content-type on that response, for example "image/jpg" in case of JPEG pictures.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
  • can you be more specific what the new servlet must have inside? – marios Apr 05 '13 at 12:00
  • @marios: Just have a look here: http://stackoverflow.com/questions/4848819/how-to-display-image-from-database-in-servlet and here also: http://stackoverflow.com/questions/6315671/how-to-retrieve-image-from-database-and-display-in-jsp-via-servlet As I think its a better approach to store files in a directory and to keep directory path in DB. – Shailesh Saxena Apr 05 '13 at 12:05
  • Well, you **could** put the code in a JSP as well (like you're already fetching products from the database by calling the bean from your JSP), but I would advice against that approach because it violates the Model-View-Controller separation, IMHO. – mthmulders Apr 05 '13 at 12:13
  • is not mvc model is multy-tier model. – marios Apr 05 '13 at 12:17
  • what if make some class in my accessor like getimg() and inside put it in? (instead of blob can i use something else more esay?) – marios Apr 05 '13 at 12:33
  • 2
    That would not work, since you cannot include an image directly in your JSP. You will *always* need an `` tag to achieve that. – mthmulders Apr 05 '13 at 12:36
0

As mthmulders said what you are seeing is the the value of Blob.toString().

BalusC posted sometime ago a 2 great articles on serving images recovered from a database blob through a servlet :

http://balusc.blogspot.fr/2007/04/imageservlet.html

And a more recent one with GZIP and resume handling

http://balusc.blogspot.fr/2009/02/fileservlet-supporting-resume-and.html

ufasoli
  • 1,038
  • 2
  • 19
  • 41
  • Note that these examples read the images from a specified location on the filesystem, whereas the question is about images being stored in a relational database. – mthmulders Apr 05 '13 at 12:08
  • No actually the first link shows both, first from the filesystem then from a relational database – ufasoli Apr 05 '13 at 12:13
  • Marios regarding your comment "i am not working with servlet.i am working with classes" I agree with mthmulders you could put your code directly in the JSP but is not a good practice. By creating a separate servlet that handles the image you keep the view clean – ufasoli Apr 05 '13 at 12:18