在oracle中更新BLOB字段出现问题.

ls176 2004-07-27 12:01:29
如果数据表中有数据的话,并且数据的长度大于更新的数据的长度,那么更新后,数据长度还是为原有数据的长度.
我是使用下面的语句更新的,要怎么改才能清空原有的数据?
BLOB b = ( BLOB )dr.getItem( 0 );
OutputStream outs = b.getBinaryOutputStream();
ByteArrayInputStream bais = new ByteArrayInputStream( com.system.util.util.BASE64Decoder( dm.getParams().get( paramName ).getValue() ) );
byte[] buf = new byte[ b.getChunkSize() ];
int len;
{
outs.write( buf , 0 , len );
}
outs.flush();
outs.close();
bais.close();
...全文
341 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuchuntao 2004-12-10
  • 打赏
  • 举报
回复
import oracle.sql.*;
import java.sql.*;
import java.io.*;

public class WriteBlob {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.0.204:1521:main", "lct", "lct");
conn.setAutoCommit(false);
BLOB blob = null;
//清空
PreparedStatement pstmt = conn.prepareStatement(
"update javatest set content=empty_blob() where name=?");
pstmt.setString(1, "liuchuntao");
pstmt.executeUpdate();

pstmt = conn.prepareStatement(
"select content from javatest where name= ? for update");
pstmt.setString(1, "liuchuntao");
ResultSet rset = pstmt.executeQuery();
if (rset.next()) {
blob = (BLOB) rset.getBlob(1);

}
String fileName = "e:\\pic\\1.jpg";
File f = new File(fileName);
FileInputStream fin = new FileInputStream(f);
System.out.println("file size = " + fin.available());

pstmt = conn.prepareStatement(
"update javatest set content=? where name=?");

OutputStream out = blob.getBinaryOutputStream();

int count = -1, total = 0;
byte[] data = new byte[ (int) fin.available()];
fin.read(data);
out.write(data);

fin.close();
out.close();

pstmt.setBlob(1, blob);
pstmt.setString(2, "fankai");

pstmt.executeUpdate();
pstmt.close();

conn.commit();
rset.close();
conn.close();
}
catch (SQLException e) {
System.err.println(e.getMessage());
System.out.println(e.getErrorCode());
e.printStackTrace();
}
catch (IOException e) {
System.err.println(e.getMessage());
}
}

}
追求自由 2004-12-06
  • 打赏
  • 举报
回复
这种情况我碰到过,如果文件太长,哪么就用循环来处理,一次写一定长度的字节
huanlw 2004-12-06
  • 打赏
  • 举报
回复
我在写入BLOB字段是也遇到了同样的问题,更主要的是,在存入的数据大于2K时,无法正常读出
rambostar 2004-12-06
  • 打赏
  • 举报
回复
替换CLOB对象(将原CLOB对象清除,换成一个全新的CLOB对象),详细信息请参考:

http://www.s8s8.net/ipb/lofiversion/index.php/t20319.html
bafver 2004-09-11
  • 打赏
  • 举报
回复
我用过Clob大字段,不过也只是读里面的数据,没写过
ls176 2004-07-29
  • 打赏
  • 举报
回复
晕啊,两天了,还没有人回答我呀.
ls176 2004-07-28
  • 打赏
  • 举报
回复
没人知道吗?
wayto 2004-07-28
  • 打赏
  • 举报
回复
顶到望月湖去吃鱼
ls176 2004-07-28
  • 打赏
  • 举报
回复
顶一顶
stonegump 2004-07-27
  • 打赏
  • 举报
回复
有问题吧?len都没有初始化:
int len;
{
outs.write( buf , 0 , len );
}
wayto 2004-07-27
  • 打赏
  • 举报
回复
我也想知道啊,也顶一下!
ls176 2004-07-27
  • 打赏
  • 举报
回复
再顶
bafver 2004-07-27
  • 打赏
  • 举报
回复
ftherwktg
ls176 2004-07-27
  • 打赏
  • 举报
回复
hbzyduwu 2004-07-27
  • 打赏
  • 举报
回复
up
ls176 2004-07-27
  • 打赏
  • 举报
回复
不好意思,int len;下有个while
BLOB b = ( BLOB )dr.getItem( 0 );
OutputStream outs = b.getBinaryOutputStream();
ByteArrayInputStream bais = new ByteArrayInputStream( com.system.util.util.BASE64Decoder( dm.getParams().get( paramName ).getValue() ) );
byte[] buf = new byte[ b.getChunkSize() ];
int len;
while( ( len = bais.read( buf ) ) != -1 )
{
outs.write( buf , 0 , len );
}
outs.flush();
outs.close();
bais.close();

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧