Converting a String To a CLOB and Converting a CLOB to a String
The following code snippets demonstrate: (1) how to convert a Java String object to an Oracle CLOB object and (2) how to convert an Oracle CLOB object into a Java String object. The code assumes you know how to define a JDBC connection and that you have imported the Oracle SQL types library. I did not invent this method, but I did have a hard time finding an example that filled in all of the blanks. Let me know if you have questions. Here is the code:
try { String v_clob_string = <STRING OF ANY LENGTH>; Connection conn = <define JDBC Connection>; CLOB v_clob = CLOB.createTemporary(conn,false,CLOB.DURATION_CALL); CallableStament cs = conn.prepareCall("begin ClobPackage.insertClob(?); end;"); int x = v_clob.putString(1,p_clob_string);
cs.setClob(1,v_clob);
cs.execute();
cs.close();
}catch(Exception e){
<Your exception handling code>
}finally{
<Might want to clean up connections here>
}
try { Connection conn = <define JDBC Connection>; CallableStament cs = conn.prepareCall("begin ? = ClobPackage.getClob; end;"); cs.registerOutParameter(1,OracleTypes.CLOB);
cs.execute();
Clob v_clob = cs.getClob(1); long v_len = v_clob.length(); String v_clob_string = v_clob.getSubString(1,(int)v_len);
cs.close();
}catch(Exception e){
<Your exception handling code>
}finally{
<Might want to clean up connections here>
}
5:07:24 PM
|