Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Retrieve image from database using Servlet

In this example we will show you how to develop a Servlet that connects to the MySQL database and retrieves the image from the table.



Retrieve image from database using Servlet


In this example we will show you how to develop a Servlet that connects to the MySQL database and retrieves the image from the table. After completing this tutorial you will be able to develop program for your java based applications that retrieves the image from database. You can use this type of program to retrieve the employee image in HR application. In case social networking site you can save the user's photo in database and then retrieve the photo for display.

Our Servlet connects to the MySQL database and then retrieves the saved password. Here is the structure of MySQL table used in this program.

In this example the Image field will be blob and access by image id.

How to Compile Servlet program

1. Save your file same name as class name.

2. Map your servlet in web.xml file.

3. Open Command Prompt and give appropriate path of your class file.

4.  Compile your servlet class file by using javac .

5. Run your program on the Browser by url-pattern which is define in web.xml file.

MySql Table Structure:

CREATE TABLE `pictures` (
`id` int(11) NOT NULL auto_increment,
`image` blob,

Here is the Example: 

import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayImage extends  HttpServlet{
  public void doGet(HttpServletRequest request, HttpServletResponse 
throws ServletException, IOException{
  //PrintWriter pw = response.getWriter();
  String connectionURL = "jdbc:mysql://";
  java.sql.Connection con=null;
  Statement st1=con.createStatement();
  ResultSet rs1 = st1.executeQuery("select image from"+
   " pictures where id='5'"
  String imgLen="";
  imgLen = rs1.getString(1);
  rs1 = st1.executeQuery
"select image from pictures where id='5'");
  int len = imgLen.length();
  byte [] rb = new byte[len];
  InputStream readImg = rs1.getBinaryStream(1);
  int, 0, len);  
  catch (Exception e){


Download Source Code


Share on Google+Share on Google+

Retrieve image from database using Servlet

Posted on: June 20, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Retrieve image from database using Servlet   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
swapnil gadge
March 30, 2011

nice code in servlet
September 5, 2011
java problems

after executing i got an error on browser as The image â??http://localhost:8084/uvc/DisplayImageâ?? cannot be displayed because it contains errors. plz write me how can be resolve it.
hari hara sudhan .p
January 12, 2012

servlet is addtional package to be downloaded uh
April 14, 2012
image insertion

this image insertion code is working but i am not able to move to any place in page what should i do to do so
January 2, 2013

This is a perfect help for fresher thanks roseindia team
April 16, 2013
How to re-size the image

Thanks for the code it really helped. How can i now re-size the image i have downloaded from the database so that it can fit nicely on the part of the page i want not to fill the whole page?
Rohit Singhal
July 22, 2013
regarding the diplayimage servlet

i tried the code but its not working please help.. IT says navigation to webpage is cancelled and gives a dialog box for saving ..
April 22, 2014
Regarding above mentioned code.

My image is of datatype image in sql server 2008.I tried the above mentioned code in servlet and it is not working.The image is not getting displayed.
July 16, 2014
retrieve the image from database

with the above code, in a jsp page I retrieved only first row image from database instead of alll the image ,,I want to retrieve all of the images.