Home Servlets Retrieving the Image from a database Table
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Retrieving the Image from a database Table

Advertisement
This page discusses - Retrieving the Image from a database Table

Retrieving the Image from a database Table

Consider a case where we want that along with the name of the person and its information, his image should also come. For this it is necessary that there must be his/her image in the database so that whenever needed we can retrieve it.  In this program we are going to retrieve the image from the database table. You can do it very easily after going through this program. 

To get the program working we need to use a doGet() method to write our business logic as it is server side programming so all the processing will be done by the container.  First of all make a class named JdbcSelectingImage, the name of the class should be such that the person can understand what the program is going to do. This class must extend the HttpServlet class which is an abstract method.  Now inside the doGet() method call the method getWriter() of the class PrintWriter. To retrieve an image from the database table our java program need to make a connection with the MySql database. The database can be any, it all depends on the person's choice.  After the connection establishment we will pass a query for retrieving the image from the database table in prepareStatement() method of the Connection object which returns the PreparedStatement object.

As we have to retrieve the image from the database table so there is need of a ResultSet object which will help us to retrieve the image from the database. The ResultSet obiect will be retrieved from calling the executeQuery() method of the PreparedStatement object.. To get the image from the database use getBinaryStream() method of the ResultSet interface.  

The code of the program is given below:

The output of the program is given below:

Download this example.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Ask Questions?    Discuss: Retrieving the Image from a database Table  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
elango
December 1, 2011
jsp

nice
Mansa
August 14, 2012
hello can u help me out for this

How to Retrieve the Audio clip stored in table in MYSQL and play it?????
S.Manjunath Reddy
May 11, 2013
Retrive image from database using jsp.

hai sir, i am S.Manjunath Reddy.two days back i taken upload imagepath stored in database and image will store in another folder.how to retrive the image from database.so send me code sir. thank u sir
DMCA.com