Share on Google+Share on Google+

Displaying Mysql clob data 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.


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.  


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"

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();
    out.println("<html><head><title>Display Clob Example</title></head>");
    try {
con =DriverManager.getConnection                                                     ("jdbc:mysql://","root","root");
      stmt = con.createStatement();
      rs = stmt.executeQuery(query);
      if ( {
        clobFile = rs.getClob(1);
      } else {
     out.println("<body><h4><font color='red'>There are no any File on id=" 
     + id + "</font></h4></body></html>");
      long length = clobFile.length();
      String fileData = clobFile.getSubString(1, (int) length);
      out.println("<body><h4><font color='green'>Successfully Display The Record:
    } catch (Exception e) {
      out.println("<body><h4><font color='red'>Unable to display" +e.getMessage()    + "</font></h4></body></html>");
    } finally {
      try {
      } catch (SQLException e) {


Download this code


Posted on: June 5, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.