使用 JDBC 从长字符串创建 CLOB

发布于 2024-10-19 00:26:55 字数 1126 浏览 4 评论 0原文

我有以下查询:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

策马西风 2024-10-26 00:26:56

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

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

然后当然应该释放这些 CLOB。

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.

撞了怀 2024-10-26 00:26:56

根据我的经验,setCharacterStream() 比 setClob() 可靠得多,

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

并且不需要创建 CLOB 对象即可工作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文