Save and Retrieve Image from MySQL Database Using Servlet and JSP

Here you will learn how to save and retrieve image from mysql database using servlet and jsp.

This method will work with Servlet 3.0 and MySQL 5.5 and higher versions.

Also Read: How to Connect Java (JDBC) with MySQL or Oracle Database

Save and Retrieve Image from MySQL Database Using Servlet and JSP

Database

Create a table in mysql database with following schema.

Database Name: test

Table Name: data

Save and Retrieve Image from MySQL Database Using Servlet and JSP

Here I have used MEDIUMBLOB datatype to store image in database. It supports maximum 16 MB file. You can also use other types like TINYBLOB (255 bytes), BLOB (64 KB), LONGBLOB (4 GB).

 

Project

Create a dynamic web project with name ImageUploadDownload. The project have following files.

index.jsp

It contains a form to choose image to upload. When user chooses an image and submit form, the request is sent to UploadImage.java.

 

UploadImage.java

It is a servlet that is responsible for storing image in database.

 

result.jsp

This page shows success if image is successfully saved in database otherwise shows error.

 

view.jsp

It contains a form with a text field. User have to enter unique image id and submit the form. The request is sent to viewImage.jsp page.

 

viewImage.jsp

It contains an image tag to display the image.

 

getImage.jsp

It is responsible for fetching image form database with given image id and display it.

 

Screenshots

Save and Retrieve Image from MySQL Database Using Servlet and JSP Save and Retrieve Image from MySQL Database Using Servlet and JSP

Feel free to ask your queries in comment section.

20 comments

  • Hana

    Hi this has been really helpful! I was wondering how do I upload several images at once? 🙂

    Reply
  • shibo

    it is really awesome

    Reply
  • Neha

    I want to display all the images in the table column, how can I do that? I don’t want to get photo by id, I want to display all images from a table onto jsp (front).

    Please help me, I’m stuck here for a long time & not getting any solution for this. I want to implement this in my final project please reply asap.

    thanks

    Reply
  • Amal

    Hello , plz I need your helps , I want to display image from table column using servlet and jsp (user’s profil picture )

    Reply
  • Amal

    I puted the same code , db …. not working 🙁 plz i need your helps !!!

    Reply
  • fixa

    sir, can you show the process from upload,edit until delete??

    Reply
  • ram

    Field ‘id’ doesn’t have a default value i have an error sir

    Reply
    • vishal

      Your id is not taking any value… thats why it is showing that error @ram
      Just make run this command ‘ALTER TABLE MODIFY COLUMN id INT auto_increment;’

      Reply
  • vishal

    Solved my problem…
    Thanks admin 🙂

    Reply
  • Ashit Panda

    It shows an error-
    Method com/mysql/jdbc/PreparedStatement.setBlob(ILjava/io/InputStream;)V is abstract
    com.mysql.jdbc.PreparedStatement.setBlob(PreparedStatement.java)

    If anyone can solve this error !!!

    Reply
  • Ajay Gautam

    Thanks Buddy

    Reply
  • Jack R

    thanks for this code…

    Reply
  • Joy

    Thanks

    Reply
  • Adrian Mills

    Hello, I really like your code it is awesome looking, I am going to try and run it soon as I am looking for an image
    upload in jsp to mysql database and I really like want you have come up with, however I was wondering do I need a web.xml to go along with this code or not? if So can you please provide the exact web.xml code so i can plug it in my WEB-INF folder, thank you very much. Adrian Mills.

    Reply
    • Adrian Mills

      Ok! I got the servlet and all the pages with the web.xml and everything is working properly, however the message that is written in the servlet code for result.jsp is displaying on the page “Some Error Occurred” is coming up, but I know the prgram is working correctly and everything, but I am getting this message on the result.jsp page, can you please help and tell me what the problem is, why i am getting this message, thank you very much. Adrian Mills.

      Reply
  • Adrian Mills

    hello, never mind about the error that I was getting from the jsp page , I have figured it out, there was a bug in the mysql.connector.java it was an older connector, and I downloaded the latest version and a later version of mysql and then everything worked perfect thanks again for the code it is awesome, I like this website too, thanks again, bye. Adrian Mills.

    Reply
  • hema

    sir, what should be the type of image of bean class so that it would be compatible with inputstream

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *