1. <small id='r9CFW'></small><noframes id='r9CFW'>

    <i id='r9CFW'><tr id='r9CFW'><dt id='r9CFW'><q id='r9CFW'><span id='r9CFW'><b id='r9CFW'><form id='r9CFW'><ins id='r9CFW'></ins><ul id='r9CFW'></ul><sub id='r9CFW'></sub></form><legend id='r9CFW'></legend><bdo id='r9CFW'><pre id='r9CFW'><center id='r9CFW'></center></pre></bdo></b><th id='r9CFW'></th></span></q></dt></tr></i><div id='r9CFW'><tfoot id='r9CFW'></tfoot><dl id='r9CFW'><fieldset id='r9CFW'></fieldset></dl></div>
    <tfoot id='r9CFW'></tfoot>

  2. <legend id='r9CFW'><style id='r9CFW'><dir id='r9CFW'><q id='r9CFW'></q></dir></style></legend>

    • <bdo id='r9CFW'></bdo><ul id='r9CFW'></ul>

      OutputStream 到 DB2 数据库表的 BLOB 列

      时间:2023-09-30

        <bdo id='T2HPA'></bdo><ul id='T2HPA'></ul>

        • <i id='T2HPA'><tr id='T2HPA'><dt id='T2HPA'><q id='T2HPA'><span id='T2HPA'><b id='T2HPA'><form id='T2HPA'><ins id='T2HPA'></ins><ul id='T2HPA'></ul><sub id='T2HPA'></sub></form><legend id='T2HPA'></legend><bdo id='T2HPA'><pre id='T2HPA'><center id='T2HPA'></center></pre></bdo></b><th id='T2HPA'></th></span></q></dt></tr></i><div id='T2HPA'><tfoot id='T2HPA'></tfoot><dl id='T2HPA'><fieldset id='T2HPA'></fieldset></dl></div>
          <tfoot id='T2HPA'></tfoot>
            <legend id='T2HPA'><style id='T2HPA'><dir id='T2HPA'><q id='T2HPA'></q></dir></style></legend>

                <tbody id='T2HPA'></tbody>

            1. <small id='T2HPA'></small><noframes id='T2HPA'>

              1. 本文介绍了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 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:更新 DB2 for iSeries 表时出错 SQL7008 下一篇:OpenJPA 查询第二次失败(可能是因为字节数组参数

                相关文章

                最新文章

                  <tfoot id='ft5LI'></tfoot>
                    • <bdo id='ft5LI'></bdo><ul id='ft5LI'></ul>
                  1. <i id='ft5LI'><tr id='ft5LI'><dt id='ft5LI'><q id='ft5LI'><span id='ft5LI'><b id='ft5LI'><form id='ft5LI'><ins id='ft5LI'></ins><ul id='ft5LI'></ul><sub id='ft5LI'></sub></form><legend id='ft5LI'></legend><bdo id='ft5LI'><pre id='ft5LI'><center id='ft5LI'></center></pre></bdo></b><th id='ft5LI'></th></span></q></dt></tr></i><div id='ft5LI'><tfoot id='ft5LI'></tfoot><dl id='ft5LI'><fieldset id='ft5LI'></fieldset></dl></div>

                    <small id='ft5LI'></small><noframes id='ft5LI'>

                  2. <legend id='ft5LI'><style id='ft5LI'><dir id='ft5LI'><q id='ft5LI'></q></dir></style></legend>