Ejemplo n.º 1
1
  /** 针对oracle Blob字段的插入操作 */
  @Test
  public void testBigBlobWrite() {
    PreparedDBUtil dbUtil = new PreparedDBUtil();
    TransactionManager tm = new TransactionManager();
    try {
      // 启动事务
      tm.begin();
      // 先插入一条记录,blob字段初始化为empty_lob
      dbUtil.preparedInsert("insert into test(id,blobname) values(?,?)");
      String id = DBUtil.getNextStringPrimaryKey("test");
      dbUtil.setString(1, id);
      dbUtil.setBlob(2, BLOB.empty_lob()); // 先设置空的blob字段

      dbUtil.executePrepared();

      // 查找刚才的插入的记录,修改blob字段的值为一个文件
      dbUtil = new PreparedDBUtil();
      dbUtil.preparedSelect("select blobname from test where id = ?");
      dbUtil.setString(1, id);
      dbUtil.executePrepared();

      BLOB blob = (BLOB) dbUtil.getBlob(0, "blobname");
      if (blob != null) {
        DBUtil.updateBLOB(blob, new java.io.File("d:/dominspector.rar"));
      }
      tm.commit();

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      try {
        tm.rollback();
      } catch (RollbackException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    } finally {
      tm = null;
      dbUtil = null;
    }
  }