public void insertBin(String empID, byte[] image, String Crd) throws
      Exception {
    PreparedStatement pstmt = null;    
    ResultSet rs = null;
    Connection conn = Conn();
    BLOB blob = null;    
    try {
      pstmt = conn.prepareStatement(InserteEmptyBlob_SQL); 
       pstmt.setString(1, empID);
      pstmt.setString(2, Crd);
      conn.setAutoCommit(false);         if (pstmt.executeUpdate() == 0) {
        throw new NotExistException("InserteEmptyBlob_SQL: " +
                                    "Update No data exists");
      }
      pstmt.close();      pstmt = conn.prepareStatement(ListBin_SQL);       pstmt.setString(1, empID);
      System.out.println(empID);      
      rs = pstmt.executeQuery();
      if (rs.next()) {
        blob = (BLOB)rs.getBlob(1);
      }      pstmt.close();//这边关闭了,会不会有影响呢,为什么?
      BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());//这边打的数据流是怎么写进数据中的那个字段的呢    
      int size = 8192;
      byte[] buffer = new byte[size];
      int length = -1;
      ByteArrayInputStream in = new ByteArrayInputStream(image);      
      while ( (length = in.read(buffer)) != -1) {
        out.write(buffer,0,length);
      }      
      in.close();
      out.close();
      conn.commit();
    }
    catch (SQLException e) {
      System.out.print("insertBin Error:" + e);
    }
    finally {
      if (pstmt != null) {
        pstmt.close();
      }
      if (conn != null) {
        conn.close();
      }
      if (rs != null) {
        rs.close();
      }
    }  }
}
我想理解,上面我标注出的代码旁边的我提出的问题?,谢谢各位!重分感谢

解决方案 »

  1.   

    PreparedStatement ps = null;
    ResultSet rs = null;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        String sql = "insert into table(col_blob) values(empty_blob())";  //先插入一个空的blob
        ps = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ps.executeUpdate();
        sql = "select col_bolb from table for update";    //再打开bolb流
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        if(rs!=null && rs.next()) {
            try {
                  java.sql.Blob blob = rs.getBlob("col_bolb");
                  bis = new BufferedInputStream(new FileInputStream(new File(写入bolb中的文件)));
                  bos = new BufferedOutputStream(((oracle.sql.BLOB)blob).getBinaryOutputStream());
                  int n;
                  while((n = bis.read()) != -1) {
                     bos.write(n);
                  }
            }finally {
                  if(bis!=null)bis.close();
                  if(bos!=null)bos.close();
            }
       }
    }finally {
    if(ps != null) {
    ps.close();
    }
    }
      

  2.   

    blob是用来存储图片的,其实long类型也很好用,存大文本,也可存小图片..