Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
 

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 file_name.java .

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,
PRIMARY KEY (`id`)
)

Here is the Example: 

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

public class DisplayImage extends  HttpServlet{
  public void doGet(HttpServletRequest request, 
      HttpServletResponse response
throws ServletException, IOException{
    //PrintWriter pw = response.getWriter();
    String connectionURL = "jdbc:mysql://192.168.10.59:3306/example";
    java.sql.Connection con=null;
    try{      
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      con=DriverManager.getConnection(connectionURL,"root","root");      
      Statement st1=con.createStatement();
      ResultSet rs1 = st1.executeQuery("select image from pictures where id='5'");
      String imgLen="";
      if(rs1.next()){
        imgLen = rs1.getString(1);
        System.out.println(imgLen.length());
      }  
      rs1 = st1.executeQuery("select image from pictures where id='5'");
      if(rs1.next()){
        int len = imgLen.length();
        byte [] rb = new byte[len];
        InputStream readImg = rs1.getBinaryStream(1);
        int index=readImg.read(rb, 0, len);  
        System.out.println("index"+index);
        st1.close();
        response.reset();
        response.setContentType("image/jpg");
        response.getOutputStream().write(rb,0,len);
        response.getOutputStream().flush();        
      }
    }
    catch (Exception e){
      e.printStackTrace();
    }
  }
}

Output:

Download Source Code

                         

» View all related tutorials
Related Tags: c database error data input servlet user screen display name this message ai tab check if example with exists to

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

thanks sir...
but i want to mutiple images retrieve from DB2 is difficult

Posted by kannan on Saturday, 12.13.08 @ 04:15am | #82710

i tried the above code
but i am getting null pointer exception for integer value len
can u clarify it
thnx in advance

Posted by diwakar on Thursday, 09.18.08 @ 15:39pm | #80298

 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.