java resultset 最后一行,使用sql server从jdbc中的结果集转到最后一行

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("noorder");`

rset is resultset, and s is statement. But it throw an exception : ResultSet may only be accessed in a forward direction`

I've tried this to :

if (rset != null) {

while(rset.next()){

rset.last();

}

}

Am I doing wrong? Any idea? Thanks

Edit :

This is the answer, as suggested by @Bhavik-Ambani (thanks for him). And this is my code :

Statement s2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

rset = s2.executeQuery("select noorder from orders");

rset.afterLast();

GETLASTINSERTED:

while(rset.previous()){

noorder = rset.getString("noorder");

break GETLASTINSERTED;//to read only the last row

}

Hope it will be help another. Java rocks!

解决方案

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

Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

ResultSet resultSet = statement.executeQuery("select noorder from orders");

resultSet.afterLast();

while (resultSet.previous()) {

String productCode = resultSet.getString("col_one");

String productName = resultSet.getString("col_two");

}

connection.close();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值