java读写oracle Blob数据
代码
public int setBlob(DBConnection dbConn, String table, String idField, String fieldName,
String id, String content, boolean bApeand) {
int ret = -1;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
content = this.preDisposeString(content);
//java.sql.*
Connection conn = dbConn.getConnection();
String sql = "select "+fieldName+" from "+table+" where "+idField+"="+id+" for update";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
Blob blob = null;
if (rs.next()) {
//获取blob对象,此处的blob是oracle.sql.Blob
blob = (Blob)rs.getBlob(fieldName);
//执行更新操作
long pos = 0; //Blob.length();
if(bApeand) {
pos = blob.length() + 8;
if(blob.length() > 0) {
content = " \n " + content;
}
}
OutputStream wr = ((BLOB)blob).getBinaryOutputStream();
InputStream param = (InputStream) (new ByteArrayInputStream(content.getBytes()));
for(int temp = param.read(); temp != -1; temp = param.read()) {
wr.write(temp);
}
wr.flush();
wr.close();
// //
ret = 1;
}
} catch(SQLException es) {
es.printStackTrace();
ret = -1;
} catch(Exception e) {
e.printStackTrace();
ret = -1;
} finally {
try {
if(null != rs) {
rs.close();
}
if(null != pstmt) {
pstmt.close();
}
} catch (SQLException e) {}
}
return ret;
}
String id, String content, boolean bApeand) {
int ret = -1;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
content = this.preDisposeString(content);
//java.sql.*
Connection conn = dbConn.getConnection();
String sql = "select "+fieldName+" from "+table+" where "+idField+"="+id+" for update";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
Blob blob = null;
if (rs.next()) {
//获取blob对象,此处的blob是oracle.sql.Blob
blob = (Blob)rs.getBlob(fieldName);
//执行更新操作
long pos = 0; //Blob.length();
if(bApeand) {
pos = blob.length() + 8;
if(blob.length() > 0) {
content = " \n " + content;
}
}
OutputStream wr = ((BLOB)blob).getBinaryOutputStream();
InputStream param = (InputStream) (new ByteArrayInputStream(content.getBytes()));
for(int temp = param.read(); temp != -1; temp = param.read()) {
wr.write(temp);
}
wr.flush();
wr.close();
// //
ret = 1;
}
} catch(SQLException es) {
es.printStackTrace();
ret = -1;
} catch(Exception e) {
e.printStackTrace();
ret = -1;
} finally {
try {
if(null != rs) {
rs.close();
}
if(null != pstmt) {
pstmt.close();
}
} catch (SQLException e) {}
}
return ret;
}
相关: