Java 对BLOB 操作

项目开发需要对Oracle 的BLOB对象进行操作,大概就是帮文件写入BLOB,页面上可以按日期罗列所有的文件,对文件进行操作。

 

BLOB对象操作

 

package sg.gov.wda.l3e.disbursement.batch.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import oracle.sql.BLOB;

import sg.gov.wda.l3e.disbursement.batch.model.NfsFile;
import sg.gov.wda.l3e.disbursement.util.DisbursementUtil;
import ecquaria.reuse.boa.dao.BaseDAO;

// BaseDAO 是框架对DAO操作的基类,
// connection封装在Session中
   
public class NfsFileDAO extends BaseDAO {

	private static final String TABLE_NAME = "DINFSFILES";

	private static final String FILE_ID = "FILE_ID";
	private static final String FILE_NAME = "FILE_NAME";
	private static final String FILE_OBJECT = "FILE_OBJECT";

	private static final String DATE_CREATION = "DATE_CREATION";
	private static final String CREATOR_ID = "CREATOR_ID";
	private static final String DATE_LAST_UPDATE = "DATE_LAST_UPDATE";
	private static final String LAST_UPDATE_ID = "LAST_UPDATE_ID";

	private static final String PRE_INSERT_QUERY = "insert into " + TABLE_NAME
			+ " values(?,?,empty_blob(),sysdate,?,sysdate,?)";
	
	private static final String RETRIEVE_BLOB_BY_FILE_ID = " select "
			+ FILE_OBJECT + " from " + TABLE_NAME + " where " + FILE_ID + " = ? ";
	
	private static final String UPDATE_NFS_FILE = " select " + FILE_OBJECT
			+ " from " + TABLE_NAME + " where " + FILE_ID + " = ? for update";
	
	private static final String RETRIEVE_ERROR_FILE_LIST = " select " + FILE_ID
			+ "," + FILE_NAME + "," + DATE_CREATION + " from "+TABLE_NAME+" where "
			+ DATE_CREATION+" between to_date(?,'yyyy/MM/dd') and to_date(?,'yyyy/MM/dd')";
	
	private static final String TEMP_USER = "temp_user" ;


      // table 存file_type column还没有加

	public long preInsertNfsFile(String file_name,String file_type) throws Exception {
		
		//=== DINFSFILES did nod have the sequence yet , user the DIMANUALCLAIMENTITYSEQ temporary
		long file_id = DisbursementUtil.getSeqId("DIMANUALCLAIMENTITYSEQ");
		
		PreparedStatement ps = null;
		try{
			ps = prepareStatement(PRE_INSERT_QUERY);
			ps.setLong(1, file_id);
			ps.setString(2, file_name);
			ps.setString(3, TEMP_USER);
			ps.setString(4, TEMP_USER);
			
			ps.execute() ;
			
		} catch(Exception e) {
			e.printStackTrace();
			throw(e);
		} finally {
			if(ps!=null) ps.close();
		}
		
		return file_id ;
	}
	
	public void UpdateNfsFile(long file_id, String filePath) throws Exception {

		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = prepareStatement(UPDATE_NFS_FILE);
			ps.setLong(1, file_id);
			rs = ps.executeQuery();

			BLOB blob = null;
			if (rs.next()) {
				blob = (BLOB) rs.getBlob(1);
			}

			OutputStream out = blob.getBinaryOutputStream();
			FileInputStream fin = new FileInputStream(new File(filePath));
			byte[] b = new byte[((BLOB) blob).getBufferSize()];
			int len = 0;
			while ((len = fin.read(b)) != -1)
				out.write(b, 0, len);
			fin.close();
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
			throw (e);
		} finally {
			if (rs != null)
				rs.close();
			if (ps != null)
				ps.close();
		}
	}
	
	public List retrieveErrorFileList(String start , String end ) throws Exception {
		
		PreparedStatement ps = null;
		ResultSet rs = null;
		List result = new ArrayList() ;
		NfsFile nf ;
		
		try{
			ps = prepareStatement(RETRIEVE_ERROR_FILE_LIST);
			ps.setString(1, start);
			ps.setString(2, end);
			rs = ps.executeQuery();
			
			while(rs.next()) {
				nf = new NfsFile();
				nf.setFile_id(rs.getLong(1));
				nf.setFile_name(rs.getString(2));
				nf.setDate_creation(rs.getDate(3));
				
				result.add(nf);
			}
			
		}catch(Exception e) {
			e.printStackTrace();
			result.clear();
			throw(e) ;
		}finally {
			if(rs!=null) rs.close();
			if(ps!=null) ps.close();
		}
		
		return result ;
	}
	
	public Blob retrieveBLOBObjectByFileId(long file_id) throws Exception{
		
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try{
			ps = prepareStatement(RETRIEVE_BLOB_BY_FILE_ID);
			ps.setLong(1, file_id);
			rs = ps.executeQuery();
			Blob blob = null;
			if(rs.next()) {
				blob = rs.getBlob("FILE_OBJECT");
			}
			
			return blob ;
			
		}catch(Exception e) {
			e.printStackTrace();
			throw(e) ;
		}finally {
			if(rs!=null) rs.close();
			if(ps!=null) ps.close();
		}
		
	}
}


     // 业务层调用

      public void InsertNfsFile (String fileName,String path,String type) throws Exception {
		
		long file_id = fileDao.preInsertNfsFile(fileName,type);
		fileDao.UpdateNfsFile(file_id, path);
	} 
 

你可能感兴趣的:(java,DAO,oracle,sql,框架)