[an error occurred while processing this directive]

Servlet Example To Delete Mysql Blob Data


BLOB data in SQL is a built-in type used for storing Binary Large Object. The BLOB type stores/retreives large binary objects such as PDF files, video clips, JPEG/GIF pictures, and Microsoft word documents. getBlob() and setBlob() methods from interfaces like ResultSet, CallableStatement and Prepared Statement can be used to access an SQL BLOB value. The Blob interface (java.sql.Blob) provides methods like length() to find the length of the value, position() to get the position of a pattern of bytes, getBytes() to retrieves all or part of the BLOB value as an array of bytes etc.

Creating The Table In Mysql Database:

Table             Create Table 
--------          -----------------------------------------
pictures                  CREATE TABLE `pictures` (
                     `id` int(11) NOT NULL auto_increment,
                     `image` blob,
                      PRIMARY KEY (`id`)
                      ) ENGINE=InnoDB DEFAULT CHARSET=latin1

Using java.sql.Blob interface in servlet (DeleteBlobExample) to delete the image from existing table according to the id. In this example we show you how to delete the image from Mysql table using servlet. 


import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.sql.DriverManager;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeleteBlobExample extends HttpServlet{
  public void doGet(HttpServletRequest request, HttpServletResponse response
IOException,ServletException {
    Connection con = null;
    PreparedStatement ps = null;
    Integer id = 9;
    ServletOutputStream out = response.getOutputStream();
    out.println("<html><head><title>Delete Blob Example</title></head>");
    try {
      con =DriverManager.getConnection ("jdbc:mysql://",
      ps = con.prepareStatement("delete from pictures where id = '9' ");
      ps.setInt(1, id);
      out.println("<body><h4><font color='green'>Successfully deleted the blob data 
      of given id</font></h4></body></html>"
    catch (Exception e) {
      out.println("<body><h4><font color='red'>File could not be deleted. <br><br>
       Exception Thrown:<br>  " 
+ e.getMessage() "</font></h4></body></html>");
    }finally {
      try {
      catch (SQLException e) {

Mapping of servlet (DeleteBlobExample.java) in web.xml


Run the servlet (DeleteBlobExample.java) on this url: http://localhost:8080/JavaExample/DeleteBlobExample . The data of the file will be displayed as below:

and if in case any error in database connection exists and there is no file on given id then the following message will be displayed.

Download Source Code


[an error occurred while processing this directive]