Deleting Mysql Clob data using servlet


 

Deleting Mysql Clob data using servlet

In this Section, we will discuss about how to delete a Clob data from a database table using servlet.

In this Section, we will discuss about how to delete a Clob data from a database table using servlet.

Deleting  Mysql Clob data using servlet

In this Section, we will discuss about how to delete a Clob data from a database table using servlet. A CLOB is a Character Large Object in a Database table. CLOB data is used to store a block of text. It is designed to store ASCII text data including formatted text such as HTML. CLOB values are not stored as a part of the row of the database table, they are usually allocated space in whole disk pages.

Advantages

1.You can read or write any portion of the CLOB data.

2.Equallity of 2 CLOBs can  be check by using equals operator.

3.Some default characteristics for the column ,can be override by 'application programmer' ,when they create a CLOB object.  

Disadvantage

1.Due to allocation of whole disk pages, a short 'CLOB' wastes space.

2.Ristriction on how  you can use a CLOB column in an sql statement.

deleteclob.java

import java.io.*;
import java.util.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.sql.PreparedStatement;
import java.io.ByteArrayOutputStream;

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 deleteclob extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
    response.setContentType("text/html");
    Connection con = null;
    PreparedStatement ps = null;

    Integer id = 2;
    ServletOutputStream out = response.getOutputStream();
    out.println("<html><head><title>Delete Clob Example</title></head>");
    try {
      Class.forName("com.mysql.jdbc.Driver");
      con =DriverManager.getConnection ("jdbc:mysql://192.168.10.13:3306/ankdb", "root", "root");
      ps = con.prepareStatement("delete from article where id = ?");
      ps.setInt(5, id);
      ps.executeUpdate();
      out.println("<body><h4><font color='green'>Successfully deleted clob data of id= " 
+ id + "</font></h4></body></html>");
    } catch (Exception e) {
      e.printStackTrace();
      out.println("<body><h4><font color='red'>Unable to delete " + e.getMessage() + "</font></h4></body></html>");
    }finally {
      try {
        ps.close();
        con.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}

OUTPUT

DOWNLOAD SOURCE CODE

Ads