使用JDBC从长字符串创建CLOB

tau*_*rus 6 java oracle jdbc clob

我有以下查询:

select id from table1 where some_func(?) = 1;
Run Code Online (Sandbox Code Playgroud)

where some_func是一个允许其参数为VARCHAR2或CLOB的函数,并且?是一些字符串,可能非常长.

我试图使用以下代码绑定变量:

stmt.setObject(i+1, obj);
Run Code Online (Sandbox Code Playgroud)

但是如果string.length()> 4000,我会收到以下错误:

java.sql.SQLException: ORA-01460: unimplemented or unreasonable conversion requested
Run Code Online (Sandbox Code Playgroud)

原因很明显:VARCHAR2大小限制为4000个字符.

然后我尝试使用以下代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

这给出了一个不同的错误:

ORA-22922: nonexistent LOB value
Run Code Online (Sandbox Code Playgroud)

我尝试的最后一个想法是使用oracle.sql.CLOB.createTemporary()方法创建一个CLOB 但由于以下异常而失败:

java.lang.ClassCastException:
  org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper 
  cannot be cast to oracle.jdbc.OracleConnection
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?还有其他可能吗?

tau*_*rus 12

可以通过简单的方式创建CLOB:

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

那么这些clob当然应该被释放.