how to retrieve images from database to jsp?

Ads
 

how to retrieve images from database to jsp?

Hi sir,i want to stores images along with the name and price with database and retrieve it on jsp.and that image also should act as link.when i click on the image retrieved,it should display the elaborated image in th next page.plz help me in coding.its vey urgent.

View Answers

April 9, 2012 at 1:04 PM

1)images:

<%@ page import="java.sql.*,java.io.*,java.util.*" %> 
<HTML>
 <table border="1">
 <tr><th>ID</th><th>Image</th></tr>
   <%
  try{      
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
    Statement stmt=con.createStatement();
    String strQuery = "select * from data";
    ResultSet rs = stmt.executeQuery(strQuery);
    while(rs.next()){
       %>
      <tr>
      <td><%=rs.getInt("id")%></td>
      <td>
      <img src="image.jsp?imgid=<%=rs.getInt(1)%>" width="100" height="100">
</a></td>
      </tr>
      <%
    }
    rs.close();
    con.close();
    stmt.close();
  }
  catch(Exception e)
  {
    e.getMessage();
  }
  %>
 </table>
</HTML>

2)image.jsp:

<%@ page import="java.sql.*,java.io.*,java.util.*" %> 
<%
  int id =  Integer.parseInt(request.getParameter("imgid"));
  try{      
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");      
    Statement st=con.createStatement();
    String strQuery = "select image from data where id="+id;
    ResultSet rs = st.executeQuery(strQuery);

    String imgLen="";
    if(rs.next()){
      imgLen = rs.getString(1);
       }  
    rs = st.executeQuery(strQuery);
    if(rs.next()){
      int len = imgLen.length();
      byte [] rb = new byte[len];
      InputStream readImg = rs.getBinaryStream(1);
      int index=readImg.read(rb, 0, len);  
      st.close();
      response.reset();
      response.getOutputStream().write(rb,0,len); 
      response.getOutputStream().flush();        
    }
  }
  catch (Exception e){
    e.printStackTrace();
  }
%>

Ads









Related Tutorials/Questions & Answers:
how to retrieve images from database to jsp?
how to store and retrieve images int and from the database using php
Advertisements
how to retrieve data from database ?????/
how to retrieve data from database ?????/
how to retrieve data from database ?????/
how to retrieve data from database ?????/
how to retrieve data from database
how to retrieve text and images from mysql database and show on html page using jsp servlet
How to retrieve image from database
How to retrieve image from mysql database in JSP?
how to use bean to retrieve data from database
how to store and retrieve image from database
How to retrieve image from database in Servlet?
How to retrieve and display image from database in Java?
How to retrieve blob image from database in JSP?
How to store and retrieve image from database in JSP?
how we retrieve image from database
How to retrieve image from database using jsp and servlet?
How to Retrieve Data from the database and write into excel file using Java
How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java
How to insert and retrieve Questions with special symbols and images into database in an ONLINE EXAMINATION project?
how to send and retrieve image along with text to and from database
how to retrieve data into combo from database in java applet?
Retrieve date from MYSQL database
how to retrieve image from mysql database using java and show it in HTML img tag ?
retrieve from database........
retrieve data from database with hyperlink
How to retrieve record from table
display multiple images from postgres database in jframe
retrieve data from mysql database
Retrieve values from database using views

Ads