参考文章:
http://blog.csdn.net/gdweijin/article/details/5958277
import java.lang.reflect.Proxy;
import org.hibernate.engine.jdbc.SerializableBlobProxy;
import java.sql.Blob;
Blob stepBlobSmall;
Blob stepBlobBig;
File imageSmall;
File imageBig;
public void updatePic(Integer id,File imageSmall,File imageBig,Blob stepBlobSmall,Blob stepBlobBig)
{
//imageSmall||stepBlobSmall imageBig||stepBlobBig
PreparedStatement pstmt=null;
try {
String sql ="UPDATE STANDARD_BOOK SB SET SB.IMAGE_SMALL =?,SB.IMAGE_BIG = ? WHERE SB.ID="+id;
Connection conn = this.getSession().connection();
pstmt = conn.prepareStatement(sql);
System.out.println(stepBlobSmall);
if (imageSmall!=null) {
FileInputStream fis;
fis = new FileInputStream(imageSmall);
pstmt.setBinaryStream(1, fis, fis.available());
}else if(stepBlobSmall!=null){
SerializableBlobProxy proxy = (SerializableBlobProxy )Proxy.getInvocationHandler(stepBlobSmall);
java.sql.Blob stepBlobSmallBLOB = proxy.getWrappedBlob();
pstmt.setBlob(1, stepBlobSmallBLOB);
}else {
pstmt.setBinaryStream(1,null,0);
}
if (imageBig!=null) {
FileInputStream fis;
fis = new FileInputStream(imageBig);
pstmt.setBinaryStream(2, fis, fis.available());
}else if(stepBlobBig!=null){
SerializableBlobProxy proxy = (SerializableBlobProxy )Proxy.getInvocationHandler(stepBlobBig);
java.sql.Blob stepBlobBigBLOB = proxy.getWrappedBlob();
pstmt.setBlob(2, stepBlobBigBLOB);
}else{
pstmt.setBinaryStream(2,null,0);
}
pstmt.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try
{
pstmt.close();
pstmt = null;
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}