我建议LIMIT
使用 PreparedStatement 更改您的查询。就像是:
SELECT * FROM table1 LIMIT ?,?
这有几个优点:
- 您不会一次性获取所有内容 - 如果您的表格中有很多行要处理,有时可能会带来性能优势
- 您可以更改预定义要在每个批次中获取的元素数量
所以你的代码看起来像这样:
PreparedStatement ps = null;
ResultSet rs = null;
final int FETCH_LIMIT = 5; //number of elements to fetch per batch
final int BATCH_LIMIT = 3; //number of batches you would want
int currentRows = 0;
try{
ps = connection.prepareStatement("SELECT * FROM table1 LIMIT ?,?");
for(int currentBatch = 0; currentBatch < BATCH_LIMIT; currentBatch++){
ps.clearParameters();
ps.setInt(1, currentRows);
ps.setInt(2, currentRows + FETCH_LIMIT);
try{
rs = ps.executeQuery();
while(rs.next()){
// do your work
}
}catch(Exception exe){
//manage exception
}finally{
//manage resultset
}
currentRows += FETCH_LIMIT;
}
}catch(Exception exe){
//Handle your exception
}
finally{
//Manage your resources
}