如何獲取ResultSet的行數和列數
http://www.cnblogs.com/kane1990/archive/2011/12/25/2300961.html
方法1:用select count語句,然后直接從ResultSet里面獲取結果:
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select count(*) as rowCount from tableName");
resultSet.next();
int rowCount = resultSet.getInt("rowCount");
方法2:遍歷Resultset,用一個變量記錄行數:
int count = 0;
while(resultSet.next()) {
count = count + 1;
}
方法3:創建Statement的時候,加上兩個參數,這樣獲得的結果集,指針就可以在其中自由移動
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet resultSet = statement.executeQuery("select * from " + tableName);
int rowCount = 0;
resultSet.last();
rowCount = resultSet.getRow();
//其中resultSet.last()就是將指針移動到結果集的最后一條記錄;然后用resultSet.getRow()獲取指針當前所在的行號(從1開始)
//如果接下來你還要使用結果集,別忘了將指針移到第一行:
resultSet.first();