Oracle 使用 JDBC 将单表常量与 CLOB 合并

发布于 2024-07-08 03:37:08 字数 1032 浏览 12 评论 0原文

作为这个问题的后续,我需要以下场景的帮助:

在Oracle中,给定一个简单的数据表:

create table data (
    id       VARCHAR2(255),
    key      VARCHAR2(255),
    value    CLOB);

我正在使用以下合并命令:

merge into data
using (
    select
        ? id,
        ? key,
        ? value
    from
        dual
) val on (
    data.id=val.id
    and data.key=val.key
)
when matched then 
    update set data.value = val.value 
when not matched then 
    insert (id, key, value) values (val.id, val.key, val.value);

我正在通过 JDBC 从 Java 应用程序调用查询。

当“value”字符串很大时,上述查询会导致以下 Oracle 错误:

ORA-01461: cannot bind a LONG value for insert into a long column

我什至按照文档设置了“SetBigStringTryClob”属性 此处 具有相同的结果。

鉴于“值”是 CLOB,是否可以实现我想要的行为?

编辑:客户端环境是Java

As a follow-up to this question, I need help with the following scenario:

In Oracle, given a simple data table:

create table data (
    id       VARCHAR2(255),
    key      VARCHAR2(255),
    value    CLOB);

I am using the following merge command:

merge into data
using (
    select
        ? id,
        ? key,
        ? value
    from
        dual
) val on (
    data.id=val.id
    and data.key=val.key
)
when matched then 
    update set data.value = val.value 
when not matched then 
    insert (id, key, value) values (val.id, val.key, val.value);

I am invoking the query via JDBC from a Java application.

When the "value" string is large, the above query results in the following Oracle error:

ORA-01461: cannot bind a LONG value for insert into a long column

I even set the "SetBigStringTryClob" property as documented here with the same result.

Is it possible to achieve the behavior I want given that "value" is a CLOB?

EDIT: Client environment is Java

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

-柠檬树下少年和吉他 2024-07-15 03:37:08

您没有在帖子中具体提到,但从问题的标签来看,我假设您是通过 Java 执行此操作的。

我在刚刚完成的项目中使用这样的代码取得了成功。 此应用程序使用 Unicode,因此如果您的问题域仅限于标准 ASCII 字符集,可能会有更简单的解决方案。

您当前是否在使用 OracleStatement.setCLOB() 方法? 这是一件非常尴尬的事情,但我们无法以其他方式解决它。 您必须实际创建一个临时 CLOB,然后在 setCLOB() 方法调用中使用该临时 CLOB。

现在,我已经将其从工作系统中删除,并且必须进行一些临时调整,因此,如果这在您的情况下似乎不起作用,请告诉我,我会回去看看是否可以得到一个更小的工作示例。

当然,这假设您使用的是 Oracle Corp. JDBC 驱动程序(ojdbc14.jar 或 ojdbc5.jar),这些驱动程序可在 $ORACLE_HOME/jdbc/lib 中找到

CLOB tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);


// Open the temporary CLOB in readwrite mode to enable writing
tempClob.open(CLOB.MODE_READWRITE);

// Get the output stream to write
Writer tempClobWriter = tempClob.getCharacterOutputStream();

// Write the data into the temporary CLOB
tempClobWriter.write(stringData);

// Flush and close the stream
tempClobWriter.flush();
tempClobWriter.close();

// Close the temporary CLOB
tempClob.close();

myStatement.setCLOB(column.order, tempClob);


德维恩·金

You haven't mentioned specifically in your post, but judging by the tags for the question, I'm assuming you're doing this from Java.

I've had success with code like this in a project I just finished. This application used Unicode, so there may be simpler solutions if your problem domain is limited to a standard ASCII character set.

Are you currently using the OracleStatement.setCLOB() method? It's a terribly awkward thing to have to do, but we couldn't get around it any other way. You have to actually create a temporary CLOB, and then use that temporary CLOB in the setCLOB() method call.

Now, I've ripped this from a working system, and had to make a few ad-hoc adjustments, so if this doesn't appear to work in your situation, let me know and I'll go back to see if I can get a smaller working example.

This of course assumes you're using the Oracle Corp. JDBC drivers (ojdbc14.jar or ojdbc5.jar) which are found in $ORACLE_HOME/jdbc/lib

CLOB tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);


// Open the temporary CLOB in readwrite mode to enable writing
tempClob.open(CLOB.MODE_READWRITE);

// Get the output stream to write
Writer tempClobWriter = tempClob.getCharacterOutputStream();

// Write the data into the temporary CLOB
tempClobWriter.write(stringData);

// Flush and close the stream
tempClobWriter.flush();
tempClobWriter.close();

// Close the temporary CLOB
tempClob.close();

myStatement.setCLOB(column.order, tempClob);

Regards,
Dwayne King

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