Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Servlet Example To Update Mysql Clob Data

This example shows how to update 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).

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

Servlet Example To Update Mysql Clob Data

  

This example shows how to update 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).

Table 'article' in Mysql:

The structure of the table 'article' is as follows:

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

Creating 'UpdateClobExample' servlet:

The following servlet UpdateClobExample shows how to update data of CLOB type in mysql database through the servlet program. It has two variables:

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

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

Source code of UpdateClobExample.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 UpdateClobExample extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) 
  throws IOException,ServletException {
  response.setContentType("text/html");
  String clobData = null;
  Connection con = null;

  Integer id = 1;
  String sourceURL = "http://fddfg:8080/JavaExample/images/large.dat";
  ServletOutputStream out = response.getOutputStream();
  out.println("<html><head><title>Update Clob Example</title></head>");
  try {
  Class.forName("com.mysql.jdbc.Driver");
  con =DriverManager.getConnection ("jdbc:mysql://192.168.10.59:3306/
  example","root", "root");
  clobData = getClobDataAsString(sourceURL);
  updateClob(con, id, clobData);
  out.println("<body><h4><font color='green'>Successfully update Your 
   Record with id=" + id + "</font></h4></body></html>");
  } catch (Exception e) {
  e.printStackTrace();
  out.println("<body><h4><font color='red'>Unable to update  " 
  + e.getMessage() + "</font></h4></body></html>");
  }
  }
  public void updateClob(Connection con, Integer id, String fileData)
  throws Exception{
  PreparedStatement ps = null;
  try {
  ps = con.prepareStatement("update article set body = ? where id = ?");
  ps.setInt(2, id);
  ps.setString(1, fileData);
  ps.executeUpdate();
  }catch(Exception e){
  System.out.println(e);
  }finally {
  ps.close();
  }
  }
  public static String getClobDataAsString(String urlData) throws Exception {
  InputStream is = null;
  try {
  URL url = new URL(urlData);
  System.out.println("url"+url);
  URLConnection urlConn = url.openConnection();
  urlConn.connect();
  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 {
  is.close();
  }
  }
} 

 Mapping of servlet (UpdateClobExample) in web.xml file:

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

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

 

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

Download Source Code

Advertisements

Online Training Classes

Share on Google+Share on Google+

Servlet Example To Update Mysql Clob Data

Posted on: July 4, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Servlet Example To Update Mysql Clob Data  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads