正文

疑问:resultset中的next与get方法2008-02-22 10:59:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/daweizhao/32858.html

分享到:

resultset即是JDBC中执行数据库查询语句后的查询结果集合,例如保存此SQL语句select distinct property4 from answer_new的结果[d1, d2, d3],但是今天遇到了一个取出来的问题

因为我只要查询出一个property4就可以了,所以自己知道只有一列查询结果,因此就不想用rs.next方法,直接使用getString(int columnIndex) 方法(将columIndex直接赋值为1即可),但是却运行出如下错误:

Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid operation for the current cursor position.
 at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
 at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
 at com.microsoft.jdbc.base.BaseResultSet.validateCursorPosition(Unknown Source)
 at com.microsoft.jdbc.base.BaseResultSet.getString(Unknown Source)
 at datamining.PropertyReduction.getDecisionProperty(PropertyReduction.java:30)
 at datamining.PropertyReduction.main(PropertyReduction.java:108)

后来查了些资料,加入rs.next方法就好了,如下:

  while(rs.next()){
   decisionProperty.add(rs.getString(1));
  }

为什么呢?

后来GOOGLE了下:

报错信息[Microsoft][SQLServer 2000 Driver for JDBC]Invalid operation for the current cursor position.为当所查记录为空的时候报错

而next方法作如下解释:将指针从当前位置下移一行。ResultSet 指针最初位于第一行之前;第一次调用 next 方法使第一行成为当前行;第二次调用使第二行成为当前行,依此类推。

两下对比,即若没有调用next方法时,resultset的初始指针并没有指向查询结果的第一行,为空,则。。。。。

想偷懒,也要偷对了才行啊,:) 

阅读(4656) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册