我是用java语言作的Oracle的Blob字段update这个字段的时候
我现在做法是,先select..... for update出来
然后,getblob再把blob对象转换为outputstream
再outputstream.write(string.getByte())
这样的话就会有一个问题
如果原来的blob里面是123 修改成abcdefg没什么问题
结果就会是abcdefg
但是,如果原来的blob是abcdefg,而换成123就出问题了
结果成为123defg
我现在做法是,先select..... for update出来
然后,getblob再把blob对象转换为outputstream
再outputstream.write(string.getByte())
这样的话就会有一个问题
如果原来的blob里面是123 修改成abcdefg没什么问题
结果就会是abcdefg
但是,如果原来的blob是abcdefg,而换成123就出问题了
结果成为123defg
不会吧
给各解决的办法吧,高手救我,拉兄弟一把
应该用
update table name=hextoraw('加入要修改的内容')
你执行的是覆盖式更新,你应该将原CLOB对象清除,再换成一个全新的CLOB对象.先用这一句,再执行你的更新代码。
http://fengyu.china.com/clob.htm
you can make a dump file firstly, and then open the dump file by
a (16 bit )texteditor,just like editorplus etc.
and then change what you want to change field (blob field).
finally you must import the dump file.btw : Oracle doesn't encourage this way because some error will be happen
in sometimes.
{
BLOB blob = getEmptyBlob(conn);
blob.putBytes(1,data);
pstmt.setBlob(1,blob);
pstmt.executeUpdate();
}
public static BLOB createEmptyBlob( Connection conn )
{
return BLOB.createTemporary(conn,true,1);
}
可以保证正确性和良好的速度。
(
x NUMBER(5) NOT NULL,
b BLOB DEFAULT EMPTY_BLOB() NOT NULL
)插入: stmt.executeUpdate("insert into txjblob values(1,EMPTY_BLOB())");
ResultSet rs = stmt.executeQuery("SELECT b FROM fff WHERE x=1 FOR UPDATE NOWAIT");
FileInputStream fin = new FileInputStream("c:chart.gif");
byte[] blobBuf = new byte[(int)fin.available()];
fin.read(blobBuf);
fin.close(); if(rs.next()) {
System.out.println(blobBuf.length);
BLOB blob = (oracle.sql.BLOB)rs.getBlob(1);
OutputStream out = blob.getBinaryOutputStream();
out.write(blobBuf);
out.close();
conn.commit();
}更新: stmt.executeUpdate("UPDATE txjblob SET b=EMPTY_BLOB() WHERE x=1");
ResultSet rs = stmt.executeQuery("SELECT b FROM fff WHERE x=1 FOR UPDATE NOWAIT");
FileInputStream fin = new FileInputStream("c:chart.gif");
byte[] blobBuf = new byte[(int)fin.available()];
fin.read(blobBuf);
fin.close(); if(rs.next()) {
System.out.println(blobBuf.length);
BLOB blob = (oracle.sql.BLOB)rs.getBlob(1);
OutputStream out = blob.getBinaryOutputStream();
out.write(blobBuf);
out.close();
conn.commit();
}删除:stmt.executeUpdate("DELETE txjblob WHERE x=1");读取: ResultSet rs = stmt.executeQuery("select b from fff where x=1");
if(rs.next()){
BLOB blob = (BLOB)rs.getBlob(1);
InputStream is = blob.getBinaryStream();
// InputStream is = rs.getBinaryStream(1);
FileOutputStream os = new FileOutputStream("c:1.gif");
int b;
byte[] buffer = new byte[1024];
while( (b=is.read(buffer)) != -1){
os.write(buffer,0,b);
}
is.close();
os.close();
}