| Author |
Insert a 7k String into Oracle CLOB?
|
Fahd Shariff
Ranch Hand
Joined: Nov 22, 2002
Posts: 38
|
|
I've been struggling to store a huge string (7k) into an Oracle CLOB column. This is what I have tried so far: 1. <blockquote>code: <pre name="code" class="java">String toStore="a long string" ; Reader sReader = new StringReader(toStore); int readerLength = toStore.toCharArray().length; insertStatement.setCharacterStream(2, sReader, readerLength);</pre> </blockquote> This inserts junk characters into the table with both thin and oci drivers. 2. <blockquote>code: <pre name="code" class="java">CLOB clob = CLOB.createTemporary(connection, true, oracle.sql.CLOB.DURATION_SESSION); clob.trim(0); Writer writer = clob.getCharacterOutputStream(); writer.write(toStore.toCharArray()); writer.flush(); writer.close(); insertStatement.setCLOB(2, clob);</pre> </blockquote> This fails with ORA-12704: character set mismatch 3. This consists of two steps: a) Insert EMPTY_CLOB() into clob table b) Select the CLOB column from the table for update and then: <blockquote>code: <pre name="code" class="java">oracle.sql.CLOB clob = ((oracle.jdbc.OracleResultSet) resultSet).getCLOB("clob_column"); Writer writer = clob.getCharacterOutputStream(); writer.write(toStore.toCharArray()); writer.flush(); writer.close();</pre> </blockquote> This one works perfectly, but with the overhead of two calls to the database. Does anyone have any neater way of storing a large clob object? Any help would be appreciated! Thanks
|
Fahd Shariff<br />"Let the code do the talking"
|
 |
Fahd Shariff
Ranch Hand
Joined: Nov 22, 2002
Posts: 38
|
|
I have tried various methods and finally found a couple of solutions to this problem. Please read my blog, to find out everything I tried as it is too much to post here: Big Strings and Oracle Clobs
|
 |
Scott Selikoff
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
Still confused about what you mean by junk characters... is it possible it just converted to a different character encoding? Did you check the character encoding properties of the database? I would think attempt #4 (setCharacterStream) would work fine if you set the encoding properly. [ July 16, 2008: Message edited by: Scott Selikoff ]
|
My Blog: Down Home Country Coding with Scott Selikoff
|
 |
Purushoth Thambu
Ranch Hand
Joined: May 24, 2003
Posts: 425
|
|
|
You could have used setAsciiStream API in prepared statement interface. CLOB is specific to Oracle
|
 |
Scott Selikoff
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
|
Good point Purushothaman. Clobs are fine from a database-perspective, but most JDBC developers skip Clobs and rely on Blob/Stream interface methods.
|
 |
Fahd Shariff
Ranch Hand
Joined: Nov 22, 2002
Posts: 38
|
|
I tried using setAsciiStream, but that produced junk as well: ByteArrayInputStream bis = new ByteArrayInputStream(bigString.getBytes()); st.setAsciiStream(1, bis, bigString.getBytes().length);
|
 |
Fahd Shariff
Ranch Hand
Joined: Nov 22, 2002
Posts: 38
|
|
This is the junk I get back. Could be a different character set. Is there any way I can set the character set in jdbc? My Oracle NLS_CHARACTERSET is WE8ISO8859P1. �������������������������>�����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������>������������������������������������>������������������������������������>������������������>���������������������������������������������������������>������������������>������������������>��� ���������������������������������>������������������������������������>������������������>������������������������������������>������������������������������������>������������������������������������>������������������������������������>������������������������������������>������������������>��������������������������������������>�������������������������������������������������������>������������������>�����������������>����������������������������������� �>������������������>������������������������������������>�����������������������������������>����������������������������������������������������������������������������������������������������������������>����������������������������������>������������������������������������>��������������������������������������������������������������������������������������������>�����������������>�����������������>������������������>�����������������>������������������& gt;�������������������/�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������'������������������������������������������������������������������������������������������������������������������������������������������������������� ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������>������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������ �������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� �����������������������������>������������>������������>������������>�������>������������>������������>������������>������������>������������>������������>������������>������������>������������>������������>��������������������>������������>������������>������������>������������>������������>������������>������������>�������>������������>������������>������������>������������>������������>������������>����� �������>�������������������������������������������������������������������������������>������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������>�������������������������>����������������������������������������������>��������������������������������������������������������������������>��������������������������
|
 |
 |
|
|
subject: Insert a 7k String into Oracle CLOB?
|
|
|