ResultSet方法“last”这是一种最佳方式吗?

时间:2022-11-11 05:00:27

I have this java code which does this

我有这个java代码,这样做

ResulSet rs = stmt.executeQuery();
while (rs.next()) {
    .... //do regular processing

    if (rs.last()) {
       //do last record processing
    }

}

3 个解决方案

#1


You should use: isLast() instead.

你应该使用:isLast()代替。

Be warned, not all the JDCB drivers support this feature. Obviously you have to check it with your installation.

请注意,并非所有JDCB驱动程序都支持此功能。显然你必须检查你的安装。

Most major DB work fine.

大多数主要DB工作正常。

The fixed code should look like this.

固定代码应如下所示。

if( rs.isLast() ) { 
   // do last record processing
}

#2


Personally, I would declare the variables that are retrieved from the query outside the loop, and then do the "last record processing" after the loop.

就个人而言,我会声明从循环外的查询中检索的变量,然后在循环之后执行“最后一次记录处理”。

ResulSet rs = stmt.executeQuery();
long primaryKey;

while (rs.next()) {
    primaryKey = rs.getLong(1);
    .... //do regular processing
}

// Do last record processing.
System.out.println("last primary key = " + primaryKey);

#3


No: rs.last() will actually send the cursor to the last row in the ResultSet, which is definitely not what you want to do. You would only ever process at most 2 rows (first and last) from the result set.

否:rs.last()实际上会将光标发送到ResultSet中的最后一行,这绝对不是您想要做的。您只能处理结果集中最多2行(第一行和最后一行)。

#1


You should use: isLast() instead.

你应该使用:isLast()代替。

Be warned, not all the JDCB drivers support this feature. Obviously you have to check it with your installation.

请注意,并非所有JDCB驱动程序都支持此功能。显然你必须检查你的安装。

Most major DB work fine.

大多数主要DB工作正常。

The fixed code should look like this.

固定代码应如下所示。

if( rs.isLast() ) { 
   // do last record processing
}

#2


Personally, I would declare the variables that are retrieved from the query outside the loop, and then do the "last record processing" after the loop.

就个人而言,我会声明从循环外的查询中检索的变量,然后在循环之后执行“最后一次记录处理”。

ResulSet rs = stmt.executeQuery();
long primaryKey;

while (rs.next()) {
    primaryKey = rs.getLong(1);
    .... //do regular processing
}

// Do last record processing.
System.out.println("last primary key = " + primaryKey);

#3


No: rs.last() will actually send the cursor to the last row in the ResultSet, which is definitely not what you want to do. You would only ever process at most 2 rows (first and last) from the result set.

否:rs.last()实际上会将光标发送到ResultSet中的最后一行,这绝对不是您想要做的。您只能处理结果集中最多2行(第一行和最后一行)。