[an error occurred while processing this directive]

Servlet Example To Insert Mysql Clob Data


This example shows how to insert CLOB data in the database. In our example, servlet InsertClobExample takes url for the CLOB data (the url will be pointing to a text file). Therefore, we will represent the CLOB as a url.

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 create Clob Data type in Mysql Table

CREATE TABLE `article` ( 
             `ID` int(11) NOT NULL, 
              `Subject` varchar(256) NOT NULL, 
              `Body` longtext, 
              PRIMARY KEY (`ID`) 

The following servlet InsertClobExample shows how to insert data of CLOB type in mysql database through the servlet program. It has three variables:

  • id (the ID of file).
  • fileName (the name of file)
  • sourceURL (the url of file representing the CLOB; the servlet will open the url, construct a CLOB and insert it into the CLOB column of the article table)

Let's insert a new record with the following data in the article table:

Source code of InsertClobExample.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 InsertClobExample extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response
IOException,ServletException {
    String clobData = null;
    Connection con = null;

    Integer id = 1;
    String fileName = "san";
    String sourceURL = "http://localhost:8080/JavaExample/images/san.dat";
    ServletOutputStream out = response.getOutputStream();
    out.println("<html><head><title>Insert Clob Example</title></head>");
    try {
      con =DriverManager.getConnection ("jdbc:mysql://
      clobData = getClobDataAsString(sourceURL);
      insertClob(con, id, fileName, clobData);
      out.println("<body><h4><font color='green'>Successfully insert 
      Your Record with id= " 
+ id + "</font></h4></body></html>");
    catch (Exception e) {
      out.println("<body><h4><font color='red'>Unable to insert " 
      + e.getMessage
() "</font></h4></body></html>");
  public void insertClob(Connection con, Integer id, String fileName, 
      String fileData
)throws Exception{
    PreparedStatement ps = null;
    try {
      ps = con.prepareStatement("insert into article(id, subject, body)
       values (?, ?, ?)"
      ps.setInt(1, id);
      ps.setString(2, fileName);
      ps.setString(3, fileData);
    }catch(Exception e){
    }finally {
  public static String getClobDataAsString(String urlDatathrows Exception {
    InputStream is = null;
    try {
      URL url = new URL(urlData);
      URLConnection urlConn = url.openConnection();
      is = urlConn.getInputStream();
      int BUFFER_SIZE = 1024;
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      int length;
      byte[] buffer = new byte[BUFFER_SIZE];
      while ((length = is.read(buffer)) != -1) {
        output.write(buffer, 0, length);
      return new String(output.toByteArray());
    finally {

 Mapping of servlet (InsertClobExample.java) in web.xml file:


Run the servlet (InsertClobExample.java) on this url: http://localhost:8080/JavaExample/InsertClobExample.

In case of any error in the database connection then the following error message will be displayed.

Download Source Code


[an error occurred while processing this directive]