今天在做一個數據庫查詢的時候遇到一個關于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
提交
取消