Showing posts with label write CLOB column. Show all posts
Showing posts with label write CLOB column. Show all posts

Thursday 21 March 2013

How to write CLOB fields

Below code shows the way to write CLOB fields to the database:

import java.io.ByteArrayInputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author http://java-sample-program.blogspot.in/  
 * Write CLOB to database
 */
public class ClobTest {

 public static void main(String[] args) throws Exception{
 Connection con = null;
 CallableStatement stmt = null;
 
 try {
 
  Class.forName("oracle.jdbc.driver.OracleDriver");
  con = DriverManager.getConnection("jdbc:oracle:thin:@IPAddress:Port:Database",
    "username","password");
  stmt = con.prepareCall("{call CLOBTEST.saveClob(?)}");
  String xmlString = "1234567890";
  
  stmt.setAsciiStream(1,new ByteArrayInputStream(xmlString.getBytes()),
     xmlString.getBytes().length);
  
  stmt.execute();
  System.out.println("executed successfully");
  
 }catch(Exception e){
  e.printStackTrace();
 } finally {
  try{
   if(stmt != null){ 
    stmt.close();
   }
   if(con!= null){
    con.close();     
   }
  }catch (SQLException e) {
   e.printStackTrace();
  }
 }
 
 }

}