how to retrieve images from database to jsp?

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();
  }
%>









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 text and images from mysql database and show on html page using jsp servlet
how to retrieve data from database
How to retrieve image from database
How to retrieve image from mysql database in JSP?
how to store and retrieve image from database
how to use bean to retrieve data 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 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 retrieve image from database using jsp and servlet?
How to Retrieve Data from the database and write into excel file using Java
retrieve from database........
JSP1
JSP2
how to send and retrieve image along with text to and from database
retrieve data from database with hyperlink
how to retrieve data into combo from database in java applet?
Retrieve date from MYSQL database
display multiple images from postgres database in jframe
how to retrieve image from mysql database using java and show it in HTML img tag ?
how to retrieve data from database using combobox value without using request.getParameter in jsp - JSP-Servlet
retrieve data from mysql database
Data retrieve from mysql database
pint multiple images from blob database
Retrieve values from database using views
retrieve data from database using jsf
Retrieve image from mysql database through jsp
Retrieve multiple data from database into Table.....
Retrieve value of radio button from database to form
How to retrieve record from table
retrieve data from mysql database and store it in a variable ?
Protect JSPs from direct access
how to retrieve the id value itself from the access database to drop down listbox in jsp
Dropdown code to retrieve result from oracle database
how to retrieve a checkbox value(retrieve from DB)
<img src=""> using retrieve image from database using jsp
How to Retrieve data from database in jsp
Retrieve database from the table dynamically in jsp from oracle using servlet
Retrieve data from database in swing application
Download images from Database in JSP

Ads