- /////////////////////
- // 圖紙
- /////////////////////
- create table EMHOOKUP
- (
- PICID NUMBER(8) not null,
- PICNAME VARCHAR2(100) not null,
- PICTUREDATA BLOB,
- PICTYPE NUMBER(8),
- PICMODIFYID NUMBER(8)
- )
- alter table EMHOOKUP add primary key (PICID)
/////////////////////
// 圖紙
/////////////////////
create table EMHOOKUP
(
PICID NUMBER(8) not null,
PICNAME VARCHAR2(100) not null,
PICTUREDATA BLOB,
PICTYPE NUMBER(8),
PICMODIFYID NUMBER(8)
)
alter table EMHOOKUP add primary key (PICID)
下面讓我們來研究一下,如果在Oracle數據庫中進行實際的存儲
- DBTemplate db = new DBTemplate() {
- public void dbProcess(Connection con) throws Exception {
- StringBuffer sql = new StringBuffer();
- sql.append(" insert into emhookup ");
- sql.append(" (picid, picname, picmodifyid, picturedata, pictype)values ");
- sql.append(" (?, ?, picmodifyidseq.nextval, EMPTY_BLOB(), ?)");
- PreparedStatement ps = con.prepareStatement(sql.toString());
- ps.setInt(1, picID);
- ps.setString(2, picName);
- ps.setInt(3, data.picType);
- ps.executeUpdate();
- ps.close();
- sql.setLength(0);
- sql.append(" select picturedata from emhookup ");
- sql.append(" where picid=? for update ");
- ps = con.prepareStatement(sql.toString());
- ps.setInt(1, picID);
- ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("picturedata");
- ObjectOutputStream out = new ObjectOutputStream(blob.getBinaryOutputStream());
- out.writeObject(DataProcess.toByteByGZIP(data));
- out.flush();
- out.close();
- }
- rs.close();
- ps.close();
- }
- };
- db.runWithTransaction();
DBTemplate db = new DBTemplate() {
public void dbProcess(Connection con) throws Exception {
StringBuffer sql = new StringBuffer();
sql.append(" insert into emhookup ");
sql.append(" (picid, picname, picmodifyid, picturedata, pictype)values ");
sql.append(" (?, ?, picmodifyidseq.nextval, EMPTY_BLOB(), ?)");
PreparedStatement ps = con.prepareStatement(sql.toString());
ps.setInt(1, picID);
ps.setString(2, picName);
ps.setInt(3, data.picType);
ps.executeUpdate();
ps.close();
sql.setLength(0);
sql.append(" select picturedata from emhookup ");
sql.append(" where picid=? for update ");
ps = con.prepareStatement(sql.toString());
ps.setInt(1, picID);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("picturedata");
ObjectOutputStream out = new ObjectOutputStream(blob.getBinaryOutputStream());
out.writeObject(DataProcess.toByteByGZIP(data));
out.flush();
out.close();
}
rs.close();
ps.close();
}
};
db.runWithTransaction();
從以上代碼可知oracle中的blob/clob都需要先插入空數據empty_blob()/empty_clob()然後再鎖定該行 for update 進行更新,並且必須在事務中運行runWithTransaction 。