How to write / update Oracle blob in a reliable way?

前端 未结 3 759
盖世英雄少女心
盖世英雄少女心 2020-11-28 09:09

I\'m trying to write and update a pdf document in a blob column but I\'m just able to update the blob only writing more data than the previous stored data. If I try to updat

3条回答
  •  广开言路
    2020-11-28 09:33

    It's a lot easier:

    PreparedStatement pstmt =
      conn.prepareStatement("update blob_table set blob = ? where id = ?");
    File blob = new File("/path/to/picture.png");
    FileInputStream in = new FileInputStream(blob);
    
    // the cast to int is necessary because with JDBC 4 there is 
    // also a version of this method with a (int, long) 
    // but that is not implemented by Oracle
    pstmt.setBinaryStream(1, in, (int)blob.length()); 
    
    pstmt.setInt(2, 42);  // set the PK value
    pstmt.executeUpdate();
    conn.commit();
    pstmt.close();
    

    It works the same when using an INSERT statement. No need for empty_blob() and a second update statement.

提交回复
热议问题