Go To Last Row From Result Set In Jdbc With Sql Server
i try to select from my table, only select the last row. I've tried this : rset = s.executeQuery('select noorder from orders'); rset.last(); String noorder = rset.getString('noorde
Solution 1:
A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row.
At code level you can do the following thing
Statementstatement= connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSetresultSet= statement.executeQuery("select noorder from orders");
resultSet.afterLast();
while (resultSet.previous()) {
StringproductCode= resultSet.getString("col_one");
StringproductName= resultSet.getString("col_two");
}
connection.close();
Solution 2:
The isLast() should be what you're looking for.
ResultSetrs= stmt.executeQuery(query);
while(rs.next()) {
if(rs.isLast()) {
// is last row in ResultSet
}
}
Solution 3:
Remember to apply an order by clause otherwise the last entry in your ResultSet may not be what you expect.
Solution 4:
You can use:
connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
Solution 5:
rset = s.executeQuery("SELECT * FROM table_name ORDER BY unique_column DESC LIMIT 1");
Stringnoorder= rset.getString("noorder");`
Post a Comment for "Go To Last Row From Result Set In Jdbc With Sql Server"