该设计模式称为分页(使用光标)。例如,当您向 Oracle DB 发送查询时,返回的结果集中的默认结果数为 50。只有当您要求结果集获取时next()
,才会返回接下来的 50 个结果。这就是大多数 DB 的工作方式,并被设计为对这种模式有效(请参阅此代码示例):
public static void viewTable(Connection con, String dbName)
throws SQLException {
Statement stmt = null;
String query =
"select COF_NAME, SUP_ID, PRICE, " +
"SALES, TOTAL " +
"from " + dbName + ".COFFEES";
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "\t" + supplierID +
"\t" + price + "\t" + sales +
"\t" + total);
}
} catch (SQLException e ) {
JDBCTutorialUtilities.printSQLException(e);
} finally {
if (stmt != null) { stmt.close(); }
}
}
所以,你可以打开文件,写下你得到的每一“页”结果,请求下一页等等。一旦你完成了——关闭文件。