问题
At my new/first job We have an oracle database with one of its fields set to LOB(large objects) data type. When I run a select statement , I get this issue, which gives me an error as following.
ERROR:
ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 10228,
maximum: 4000)
This size of the string(which are logs) inside each tuble is 10228 more than the Max capacity of the buffer . Now my boss has asked me to write a java class that will make us able to
retrieve the entire string.
I need some guidance on how to approach solving this issue . * If lets say, I have the java class ready how can I configure it to work with oracle, beside(server name, user. pass)? is it going to be placed in the same directory as JDBC and other jar files? . Also do you think the links below are relevant at all.
http://docs.oracle.com/cd/F49540_01/DOC/java.815/a64685/oraext4.htm#1003223
http://docs.oracle.com/cd/F49540_01/DOC/java.815/a64685/samapp2.htm
http://www.idevelopment.info/data/Programming/java/jdbc/LOBS/BLOBFileExample.java
BTW, I was able to come up with a temp solution as follow:
Select dbms_lob.substr( BLOB_FieldName, 4000, 1 )
from Database name Where [Condition];
with this query I was able to get the first 4000 bytes of the string which is great, but we need the entire string. And then after reading about utl_raw.cast_to_varchar2 I tried the following
Select utl_raw.cast_to_varchar2( dbms_lob.substr( BLOB_fieldName, 19000, 1 ) )
from[dbname];
I used utl_raw.cast_to_varchar2 because I read somewhere online that [Varchar2] datatype can hold up to 32k byte and thought of casting [LOB] data type to Varchar2 , but it did not work.
FYI, We are restricted from using PL/SQL. Environment is linux and I have access to the sqldeveloper file which contains JDBC and all the other jar files and can add any new files.
Thanks in advance.
回答1:
With any up-to-date driver, you can just select CLOB columns and call ResultSet.getString() to obtain the value. No need for any special treatment like dbms_lob().
The 11.x drivers are definitely capable of doing that, not sure about the outdated 10.x drivers though.
If the driver is too old and upgrading is not an option, you still don't need any special treatment in your SQL statement, just process the CLOB from the ResultSet using getCharacterStream() - that even works with 9.x drivers.
Something like:
ResultSet rs = statement.executeQuery("Select clob_column from table_name name ...");
Reader in = rs.getCharacterStream(1);
String clobValue = null;
if (!rs.wasNull())
{
// process whatever the Reader returns, e.g. using Apache Commons IO
clobValue = IOUtils.toString(in);
}
in.close();
With a current driver, you'd just do:
ResultSet rs = statement.executeQuery("Select clob_column from table_name name ...");
String clobValue = rs.getString(1);
Btw: Oracle 10 is pretty outdated, you should really think about upgrading.
来源:https://stackoverflow.com/questions/17239328/java-class-for-retrieving-large-string-from-oracle-db-of-lob-datatype