需求:有两个表a和b,分别都有一个blob字段,现在需要用java程序把a中的blob字段值导入到b字段中的blob字段。请大家有什么好的方法,不内存溢出。谢谢各位。

解决方案 »

  1.   

    通过sql直接完成
    update b set b.blob_field = (select a.blob_field where a.id = 123) where b.id=123;
      

  2.   

    update b,a set b.blob=a.blob where b.id=a.id将a所有的blob导入到对应的b.blob中
      

  3.   

    可以考虑,用数据持久化成的开源框架解决,比如hibernate提供的缓存机制,或者OSCache等;
      

  4.   

    import java.sql.*;
    import java.io.*;
    import com.microsoft.sqlserver.jdbc.SQLServerStatement;public class useAdaptiveBuffering {

       public static void main(String[] args) {      // Create a variable for the connection string.
          String connectionUrl = 
             "jdbc:sqlserver://localhost:1433;" +
                "databaseName=AdventureWorks;integratedSecurity=true;" +
                "responseBuffering=adaptive";      // Declare the JDBC objects.
          Connection con = null;
          Statement stmt = null;
          ResultSet rs = null;  
               
          try {
              // Establish the connection.
              Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
              con = DriverManager.getConnection(connectionUrl);
             
              // Create test data as an example.
              StringBuffer buffer = new StringBuffer(4000);
              for (int i = 0; i < 4000; i++) 
                  buffer.append( (char) ('A'));
        
              PreparedStatement pstmt = con.prepareStatement(
                        "UPDATE Production.Document " +
                         "SET DocumentSummary = ? WHERE (DocumentID = 1)");          pstmt.setString(1, buffer.toString());
              pstmt.executeUpdate();
              pstmt.close();          // In adaptive mode, the application does not have to use a server cursor 
              // to avoid OutOfMemoryError when the SELECT statement produces very large
              // results.           // Create and execute an SQL statement that returns some data.
              String SQL = "SELECT Title, DocumentSummary " +
                  "FROM Production.Document";
              stmt = con.createStatement();          // If you have not already set the responseBuffering=adaptive in the 
              // connection properties, you can set the response buffering to adaptive 
              // on the statement level before executing the query, such as:       
              SQLServerStatement SQLstmt = (SQLServerStatement) stmt;
              SQLstmt.setResponseBuffering("adaptive");       
          
              // Display the response buffering mode.
              System.out.println("Response buffering mode has been set to " +
                 SQLstmt.getResponseBuffering());          
              
              // Get the updated data from the database and display it.
              rs = stmt.executeQuery(SQL);
                      
              while (rs.next()) {
                   Reader reader = rs.getCharacterStream(2);
                   if (reader != null)
                   {
                      char output[] = new char[40];
                      while (reader.read(output) != -1)
                      {
                          // Do something with the chunk of the data that was                       
                          // read.
                      }    
     
                      System.out.println(rs.getString(1) + 
                          " has been accessed for the summary column.");
                      // Close the stream.
                      reader.close();
                   }
              }
          }
          // Handle any errors that may have occurred.
          catch (Exception e) {
             e.printStackTrace();
          }
          finally {
              if (rs != null) try { rs.close(); } catch(Exception e) {}
              if (stmt != null) try { stmt.close(); } catch(Exception e) {}
           if (con != null) try { con.close(); } catch(Exception e) {}
          }
       }
    }这是我从微软jdbc连接的参考文档拷过来的,希望你能得到些启示
    其中最重要的就是2点就是:
    1,responseBuffering=adaptive,这个就是避免产生的结果集比较大,内存溢出...(当然,前提是你用的sqlserver数据库加jdbc3.0的驱动)
    2,Reader reader = rs.getCharacterStream(int columnindex) 然后对blob字段进行读写的时候采用一个字符数组作为缓冲,这也可以避免内存溢出的问题