Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Convert String to CLOB and Insert into Oracle database

843854Jan 23 2003 — edited Nov 20 2014
Hi,

I spent some time working on this, and I thought I would share it within the forum. This solution is unique to Oracle, so consider it's use carefully. It's the simplest coding I could come up with, and I have been able to insert Strings of any length using this technique with both the oci8 and thin driver when executed using Java 1.4 and Oracle 9.2. No guarantees that it will work for you. Let me know if it helps you out.
import java.sql.*;
import java.util.*;
import java.text.*;

/* -------------------------------------------------------------------
 * This program demonstrates a technique for converting a large String
 * to an oracle.sql.CLOB then inserting that CLOB into a database.  
 * I believe most of this is specific to Oracle, specifically the 
 * ability to create a temporary CLOB within your program.
 * -------------------------------------------------------------------
 */
class TestOraCLobInsert {
	
    public static void main (String args []) throws SQLException {
			
        try {

            DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
            Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@server:1521:sid", "uid", "pwd");

            PreparedStatement ps = conn.prepareStatement("INSERT INTO CLOBTABLE VALUES (?)");
            
            oracle.sql.CLOB newClob = oracle.sql.CLOB.createTemporary(conn, false, oracle.sql.CLOB.DURATION_CALL);
            
            newClob.putString(1,"This string, up to 4 gigabytes will be inserted into the CLOB");
            
            ps.setClob(1, newClob);
            
            int rowcnt = ps.executeUpdate();
            
            System.out.println("Successful update of "+rowcnt+" row");
            
            ps.close();
            conn.close();            
        }
        catch (Exception e) {
            System.out.println("Java Exception caught, error message="+e.getMessage());
        }
    }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 10 2010
Added on Jan 23 2003
41 comments
13,825 views