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


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).



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


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

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



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



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.



It contains an image tag to display the image.



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



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.


  • Hana

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

  • shibo

    it is really awesome

  • 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.


    • ayesha

      your code works for me 🙂 but i want to insert image with the item description price fildes i tried to alter your code but i did get the output but .and i want to display all the image with its details in table format .i don’t want to retrive them using id
      ‘can you please say me the code i need help in my final year project,iam stuck in this particular process for a long time.
      thank you

  • Amal

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

  • Amal

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

  • fixa

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

  • ram

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

    • 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;’

  • vishal

    Solved my problem…
    Thanks admin 🙂

  • Ashit Panda

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

    If anyone can solve this error !!!

  • Ajay Gautam

    Thanks Buddy

  • Jack R

    thanks for this code…

  • Joy


  • 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.

    • 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.

  • 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 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.

  • hema

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

  • adil

    explain this line code

    byte byteArray[] = blob.getBytes(1, (int)blob.length());

  • harika

    hi,I would like to display details of a person along wwith his photo,how can i do this?


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.