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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Servlet Example To Display Mysql Clob Data 
 

A Clob is a Character Large Object in a Database. Clob can be very large depending on the Database up to 4 GB.

 

Servlet Example To Display Mysql Clob Data

                         

What is Clob? 

A Clob is a Character Large Object in a Database. Clob can be very large depending on the Database up to 4 GB. It is a rich data type of JDBC. A large character data file can be stored as CLOB type. JDBC provides java.sql.Clob interface for handling large character/text object.

Mysql Clob

According to the Mysql, there are four text types TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT which can be taken as CLOB type and have the maximum lengths and storage requirements. 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).

How to define Clob Datatype in Mysql Table

In the table below, you can see "Body" field as "longtext" type in the table "article". Here, Body field represents Clob data.

CREATE TABLE `article` ( 
             `ID` int(11) NOT NULL, 
              `Subject` varchar(256) NOT NULL, 
              `Body` longtext, 
              PRIMARY KEY (`ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

Display Mysql Clob Data

We have developed a servlet (DisplayClobExample) that accepts the ID of a file and displays the associated file.

DisplayClobExample.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 DisplayClobExample 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.59:3306/example",
        "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(intlength);
      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);
      }
    }
  }
}

Mapping of servlet (DisplayClobExample) in web.xml

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

Run the servlet (DisplayClobExample) by url: http://localhost:8080/JavaExample/DisplayClobExample . 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.