Thursday 21 March 2013

CLOB to String Conversion

Below code shows the two ways to convert database CLOB to String code.

1) Convert CLOB to String using BufferedReader:

import java.io.BufferedReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author http://java-sample-program.blogspot.in/  
 * Convert CLOB to String using BufferedReader
 */
public class ClobTest {

 public static void main(String[] args) throws Exception{
 Connection con = null;
 PreparedStatement pstmt = null;
 ResultSet rs = null;
 
 try {
 
  Class.forName("oracle.jdbc.driver.OracleDriver");
  con = DriverManager.getConnection("jdbc:oracle:thin:@IPAddress:Port:Database",
    "username","password");
  pstmt = con.prepareStatement("SELECT clobCol FROM TableName");
  rs = pstmt.executeQuery();
  
  while(rs.next()){   
   
   StringBuffer strOut = new StringBuffer();
   String aux;
   try {
    BufferedReader br = new BufferedReader(rs.getClob("clobCol").
      getCharacterStream());
    while ((aux=br.readLine())!=null) {
     strOut.append(aux);
     strOut.append(System.getProperty("line.separator"));
    }
   }catch (Exception e) {
    e.printStackTrace();
   }
   String clobStr = strOut.toString();
   System.out.println(clobStr);
  } 
  
 }catch(Exception e){
  e.printStackTrace();
 } finally {
  try{
   if(pstmt != null){ 
    pstmt.close();
   }
   if(rs!= null){ 
    rs.close();
   }     
   if(con!= null){
    con.close();     
   }
  }catch (SQLException e) {
   e.printStackTrace();
  }
 }
 
 }
 
}

Use the above method if a very large Clob can be returned by the database.

2) Convert CLOB to String using getSubString() method:

import java.io.BufferedReader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author http://java-sample-program.blogspot.in/  
 * Convert CLOB to String using getSubString() method
 */
public class ClobTest {

 public static void main(String[] args) throws Exception{
 Connection con = null;
 PreparedStatement pstmt = null;
 ResultSet rs = null;
 
 try {
 
  Class.forName("oracle.jdbc.driver.OracleDriver");
  con = DriverManager.getConnection("jdbc:oracle:thin:@IPAddress:Port:Database",
    "username","password");
  pstmt = con.prepareStatement("SELECT clobCol FROM TableName");
  rs = pstmt.executeQuery();
  
  while(rs.next()){   
   Clob clob = rs.getClob("clobCol");
   if (clob != null) {  
    String clobStr = clob.getSubString(1, (int) clob.length());
    System.out.println(clobStr);
   }
  } 
  
 }catch(Exception e){
  e.printStackTrace();
 } finally {
  try{
   if(pstmt != null){ 
    pstmt.close();
   }
   if(rs!= null){ 
    rs.close();
   }     
   if(con!= null){
    con.close();     
   }
  }catch (SQLException e) {
   e.printStackTrace();
  }
 }
 
 }
 
}

11 comments:

  1. Hi! How to update CLOB field using java JDBC. I need to write one method that can update a CLOB, field but I can't yet.

    ReplyDelete
  2. It is a good website to learn Java. I am also writing many useful java programs for free learn. You can check them at http://www.javatheprogram.blogspot.com.

    ReplyDelete
  3. thank you man the second method is what i needed for my jasper report.

    ReplyDelete
  4. getSubString
    worked for me
    thanks

    ReplyDelete
  5. getSubString also worked for me... thanks a lot

    ReplyDelete
  6. Great Article I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because it becomes more and more interesting from the starting lines until the end. So Thank you for sharing a COOL Meaningful stuff with us Keep it up..!

    SAP training in Chennai

    ReplyDelete
  7. This information really worth saying, i think you are master of the content and thank you so much sharing that valuable information and get new skills after refer that post.
    Interior Decorators in Chennai
    Home Interior Designers in Chennai
    Home Interiors in Chennai

    ReplyDelete

/* */