Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create CLOB from long string using JDBC

I have the following query:

select id from table1 where some_func(?) = 1;

where some_func is a function which allows its arguments to be either VARCHAR2 or CLOB, and ? is some string, which could be really long.

I am trying to use the following code to bind variables:

stmt.setObject(i+1, obj);

but in case of string.length() > 4000 I get the following error:

java.sql.SQLException: ORA-01460: unimplemented or unreasonable conversion requested

for an obvious reason: the VARCHAR2 size limit is 4000 characters.

I then tried to use the following code:

if(obj instanceof String && ((String) obj).length() >= 4000) {
  String s = (String) obj;
  StringReader stringReader = new StringReader(s);
  stmt.setClob(i+1, stringReader, s.length());
} else {
  stmt.setObject(i+1, obj);
}

which gave a different error:

ORA-22922: nonexistent LOB value

The last idea I tried was to create a CLOB using oracle.sql.CLOB.createTemporary() method but it failed because of the following exception:

java.lang.ClassCastException:
  org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper 
  cannot be cast to oracle.jdbc.OracleConnection

What am I doing wrong? Are there any other possibilities to do this?

like image 748
taurus Avatar asked Feb 21 '11 15:02

taurus


People also ask

How to get CLOB data in java?

To read from a CLOB, use the getAsciiStream() or getCharacterStream() method of an oracle. sql. CLOB object to retrieve the entire CLOB as an input stream. The getAsciiStream() method returns an ASCII input stream in a java.

What is CLOB in JDBC?

An SQL CLOB is a built-in type that stores a Character Large Object as a column value in a row of a database table. By default drivers implement a Clob object using an SQL locator(CLOB) , which means that a Clob object contains a logical pointer to the SQL CLOB data rather than the data itself.

What is CLOB in datatype?

The CLOB data type stores any kind of text data in random-access chunks, called sbspaces. Text data can include text-formatting information, if this information is also textual, such as PostScript, Hypertext Markup Language (HTML), Standard Graphic Markup Language (SGML), or Extensible Markup Language (XML) data.


2 Answers

The CLOB could be created in a simple manner:

if(obj instanceof String && ((String) obj).length() >= 4000) {
    Clob clob = connection.createClob();
    clob.setString(1, (String) obj);
    stmt.setClob(i+1, clob);
}

Then these clobs should be freed of course.

like image 91
taurus Avatar answered Oct 01 '22 10:10

taurus


From my experience setCharacterStream() is much more reliable than setClob()

String s = (String) obj;
StringReader stringReader = new StringReader(s);
stmt.setCharacterStream(i + 1, stringReader , s.length());

and it works without the need to create CLOB objects

like image 33
a_horse_with_no_name Avatar answered Oct 02 '22 10:10

a_horse_with_no_name