以下是网上的一些代码:
import java.sql.*;    
import java.io.*;    
import oracle.sql.*;    
public class WriteBlob {    
   
  public static void main(String[] args) {    
   
    try {    
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());    
      Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","fankai","fankai");    
      conn.setAutoCommit(false);    
   
      BLOB blob = null;    
   
      PreparedStatement pstmt = conn.prepareStatement("insert into javatest(name,content) values(?,empty_blob())");    
      pstmt.setString(1,"fankai");    
      pstmt.executeUpdate();    
      pstmt.close();    
   
      pstmt = conn.prepareStatement("select content from javatest where name= ? for update");    
      pstmt.setString(1,"fankai");    
      ResultSet rset = pstmt.executeQuery();    
      if (rset.next()) blob = (BLOB) rset.getBlob(1);    
   
      String fileName = "oraclejdbc.jar";    
      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);    
      /*   
      byte[] data = new byte[blob.getBufferSize()];  另一种实现方法,节省内存   
      while ((count = fin.read(data)) != -1) {   
        total += count;   
        out.write(data, 0, count);   
      }   
      */   
   
      fin.close();    
      out.close();    
   
      pstmt.setBlob(1,blob);    
      pstmt.setString(2,"fankai");    
   
      pstmt.executeUpdate();    
      pstmt.close();    
   
      conn.commit();    
      conn.close();    
    } catch (SQLException e) {    
      System.err.println(e.getMessage());    
      e.printStackTrace();    
    } catch (IOException e) {    
      System.err.println(e.getMessage());    
    }    
  }    
   
}  
其中:
pstmt = conn.prepareStatement("update javatest set content=? where name=?");    
是必须的吗?为什么有的例子中没有``
还有如果我有两个表A和B`我想将A中的BLOB(photo)转存到B中``
可以这么做吗?
pstmt = conn.prepareStatement("select photo from A where name= ? for update");    
      pstmt.setString(1,"aaa");    
      ResultSet rset = pstmt.executeQuery();    
      if (rset.next()) blob = (BLOB) rset.getBlob(1);    
然后直接:
pstmt = conn.prepareStatement("update javatest set content=? where name=?");    
      pstmt.setString(1,blob);
      pstmt.setString(2,"aaa");
可以吗?如果不可以``我该怎么做``谢谢各位大虾了``        

解决方案 »

  1.   

    你写个存储过程在数据库服务器上实现blob的转存不就可以了吗
      

  2.   

    其实不是转存这么简单的``上面只是个例子``
    是两个用户``他们拥有的表是相同的``要将一个用户所有表中的数据存到另一个用户的对应表中中``
    不过这些表中有一些复杂的逻辑关系以及用序列生成的ID号不能重复``需要修改一些关于ID字段的数据``必须用JAVA程序做一个处理``
    这些我都做的差不多了``就是不知道BLOB字段该如何转存``
    我知道一般BLOB写入是需要用到流处理的``但是我已经获得有数据的BLOB对象了``想知道能不能简单的实现转存``而不用写进流再读出流``
    谢谢楼上朋友的热心``
      

  3.   

    加入ORACLE群吧!群号是:19312711
      

  4.   

    如果我有两个表A和B`我想将A中的BLOB(photo)转存到B中``
    可以这么做吗?
    pstmt = conn.prepareStatement("select photo from A where name= ? for update");    
          pstmt.setString(1,"aaa");    
          ResultSet rset = pstmt.executeQuery();    
          if (rset.next()) blob = (BLOB) rset.getBlob(1);    
    然后直接:
    pstmt = conn.prepareStatement("update B set photo=? where name=?");    
          pstmt.setString(1,blob);
          pstmt.setString(2,"aaa");