简体   繁体   English

将数据附加到DB2 Blob

[英]Append data to a DB2 blob

In my DB2 database, I have a table with a Blob: 在我的DB2数据库中,我有一个带有Blob的表:

CREATE TABLE FILE_STORAGE (
    FILE_STORAGE_ID integer,
    DATA blob(2147483647),
    CONSTRAINT PK_FILE_STORAGE PRIMARY KEY (FILE_STORAGE_ID));

Using the db2jcc JDBC driver (db2jcc4-9.7.jar), I can read and write data in this table without any problems. 使用db2jcc JDBC驱动程序(db2jcc4-9.7.jar),我可以毫无问题地读取和写入该表中的数据。

Now I need to be able to append data to existing rows, but DB2 gives the cryptic error 现在,我需要能够将数据追加到现有的行中,但是DB2给出了神秘的错误

Invalid operation: setBinaryStream is not allowed on a locator or a reference. ERRORCODE=-4474, SQLSTATE=null

I use the following code to append my data: 我使用以下代码追加数据:

String selectQuery = "SELECT DATA FROM FILE_STORAGE WHERE FILE_STORAGE_ID = ?";
try (PreparedStatement ps = conn.prepareStatement(selectQuery, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) {
    ps.setInt(1, fileStorageID);
    try (ResultSet rs = ps.executeQuery()) {
        if (rs.next()) {
            Blob existing = rs.getBlob(1);
            try {
                // The following line throws the exception:
                try (OutputStream output = existing.setBinaryStream(existing.length() + 1)) {
                    // append the new data to the output:
                    writeData(output);
                } catch (IOException e) {
                    throw new IllegalStateException("Error writing output stream to blob", e);
                }

                rs.updateBlob(1, existing);
                rs.updateRow();
            } finally {
                existing.free();
            }
        } else {
            throw new IllegalStateException("No row found for file storage ID: " + fileStorageID);
        }
    }
}

My code is using the methods as suggested in OutputStream to the BLOB column of a DB2 database table . 我的代码使用的是OutputStream中建议的方法到DB2数据库表的BLOB列 There also seem to be other people who have the same problem: Update lob columns using lob locator . 似乎还有其他人也有相同的问题: 使用lob locator更新lob列

As a workaround , I currently read all the existing data into memory, append the new data in memory, and then write the complete data back into the blob. 解决方法是 ,我目前将所有现有数据读入内存,将新数据追加到内存中,然后将完整数据写回到Blob。 This works, but it's very slow and obviously it will take longer if there's more data in the blob, getting slower with each update. 这可以工作,但是非常慢,而且如果blob中有更多数据,显然会花费更长的时间,每次更新都会变得更慢。

I do need to use Java to update the data, but apart from switching away from the JVM, I am happy to try any possible alternatives at all, I just need to append the data somehow. 我确实需要使用Java来更新数据,但是除了切换到JVM外,我很乐于尝试所有可能的替代方法,我只需要以某种方式附加数据即可。

Thanks in advance for any ideas! 预先感谢您的任何想法!

If you only need to append data to the end of a BLOB column and don't want to read the entire value into your program, a simple UPDATE statement will be faster and more straightforward. 如果只需要将数据追加到BLOB列的末尾,并且不想将整个值读入程序,则简单的UPDATE语句将更快,更直接。

Your Java program could run something like this via executeUpdate() : 您的Java程序可以通过executeUpdate()运行类似的内容:

UPDATE file_storage SET data = data || BLOB(?) WHERE file_storage_id = ?

The parameter markers for this would be populated by setBlob(1, dataToAppend) and setInt(2, fileStorageID) . 为此的参数标记将由setBlob(1, dataToAppend)setInt(2, fileStorageID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM