/**
*
* @param uploads
* 文件集合
* @param uploadFileNames
* 文件名稱集合
* @param apply_id_name
* 外鍵 id 字段名稱
* @param attach_id_fname
* 編號,流水號,本身的id
* @param attach_name_fname
* 附件名稱字段
* @param attach_fname
* 附件字段
* @param tname
* 表名
* @param apply_id
* 外鍵 id 字段名稱 對應的值
* @return
* @throws Exception
*/ sql
public boolean insermultipleFile(List<File> uploads, 數據庫
List<String> uploadFileNames, String apply_id_name, String attach_id_fname, String attach_name_fname, String attach_fname, String tname, String apply_id) throws Exception { // String insert_sql = // "insert into t_base_attach(attach_id,apply_id,attach_name,attach,creater,creatime,material_num) values " // + " (?,?,?,empty_blob(),?,?,?)"; // String update_sql = // "select attach from t_base_attach where attach_id=? for update"; StringBuffer insert_sql = new StringBuffer(); insert_sql.append("insert into "); insert_sql.append(tname); insert_sql.append("("); insert_sql.append(attach_id_fname).append(","); insert_sql.append(apply_id_name).append(","); insert_sql.append(attach_name_fname).append(","); insert_sql.append(attach_fname); insert_sql.append(") values "); insert_sql.append(" (?,?,?,empty_blob()) "); String update_sql = "select " + attach_fname + " from " + tname + " where " + attach_id_fname + "=? for update"; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; String attach_id = ""; try { log.debug(insert_sql.toString()); log.debug(update_sql.toString()); conn = jt.getDataSource().getConnection(); // conn.setAutoCommit(true); for (int i = 0; i < uploads.size(); i++) { attach_id = queryGuidFromOracle(); ps = conn.prepareStatement(insert_sql.toString()); ps.setString(1, attach_id); ps.setString(2, apply_id); ps.setString(3, uploadFileNames.get(i)); ps.executeUpdate(); ps = conn.prepareStatement(update_sql); ps.setString(1, attach_id); rs = ps.executeQuery(); if (rs.next()) { oracle.sql.BLOB blob = (oracle.sql.BLOB) rs .getBlob(attach_fname); // String strfilename = rs.getString("VC2FILENAME"); // 到數據庫的輸出流 OutputStream outStream = blob.getBinaryOutputStream(); // 這裏用一個文件模擬輸入流 File file = uploads.get(i); InputStream fin = null; try { fin = new FileInputStream(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 將輸入流寫到輸出流 byte[] b = new byte[blob.getBufferSize()]; int len = 0; while ((len = fin.read(b)) != -1) { outStream.write(b, 0, len); // blob.putBytes(1,b); } // 依次關閉(注意順序) fin.close(); outStream.flush(); outStream.close(); } } conn.commit(); conn.setAutoCommit(true); close(conn, ps, rs); } catch (SQLException e) { try { conn.rollback(); e.printStackTrace(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return false; } return true; }