Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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. 

DeleteBlobExample.java

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
     throws 
IOException,ServletException {
    response.setContentType("text/html");
    Connection con = null;
    PreparedStatement ps = null;
    Integer id = 9;
    ServletOutputStream out = response.getOutputStream();
    out.println("<html><head><title>Delete Blob Example</title></head>");
    try {
      Class.forName("com.mysql.jdbc.Driver");
      con =DriverManager.getConnection ("jdbc:mysql://192.168.10.59:3306/example",
       
"root""root");
      ps = con.prepareStatement("delete from pictures where id = '9' ");
      ps.setInt(1, id);
      ps.executeUpdate();
      out.println("<body><h4><font color='green'>Successfully deleted the blob data 
      of given id</font></h4></body></html>"
);
    catch (Exception e) {
      e.printStackTrace();
      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 {
        ps.close();
        con.close();
      catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}

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

<servlet>
    <servlet-name>DeleteBlobExample</servlet-name>
    <servlet-class>DeleteBlobExample</servlet-class>
</servlet> 
<servlet-mapping>
    <servlet-name>DeleteBlobExample</servlet-name>
    <url-pattern>/DeleteBlobExample</url-pattern>
</servlet-mapping>

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

                         

» View all related tutorials
Related Tags: java sql mysql c database api ide jdbc data batch application io get vi exec state int bat this id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.