前幾天作項目,用到了Oracle數據庫,是關於更新Blob,代碼是這樣寫的:
html
-
-
-
-
-
- public static boolean setBlob(String tableName,int ID, String data,Connection conn) {
- Statement statement = null;
- ResultSet ret = null;
- String insertBlob = "select content from " + tableName + " where id = '"+ID+"' for update ";
- boolean flg = false;
- try {
- statement = conn.createStatement();
- ret = statement.executeQuery(insertBlob);
- oracle.sql.BLOB blob = null;
- if (ret.next()) {
- blob = ((OracleResultSet) ret).getBLOB(1);
- }
- OutputStream outstream = blob.getBinaryOutputStream();
- byte[] data1 = data.getBytes();
- outstream.write(data1, 0, data1.length);
- outstream.flush();
- outstream.close();
- flg = true;
- } catch (SQLException e1) {
- e1.printStackTrace();
- flg = false;
- } catch (IOException e1) {
- e1.printStackTrace();
- flg = false;
- } finally {
- try {
- if(statement != null) {
- statement.close();
- }
- if(ret != null) {
- ret.close();
- }
- }catch(Exception ex) {
- ex.printStackTrace();
- }
- }
-
- return flg;
- }
/**
* 描述: 添加某張表某條記錄的content字段,此字段爲 blob 型
* param: 表名;主鍵;數據;數據庫鏈接
* return: 添加成功返回 true ;不然返回 false
* */
public static boolean setBlob(String tableName,int ID, String data,Connection conn) {
Statement statement = null;
ResultSet ret = null;
String insertBlob = "select content from " + tableName + " where id = '"+ID+"' for update ";
boolean flg = false;
try {
statement = conn.createStatement();
ret = statement.executeQuery(insertBlob);
oracle.sql.BLOB blob = null;
if (ret.next()) {
blob = ((OracleResultSet) ret).getBLOB(1);
}
OutputStream outstream = blob.getBinaryOutputStream();
byte[] data1 = data.getBytes();
outstream.write(data1, 0, data1.length);//就是這個地方出了問題,若是是修改,以前就有了長度爲100字節的數據,而此次修改只有50字節數據,那麼後面50個字節就不會被修改,仍然存在數據庫中
outstream.flush();
outstream.close();
flg = true;
} catch (SQLException e1) {
e1.printStackTrace();
flg = false;
} catch (IOException e1) {
e1.printStackTrace();
flg = false;
} finally {
try {
if(statement != null) {
statement.close();
}
if(ret != null) {
ret.close();
}
}catch(Exception ex) {
ex.printStackTrace();
}
}
return flg;
}
若是你更新blob字段時,該字段爲空(好比以前剛剛插入新記錄),則這樣操做正確。
但要更新有數據的blob字段,則只能更新部分字節,原字段數據超出長度部分的字節內容不變。
若是確實須要更新有數據的blob字段(也叫覆蓋式更新),則可在下面兩種方法中選擇其一:
一、在更新前(setBlob方法中select執行前)先將blob字段清空:
\"UPDATE \" + tableName + \" SET content=EMPTY_BLOB() WHERE id= \'\" +ID + \"\'\"
再執行你的更新代碼。
二、確保每次寫入的字段長度固定。即便data的字節數少,也要保證data1的長度固定,
也即data1定義長度要與data無關。
採用第一種方案的代碼以下:
java
-
-
-
-
-
- public static boolean setBlob(String tableName,int ID, String data,Connection conn) {
- Statement statement = null;
- ResultSet ret = null;
- String insertBlob = "select content from " + tableName + " where id = '"+ID+"' for update ";
- String flushBlob="update "+tableName+" set content=EMPTY_BLOB() where id="+ID;
- boolean flg = false;
- try {
- statement = conn.createStatement();
- statement.execute(flushBlob);
- ret = statement.executeQuery(insertBlob);
- oracle.sql.BLOB blob = null;
- if (ret.next()) {
- blob = ((OracleResultSet) ret).getBLOB(1);
- }
- OutputStream outstream = blob.getBinaryOutputStream();
- byte[] data1 = data.getBytes();
- outstream.write(data1, 0, data1.length);
- outstream.flush();
- outstream.close();
- flg = true;
- } catch (SQLException e1) {
- e1.printStackTrace();
- flg = false;
- } catch (IOException e1) {
- e1.printStackTrace();
- flg = false;
- } finally {
- try {
- if(statement != null) {
- statement.close();
- }
- if(ret != null) {
- ret.close();
- }
- }catch(Exception ex) {
- ex.printStackTrace();
- }
- }
-
- return flg;
- }
感受這個不錯~ 出處:http://andy99.javaeye.com/blog/476814sql