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.
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.*;
|
Output:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Retrieve image from database using Servlet View All Comments
Post your Comment