Monday, February 3, 2014

Oracle CLOBs with JDBC getString

In newer releases of Oracle, the preparedStatement.getString() is "overloaded" to allow you to pull back information directly from a CLOB field without using the special clob object.  Its generally good up to around 32KB;  In order to go larger, use the Java code below.

 String url   = “jdbc:oracle:thin:@localhost:1521:orcl”;  
 String user   = “scott”;  
 String password = “tiger”;

 Properties props = new Properties();  
 props.put(“user”, user );  
 props.put(“password”, password);  
 props.put(“SetBigStringTryClob”, “true”);

 DriverManager.registerDriver(new OracleDriver());  
 Connection conn = DriverManager.getConnection(url, props);  

Generally speaking, this should not be used too frequently.  In most instances, you'll want to stream the object properly, however, if the size of your strings are dancing on the edge of VARCHAR2 limits, you can get away with this workaround to simplify things.