Displaying Mysql clob data using Servlet


 

Displaying Mysql clob data using Servlet

In this Section, we will display a clob data from a table of database using servlet

In this Section, we will display a clob data from a table of database using servlet

Displaying Mysql clob data using Servlet

.In this Section, we will display a clob data from a table of database 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.

Creating CLOB data type in Mysql table

In Mysql, CLOB data type is of 4 types--TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT. The maximum length of TINYTEXT has 255 character (8 bits), TEXT has 16,535 character (16 bits), MEDIUMTEXT has 16,777,216 character (24 bits)and LONGTEXT has 4,294,967,295 character (32 bits).

"article" table in "Mysql"


displayclob.java

import java.io.IOException;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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 displayclob extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
    Clob clobFile = null;
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;

    Integer id = 1;
    String query = "select Body from article where id = " + id;
    ServletOutputStream out = response.getOutputStream();
    response.setContentType("text/html");
    out.println("<html><head><title>Display Clob Example</title></head>");
    try {
      Class.forName("com.mysql.jdbc.Driver";
con =DriverManager.getConnection                                                     ("jdbc:mysql://192.168.10.13:3306/ankdb","root","root");
      stmt = con.createStatement();
      rs = stmt.executeQuery(query);
      if (rs.next()) {
        clobFile = rs.getClob(1);
      } else {
     out.println("<body><h4><font color='red'>There are no any File on id=" 
     + id + "</font></h4></body></html>");
        return;
      }
      long length = clobFile.length();
      String fileData = clobFile.getSubString(1, (int) length);
      out.println("<body><h4><font color='green'>Successfully Display The Record:
      </font></h4></body></html>");
      out.println(fileData);
    } catch (Exception e) {
      out.println("<body><h4><font color='red'>Unable to display" +e.getMessage()    + "</font></h4></body></html>");
      return;
    } finally {
      try {
        rs.close();
        stmt.close();
        con.close();
      } catch (SQLException e) {
        System.out.println(e);
      }
    }
  }
}

Output

Download this code

Ads