Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2009
    Posts
    10

    Unanswered: Displaying blob images from database and display it on jsp

    Hey, I really need you guys help as I had been searching info on displaying blob images from database for weeks

    I tried to add in the code that i think would display however it only display the path instead if the image.

    Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    	pageEncoding="ISO-8859-1"%>
    <%@ page import="java.sql.*"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h1>PRODUCTS DETAILS</h1>
    <p>
    <%
    				//Retrieve the id of the product selected by the user
    				//the product id is sent via the URL as a parameter
    				int ProductId = -1;
    				String strProductId = request.getParameter("ProductId");
    				if(strProductId != null) {
    					//Convert from string to int
    					ProductId = Integer.parseInt(strProductId);
    				}
    				
    				//Load the database driver
    		       	Class.forName("com.mysql.jdbc.Driver");
    				//Create a connection to our database
    		       	Connection con = DriverManager.getConnection("jdbc:mysql://localhost/peisze", "root", "");
    				//create an SQL statement
    		       	PreparedStatement ps = con.prepareStatement("SELECT * FROM products WHERE ProductId=?");
    				//set the ID to be the id above
    		       	ps.setInt(1, ProductId);
    				//Execute and retrieve our result
    		       	ResultSet rs = ps.executeQuery();
    			%>
    </p>
    <%
            	//if there is a result, rs.next() will be true
            	//else it will be false
    			if(rs.next()) {
    		%>
    <fieldset>
    <legend>Product Information</legend>
    <table border="0">
    	<tr>
    		<td>
    		<div align="left">Image: <%=rs.getBlob("ProdImage")+"\t" %></div>
    		</td>
    		<td>Product Name: <%=rs.getString("ProdName")+"\t" %>
    
    		<div align="left">Product Color: <%=rs.getString("ProdColor")+"\t" %></div>
    
    		<div align="left">Product Description: <%=rs.getString("ProdDesc")+"\t" %></div>
    
    		<div align="left">Product Price: <%=String.format("$%.2f",rs.getDouble("UnitPrice"))+"\t" %></div>
    
    		<div align="left">Quantity: <%=rs.getString("Quantity")+"\t" %></div>
    		
    	
    		
    	    
    
    		</td>
    	</tr>
    </table>
    </fieldset>
    
    <%
    			}
    			else {
    				//if no record is found, simply display a no record message
    		%>
    No record found.
    <%
    			}
    		 %>
    <p>&nbsp;</p>
    
    
    </body>
    </html><%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    	pageEncoding="ISO-8859-1"%>
    <%@ page import="java.sql.*"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h1>PRODUCTS DETAILS</h1>
    <p>
    <%
    				//Retrieve the id of the product selected by the user
    				//the product id is sent via the URL as a parameter
    				int ProductId = -1;
    				String strProductId = request.getParameter("ProductId");
    				if(strProductId != null) {
    					//Convert from string to int
    					ProductId = Integer.parseInt(strProductId);
    				}
    				
    				//Load the database driver
    		       	Class.forName("com.mysql.jdbc.Driver");
    				//Create a connection to our database
    		       	Connection con = DriverManager.getConnection("jdbc:mysql://localhost/peisze", "root", "");
    				//create an SQL statement
    		       	PreparedStatement ps = con.prepareStatement("SELECT * FROM products WHERE ProductId=?");
    				//set the ID to be the id above
    		       	ps.setInt(1, ProductId);
    				//Execute and retrieve our result
    		       	ResultSet rs = ps.executeQuery();
    			%>
    </p>
    <%
            	//if there is a result, rs.next() will be true
            	//else it will be false
    			if(rs.next()) {
    		%>
    <fieldset>
    <legend>Product Information</legend>
    <table border="0">
    	<tr>
    		<td>
    		<div align="left">Image: <%=rs.getBlob("ProdImage")+"\t" %></div>
    		</td>
    		<td>Product Name: <%=rs.getString("ProdName")+"\t" %>
    
    		<div align="left">Product Color: <%=rs.getString("ProdColor")+"\t" %></div>
    
    		<div align="left">Product Description: <%=rs.getString("ProdDesc")+"\t" %></div>
    
    		<div align="left">Product Price: <%=String.format("$%.2f",rs.getDouble("UnitPrice"))+"\t" %></div>
    
    		<div align="left">Quantity: <%=rs.getString("Quantity")+"\t" %></div>
    		
    	
    		
    	    
    
    		</td>
    	</tr>
    </table>
    </fieldset>
    
    <%
    			}
    			else {
    				//if no record is found, simply display a no record message
    		%>
    No record found.
    <%
    			}
    		 %>
    <p>&nbsp;</p>
    
    
    </body>
    </html>
    This is the code that I used to display all those information of my products including the details of the products like name, color,etc...

    However the outout is:

    Code:
    Product Information
    Image: com.mysql.jdbc.Blob@78aa80  
    Product Name: Yellow Beanie 
    Product Color: Yellow 
    Product Description: FEPPZ© label was derived from FEPPZ COMPANY and is inspired by the smart and casual style with a chic look for modern female! This beanie style headwear is vibrant in color that can lift one spirit up. 
    Product Price: $30.00 
    Quantity: 10  Product Information
    Is there any way whereby I can change display blob images from database instead of the url?

    Thanks
    Hope to receive your reply soon!

  2. #2
    Join Date
    Aug 2009
    Posts
    68
    Hi everybody !

    IMO, the reason is getBlob() returns a loblocator and not the actual data. In pure java you would have to write:

    Code:
    Blob bl = rs.getBlob(n);
    byte[] pict = bl.getBytes(0,howMuch);
    Hope this helps...

    LS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •