使用 JDBC 从长字符串创建 CLOB
我有以下查询:
select id from table1 where some_func(?) = 1;
其中 some_func
是一个函数,允许其参数为 VARCHAR2 或 CLOB,而 ?
是某个字符串,可能会很长。
我正在尝试使用以下代码来绑定变量:
stmt.setObject(i+1, obj);
但如果 string.length()
> 4000 我收到以下错误:
java.sql.SQLException: ORA-01460: unimplemented or unreasonable conversion requested
原因很明显: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);
}
这给出了不同的错误:
ORA-22922: nonexistent LOB value
我尝试的最后一个想法是使用 oracle.sql.CLOB.createTemporary()
方法创建 CLOB 但由于以下原因失败了例外:
java.lang.ClassCastException:
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
cannot be cast to oracle.jdbc.OracleConnection
我做错了什么?还有其他可能性吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CLOB 可以通过简单的方式创建:
然后当然应该释放这些 CLOB。
The CLOB could be created in a simple manner:
Then these clobs should be freed of course.
根据我的经验,setCharacterStream() 比 setClob() 可靠得多,
并且不需要创建 CLOB 对象即可工作
From my experience setCharacterStream() is much more reliable than setClob()
and it works without the need to create CLOB objects