今天在做一个数据库查询的时候遇到一个关于ResultSet.next()方法的问题。在查询出结果集以后,如果我先调用一次ResultSet.next()以后,再次调用ResultSet.next()就会出现空指针异常。代码如下:pstm3 = connection.prepareStatement(sql2); rsResult = pstm3.executeQuery();
boolean flag=rsResult.next();
System.err.println(flag+"------");
if (rsResult.next()) {
jysString = rsResult.getString(6);
}如果我直接循环结果集就没有问题:pstm3 = connection.prepareStatement(sql2); rsResult = pstm3.executeQuery();
if (rsResult.next()) {
jysString = rsResult.getString(6);
}所以,我就想看ResultSet.next()方法是怎么实现的,在eclipse中我点进去这个方法,却发现这只是一个接口boolean next() throws SQLException;所以我想问一下,ResultSet.next()的机制是什么样的,怎么在eclipse中查看它的源代码?谢谢各位
2 回答

翻翻过去那场雪
TA贡献2065条经验 获得超14个赞
点进去是接口,说明jdk只定义了接口,源码需要第三方自己实现,类似servlet的api,根据不同的第三方服务提供商,实现也会有差异。如果你用的数据库是mysql,ResultSet的具体实现可以在mysql-connector-java.jar这种第三方jar里找

动漫人物
TA贡献1815条经验 获得超10个赞
A ResultSet is initially positioned before its first row, the first
call to next makes the first row the current row; the second call
makes the second row the current row, etc. If an input stream from the
previous row is open, it is implicitly closed. The ResultSet's warning
chain is cleared when a new row is read
上面是文档的描述,意思是当你调用一次next()的时候,游标即文中提到的position会往下移动。
添加回答
举报
0/150
提交
取消