需求:有两个表a和b,分别都有一个blob字段,现在需要用java程序把a中的blob字段值导入到b字段中的blob字段。请大家有什么好的方法,不内存溢出。谢谢各位。
解决方案 »
- 新手提问 如何让字符串中的指定内容全部高亮显示
- 启动都出错!到底什么回事啊!
- 【请教】关于换工作直接走人的问题
- 请教各位个问题,请进来看看~~
- 如何制作一个简单的能打开文本的程序
- 进者有分,问一个设计模式上的问题,大家参与讨论,在工程中很实用,关于CONNECTION每次用完后要关闭的问题,看谁能把这个问题复用封装的最好
- 偶是菜鸟,请大侠给推荐一下学习JDK1.4编程的最直接的参考资料和书籍!!!(20分)
- 十六进制转十进制的一段代码:请教一下。
- JTable如何排序??急切!!
- 我的java怎么会这样?
- Java正则表达式怎么验证输入的值必须为数字,不能为空,数值<1000???
- GUI代码在Eclipse里识别不了!请大家帮帮忙!在下谢谢
update b set b.blob_field = (select a.blob_field where a.id = 123) where b.id=123;
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字段进行读写的时候采用一个字符数组作为缓冲,这也可以避免内存溢出的问题