赞
踩
您可以使用setFetchSize(rows)来优化提取大小,该提取大小从DB中一次获取指定的行数。
conn = getDbConnection();
Statement createStatement = conn.createStatement();
createStatement.setFetchSize(1000);
ResultSet rs = createStatement.executeQuery(“Select * from myTable”);
while (rs.next())
{
//do nothing
}请注意,fetchSize只是对DB的提示,它可能会忽略该值。只有测试才能揭示它是否是最佳的。
此外,在您的情况下,最好更改Statement的Scrollable属性,因为您可能不会立即处理所有记录。选择哪个可滚动选项取决于您是否要在迭代时查看其他更改。
//TYPE_FORWARD_ONLY
// The constant indicating the type for a ResultSet object
// whose cursor may move only forward.
conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);要么
//TYPE_SCROLL_INSENSITIVE
// The constant indicating the type for a ResultSet object that is
// scrollable but generally not sensitive to changes made by others.
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);要么
//TYPE_SCROLL_SENSITIVE
// The constant indicating the type for a ResultSet object that is
// scrollable and generally sensitive to changes made by others.
conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);有关详细信息,请参阅JDBC API Guide
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。