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)

1
  • Just to clarify, you want to display one picture per product? Commented Apr 5, 2013 at 13:32

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

can you be more specific what the new servlet must have inside?
@marios: Just have a look here: stackoverflow.com/questions/4848819/… and here also: stackoverflow.com/questions/6315671/… As I think its a better approach to store files in a directory and to keep directory path in DB.
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.
is not mvc model is multy-tier model.
That would not work, since you cannot include an image directly in your JSP. You will always need an <img> tag to achieve that.
|
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

3 Comments

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.
No actually the first link shows both, first from the filesystem then from a relational database
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.