CLOB insertion – sample code

package com.mattiz.dao;

import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import oracle.jdbc.OraclePreparedStatement;
import oracle.sql.BLOB;
import oracle.sql.CLOB;

public class Query {

	public void insertClob() {

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		DataSource ds = getDS();
		try {
			conn = ds.getConnection();
			conn.setAutoCommit(false); // we must control the commit
			String sql = "INSERT INTO TEMP_TABLE(UID, CLOB_DATA) VALUES(?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, "1");
			ps.setClob(2, CLOB.empty_lob());
			ps.executeUpdate();
			PreparedStatement getEmptyClob = conn
					.prepareStatement("SELECT CLOB_DATA FROM TEMP_TABLE WHERE UID = ? FOR UPDATE");
			getEmptyClob.setString(1, "1");
			rs = getEmptyClob.executeQuery();
			rs.next();
			Clob clobFromTable = rs.getClob(1); // get the clob from rs
			((oracle.sql.CLOB) clobFromTable).putChars(1,
					"VERY LARGE STRING".toCharArray()); // update the clob
			rs.close();
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}