在oracle中存储BLOB类型的数据,不能和普通的String类型一样存储,而是要通过一个唯一标识来帮助储存更新,具体实现代码如下:
public boolean putSignPic(String uuid, byte[] signPic) throws SQLException{
boolean b = false;
Statement stmt = null;
NewProxyResultSet update = null;
Connection conn = DataSourceUtils.getConnection();
if (conn != null) {
try {
stmt = conn.createStatement();
stmt.executeUpdate("UPDATE T_SIGNATURE SET SIGNPIC=EMPTY_BLOB() WHERE UUID='" + uuid + "'");
stmt.close();
conn.setAutoCommit(false);
stmt = conn.createStatement();
update = (NewProxyResultSet) stmt
.executeQuery("SELECT SIGNPIC FROM T_SIGNATURE WHERE UUID='" + uuid + "'FOR UPDATE");
if (update != null && update.next()) {
BLOB strSignData = null;
strSignData = (BLOB) update.getBlob("SIGNPIC");
byte[] temp_byte_xml = signPic;
if (temp_byte_xml != null) {
b = PutAtBlob(strSignData, signPic,
temp_byte_xml.length);
conn.commit();
conn.setAutoCommit(true);
}
}
} catch (Exception e) {
logger.error(e);
return false;
} finally {
try {
if (update != null) {
update.close();
}
} catch (SQLException e) {
logger.error("OracleResultSet关闭异常:" + e.toString());
}
try {
//2015-12-14
if(stmt != null){
stmt.close();
}
} catch (SQLException e) {
logger.error("Statement关闭异常:" + e.toString());
}
try {
//2015-12-14
if(conn != null){
conn.close();
}
} catch (SQLException e) {
logger.error("数据库连接关闭异常:" + e.toString());
}
}
}
System.out.println("在数据库BLOB类型的字段SignPic中存入图片成功!!!");
return b;
}
/**
* 从字节数组中存放规定大小的数据至ORCLE BLOB对象中
*
* @throws SQLException
* @throws IOException
*/
private boolean PutAtBlob(BLOB vField, byte[] b, int vSize)
throws SQLException, IOException {
boolean mResult = false;
OutputStream outstream = null;
try {
outstream = vField.getBinaryOutputStream();
outstream.write(b, 0, vSize);
// outstream.write(b);
outstream.close();
mResult = true;
} catch (IOException e) {
logger.error(e.toString());
} finally {
if (outstream != null) {
SignExcuteUtil.outputStreamClose(outstream);
}
}
return mResult;
}