BLOB
/**
* 写文件到BLOB中
* @param FileName
*/
public void testSaveFiletoBlob(String FileName){
try
{
File file = new File(FileName) ;
//java.io.FileReader fr=new java.io.FileReader(file);
java.io.FileInputStream fr=new java.io.FileInputStream(file);
oracle.jdbc.OracleConnection conn=(oracle.jdbc.OracleConnection)getConnection();
//0) 先删除
OraclePreparedStatement pst=(OraclePreparedStatement)conn.prepareStatement("delete from tKHDZDPRINT where" +
" theyear=? and theseason=? and khh=?");
pst.setInt(1, Integer.valueOf(2007));
pst.setInt(2, Integer.valueOf(1));
pst.setString(3, "1094103");
pst.executeUpdate();
conn.commit();
pst.close();
//1)插入数据
pst=(OraclePreparedStatement)conn.prepareStatement("insert into tKHDZDPRINT(theyear,theseason,khh,printfile) values(" +
"?,?,?,empty_blob())");
pst.setInt(1, Integer.valueOf(2007));
pst.setInt(2, Integer.valueOf(1));
pst.setString(3, "1094103");
pst.executeUpdate();
conn.commit();
pst.close();
//2)锁定和读取内容
pst=(OraclePreparedStatement)conn.prepareStatement("select printfile from tkhdzdprint where theyear=2007 and " +
" theseason=1 and khh='1094103' for update");
java.sql.ResultSet rs=pst.executeQuery();
oracle.sql.BLOB c=null;
if (rs.next()){
c=(oracle.sql.BLOB)rs.getBlob(1);
}
pst.close();
//3)更新内容
java.io.OutputStream writer;
byte[] cbuf=new byte[1024];
writer=c.setBinaryStream(1L);
Integer offset=Integer.valueOf(0);
Integer iRead;
iRead=fr.read(cbuf,offset,1024);
while (iRead.compareTo(-1)!=0){
//writer.write(cbuf);
writer.write(cbuf,0, iRead);
iRead=fr.read(cbuf,offset,1024);
}
writer.flush();
writer.close();
pst=(OraclePreparedStatement)conn.prepareStatement("update tKHDZDPRINT set printfile=? where theyear=2007 and " +
" theseason=1 and khh='1094103'");
pst.setBLOB(1, c);
pst.executeUpdate();
conn.commit();
pst.close();
}
catch (java.io.IOException e){
return;
}
catch (SQLException e){
return;
}
catch (java.lang.ClassNotFoundException e){
return;
}
}
-------读blob----------------------------------------------------------------------------------------------------------------------
/**
* 测试从数据读取blob,并写到一个指定的文件.
* @param FileName
*/
public void testReadFileFromBlob(String FileName){
try{
File file=new File(FileName);
java.io.FileOutputStream fw=new java.io.FileOutputStream(file);
oracle.jdbc.OracleConnection conn=(oracle.jdbc.OracleConnection)getConnection();
OraclePreparedStatement pst=(OraclePreparedStatement)conn.prepareStatement("select printfile from tKHDZDPRINT where " +
" theyear=? and theseason=? and khh=?");
pst.setInt(1, Integer.valueOf(2007));
pst.setInt(2, Integer.valueOf(1));
pst.setString(3, "1094103");
java.sql.ResultSet rs=pst.executeQuery();
if (rs.next()){
//读取内容
oracle.sql.BLOB c=(oracle.sql.BLOB)rs.getBlob(1);
byte[] cbuf=new byte[1024];
Integer iRead;
java.io.InputStream r= c.getBinaryStream(1L);
iRead=r.read(cbuf, 0, 1024);
while (iRead.compareTo(-1)!=0){
fw.write(cbuf,0,iRead);
iRead=r.read(cbuf, 0, 1024);
}
fw.flush();;
fw.close();
}
pst.close();
}
catch( java.io.IOException e){
}
catch( java.lang.ClassNotFoundException e){
}
catch (java.sql.SQLException e){
}
}