<small id='59Dpj'></small><noframes id='59Dpj'>

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

        OutputStream 到 DB2 数据库表的 BLOB 列

        时间:2023-05-27
          <tbody id='lUepp'></tbody>
          1. <i id='lUepp'><tr id='lUepp'><dt id='lUepp'><q id='lUepp'><span id='lUepp'><b id='lUepp'><form id='lUepp'><ins id='lUepp'></ins><ul id='lUepp'></ul><sub id='lUepp'></sub></form><legend id='lUepp'></legend><bdo id='lUepp'><pre id='lUepp'><center id='lUepp'></center></pre></bdo></b><th id='lUepp'></th></span></q></dt></tr></i><div id='lUepp'><tfoot id='lUepp'></tfoot><dl id='lUepp'><fieldset id='lUepp'></fieldset></dl></div>

            1. <legend id='lUepp'><style id='lUepp'><dir id='lUepp'><q id='lUepp'></q></dir></style></legend>

                <bdo id='lUepp'></bdo><ul id='lUepp'></ul>
              • <tfoot id='lUepp'></tfoot>
              • <small id='lUepp'></small><noframes id='lUepp'>

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

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

                  相关文章

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

                      <legend id='qgLXW'><style id='qgLXW'><dir id='qgLXW'><q id='qgLXW'></q></dir></style></legend>
                      • <bdo id='qgLXW'></bdo><ul id='qgLXW'></ul>
                    1. <small id='qgLXW'></small><noframes id='qgLXW'>