前几天作项目,用到了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