Chinaunix首页 | 论坛 | 博客
  • 博客访问: 977568
  • 博文数量: 152
  • 博客积分: 4937
  • 博客等级: 上校
  • 技术积分: 1662
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-05 16:55
文章分类

全部博文(152)

文章存档

2013年(12)

2012年(6)

2011年(58)

2010年(43)

2009年(1)

2008年(15)

2007年(17)

我的朋友

分类: WINDOWS

2011-07-01 14:35:56

public static void addAttach(String source, String id, String filename, String memo)
  {
    if ((filename.equals("")) || (filename == null)) {
      return;
    }
    BufferedOutputStream out = null;
    InputStream in = null;
    int c = 0;
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    String sql = null;
    BLOB blob2 = null;
    OracleResultSet rs2 = null;
    try
    {
      sqlMap = TheAppSqlConfig.getSqlMapInstance();
      conn = sqlMap.getDataSource().getConnection();
      stmt = conn.createStatement();
      conn.setAutoCommit(false);
      boolean defaultCommit = conn.getAutoCommit();
      String attachid = Sequence.getNextValue("get-nextattachid");
      sql = "insert into all_attach(attachid,id,source,name,attach,memo) values(" + attachid + "," + id + ",'" + source + "','" + filename + "',empty_blob(),'" + memo + "')";
      stmt.executeUpdate(sql);
      sql = "select attach from all_attach where attachid = " + attachid + " for update";
      rs = stmt.executeQuery(sql);
      String pfile = FILE_PATH + filename;
      while (rs.next())
      {
        blob2 = (BLOB)rs.getBlob(1);
        out = new BufferedOutputStream(blob2.getBinaryOutputStream());
        in = new FileInputStream(pfile);
        while ((c = in.read()) != -1) {
          out.write(c);
        }
        in.close();
        out.close();
      }
      conn.commit();
      conn.setAutoCommit(defaultCommit);
      rs.close();
      rs = null;
      stmt.close();
      stmt = null;
      conn.close();
      conn = null;
      delFile(filename);
    }
    catch (Exception ex)
    {
      try
      {
        conn.rollback();
        conn.close();
      }
      catch (Exception e)
      {
        log.info("conn.rollback: " + e.toString());
      }
    }
    finally {
      Conc.closeConnection(conn, stmt, rs, true);
    }
  }
阅读(538) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~