前几天做项目,用到了Oracle数据库,是关于更新Blob,代码是这样写的:
 

Java代码 复制代码
  1. /**   
  2.      * 描述: 添加某张表某条记录的content字段,此字段为 blob 型  
  3.      * param: 表名;主键;数据;数据库连接  
  4.      * return: 添加成功返回 true ;否则返回 false  
  5.      * */  
  6.     public static boolean setBlob(String tableName,int ID, String data,Connection conn) {   
  7.         Statement statement = null;   
  8.         ResultSet ret = null;   
  9.         String insertBlob = "select content from " + tableName + " where id = '"+ID+"' for update ";   
  10.         boolean flg = false;   
  11.         try {   
  12.             statement = conn.createStatement();   
  13.                  ret = statement.executeQuery(insertBlob);   
  14.             oracle.sql.BLOB blob = null;   
  15.             if (ret.next()) {   
  16.                 blob = ((OracleResultSet) ret).getBLOB(1);   
  17.             }   
  18.             OutputStream outstream = blob.getBinaryOutputStream();   
  19.             byte[] data1 = data.getBytes();   
  20.             outstream.write(data1, 0, data1.length);//就是这个地方出了问题,如果是修改,之前就有了长度为100字节的数据,而这次修改只有50字节数据,那么后面50个字节就不会被修改,仍然存在数据库中   
  21.             outstream.flush();   
  22.             outstream.close();   
  23.             flg = true;   
  24.         } catch (SQLException e1) {   
  25.             e1.printStackTrace();   
  26.             flg = false;   
  27.         } catch (IOException e1) {   
  28.             e1.printStackTrace();   
  29.             flg = false;   
  30.         } finally {   
  31.             try {   
  32.                 if(statement != null) {   
  33.                     statement.close();   
  34.                 }   
  35.                 if(ret != null) {   
  36.                     ret.close();   
  37.                 }   
  38.             }catch(Exception ex) {   
  39.                 ex.printStackTrace();   
  40.             }   
  41.         }   
  42.            
  43.         return flg;   
  44.     }  

 


/** 
	 * 描述: 添加某张表某条记录的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字段(也叫覆盖式更新),则可在下面两种方法中选择其一:
1、在更新前(setBlob方法中select执行前)先将blob字段清空:
\"UPDATE \" + tableName + \" SET content=EMPTY_BLOB() WHERE id= \'\" +ID + \"\'\"
再执行你的更新代码。
2、确保每次写入的字段长度固定。即使data的字节数少,也要保证data1的长度固定,
也即data1定义长度要与data无关。
采用第一种方案的代码如下:
 

Java代码 复制代码
  1. /**   
  2.      * 描述: 添加某张表某条记录的content字段,此字段为 blob 型  
  3.      * param: 表名;主键;数据;数据库连接  
  4.      * return: 添加成功返回 true ;否则返回 false  
  5.      * */  
  6.     public static boolean setBlob(String tableName,int ID, String data,Connection conn) {   
  7.         Statement statement = null;   
  8.         ResultSet ret = null;   
  9.         String insertBlob = "select content from " + tableName + " where id = '"+ID+"' for update ";   
  10.         String flushBlob="update "+tableName+" set content=EMPTY_BLOB() where id="+ID;   
  11.         boolean flg = false;   
  12.         try {   
  13.             statement = conn.createStatement();   
  14.             statement.execute(flushBlob);//首先清空content字段   
  15.             ret = statement.executeQuery(insertBlob);   
  16.             oracle.sql.BLOB blob = null;   
  17.             if (ret.next()) {   
  18.                 blob = ((OracleResultSet) ret).getBLOB(1);   
  19.             }   
  20.             OutputStream outstream = blob.getBinaryOutputStream();   
  21.             byte[] data1 = data.getBytes();   
  22.             outstream.write(data1, 0, data1.length);//就是这个地方出了问题,如果是修改,之前就有了长度为100字节的数据,而这次修改只有50字节数据,那么后面50个字节就不会被修改,仍然存在数据库中   
  23.             outstream.flush();   
  24.             outstream.close();   
  25.             flg = true;   
  26.         } catch (SQLException e1) {   
  27.             e1.printStackTrace();   
  28.             flg = false;   
  29.         } catch (IOException e1) {   
  30.             e1.printStackTrace();   
  31.             flg = false;   
  32.         } finally {   
  33.             try {   
  34.                 if(statement != null) {   
  35.                     statement.close();   
  36.                 }   
  37.                 if(ret != null) {   
  38.                     ret.close();   
  39.                 }   
  40.             }catch(Exception ex) {   
  41.                 ex.printStackTrace();   
  42.             }   
  43.         }   
  44.            
  45.         return flg;   
  46.     }  

感觉这个不错~ 出处:http://andy99.javaeye.com/blog/476814