OutputStream 到 DB2 数据库表的 BLOB 列 [英] OutputStream to the BLOB column of a DB2 database table

查看:103
本文介绍了OutputStream 到 DB2 数据库表的 BLOB 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 DB2 数据库中,我有下表:

In a DB2 database, I have the following table:

CREATE TABLE MyTestTable
( 
    MYPATH VARCHAR(512) NOT NULL, 
    MYDATA BLOB, 
    CONSTRAINT MYTESTTABLE_PK PRIMARY KEY (MYPATH)
);

使用 Java,我希望用新的 blob 数据更新此表中的现有行.我的首选方法是获取 BLOB 列的 OutputStream &将我的数据写入 OutputStream.

Using Java, I wish to update an existing row in this table with new blob data. My preferred way is to obtain an OutputStream to the BLOB column & write my data to the OutputStream.

这是我正在使用的测试代码:

Here is the test code I am using:

Connection connection = null;
PreparedStatement pStmnt = null;
ResultSet rSet = null;

try {
    connection =  ... // get db connection
    String id = ... // set the MYPATH value 

    String sql = "SELECT MYDATA FROM MyTestTable WHERE MYPATH='"+id+"' FOR UPDATE";

    pStmnt = connection.prepareStatement(sql);
    rSet = pStmnt.executeQuery();
    while (rSet.next()) {
        Blob blobData = rSet.getBlob("MYDATA");  // this is a java.sql.Blob

        OutputStream blobOutputStream = blobData.setBinaryStream(1);
        blobOutputStream.write(data);
        blobOutputStream.close();
        connection.commit();
    }
}
// close ResultSet/PreparedStatement/etc in the finally block

以上代码适用于 Oracle DB.

The above code works for the Oracle DB.

但是,在 DB2 中,调用 setBinaryStream 来获取 OutputStream 似乎不起作用.数据没有更新,我也没有收到任何错误消息.

However, in DB2, calling setBinaryStream to get the OutputStream does not seem to work. The data does not get updated, and I do not get any error messages.

问:我怎样才能得到一个输出流到 DB2 表的 BLOB 列?上述代码中可能需要更改什么?

Qs: How can I get an OutputStream to the BLOB column of a DB2 table? What might need to be changed in the above code?

推荐答案

您可能已成功将数据写入 Blob 对象,但您需要对 PreparedStatement 和 ResultSet 执行更多操作才能真正更新数据库.

You are probably getting the data written to the Blob object successfully, but you need to do more with the PreparedStatement and ResultSet in order to actually update the value in the database.

首先,您的 PreparedStatement 必须使用 Connection.prepareStatement() 的版本,它采用 resultSetConcurrency 参数,您必须将其设置为值 ResultSet.CONCUR_UPDATABLE.(我不知道 SQL SELECT 实际上需要指定 FOR UPDATE 子句 - 请参阅本答案末尾链接中的教程.)

First, your PreparedStatement must be instantiated using a version of Connection.prepareStatement() that takes a resultSetConcurrency parameter, which you must set to the value ResultSet.CONCUR_UPDATABLE. (I don't know that the SQL SELECT actually needs to specify the FOR UPDATE clause - see the tutorial at the link at the end of this answer.)

其次,关闭 blobOutputStream 后,需要使用 updateBlob(int columnIndex, Blob x)updateBlob(StringcolumnLabel, Blob x),然后在执行 Connection.commit() 之前调用 ResultSet.updateRow().

Second, after you close blobOutputStream, you need to update the value in the ResultSet using updateBlob(int columnIndex, Blob x) or updateBlob(String columnLabel, Blob x), then invoke ResultSet.updateRow() before doing a Connection.commit().

我自己没有以这种方式更新 Blob 值,但它应该可以工作.如果您在尝试重用最初从 ResultSet 读取的 Blob 时遇到任何问题(如果您实际上并未使用原始数据,则可能不需要这样做),您可以使用 Connect.createBlob()做一个空的开始.您可以从本教程了解有关更新结果集的更多信息.

I haven't updated Blob values this way myself, but it should work. If you run into any issues trying to reuse the Blob originally read from the ResultSet (which you probably don't need to do if you're not actually using the original data), you can use Connect.createBlob() to make an empty one to start with. You can learn more about updating ResultSets from this tutorial.

这篇关于OutputStream 到 DB2 数据库表的 BLOB 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆