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.


 

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

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

Advertisements

Share on Google+Share on Google+

Servlet Example To Delete Mysql Blob Data

Posted on: July 4, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Servlet Example To Delete Mysql Blob Data  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads