这不是一个很简单的手术吗?但是,我看到既没有size()也没有length()方法。


当前回答

ResultSet rs = ps.executeQuery();
int rowcount = 0;
if (rs.last()) {
  rowcount = rs.getRow();
  rs.beforeFirst(); // not rs.first() because the rs.next() below will move on, missing the first element
}
while (rs.next()) {
  // do your standard per row stuff
}

其他回答

SELECT COUNT(*) FROM…查询。

OR

int size =0;
if (rs != null) 
{
  rs.last();    // moves cursor to the last row
  size = rs.getRow(); // get row id 
}

在这两种情况下,都不需要遍历整个数据。

ResultSet rs = ps.executeQuery();
int rowcount = 0;
if (rs.last()) {
  rowcount = rs.getRow();
  rs.beforeFirst(); // not rs.first() because the rs.next() below will move on, missing the first element
}
while (rs.next()) {
  // do your standard per row stuff
}

我检查了ResultSet接口的运行时值,发现它几乎一直都是一个ResultSetImpl。ResultSetImpl有一个名为getUpdateCount()的方法,它返回您正在寻找的值。

下面的代码示例应该足够了: ResultSet ResultSet = executeQuery(sqlQuery); double rowCount = ((ResultSetImpl)resultSet).getUpdateCount()

我知道向下转换通常是一个不安全的过程,但这个方法还没有让我失败。

int i = 0;
while(rs.next()) {
    i++;
}
theStatement=theConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

ResultSet theResult=theStatement.executeQuery(query); 

//Get the size of the data returned
theResult.last();     
int size = theResult.getRow() * theResult.getMetaData().getColumnCount();       
theResult.beforeFirst();