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的初始指针并没有指向查询结果的第一行,为空,则。。。。。
想偷懒,也要偷对了才行啊,:)
评论