Java操作数据库中 ResultSet的next()方法的一个小疑问

ORACLE800 2008-05-23 01:37:52
ResultSet 有个next()方法,看书上解释说, next()方法是得到下一条记录,当到达结果集的结束位置的时候,该方法会返回false。

注意 下一条 3个字。
如果数据库中只有一个字段a,很明显,这个next()方法得到的应该是字段a了。
我的问题在这里,一开始指针是在什么地方的?
...全文
2867 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
下浪淘沙 2010-11-26
  • 打赏
  • 举报
回复
读源代码啊,美女
fellatioyzx 2010-01-25
  • 打赏
  • 举报
回复
第一条的前面应该是个NIL,类似NULL的东西,但就是个空指针的指向

整个数据库读完了会转成类似链表的东西,这个链表前十NIL,后面也是NIL
刚开始的时候,指针指向NIL,全部读完后,如果指针指向NIL,程序则判明读完
wenjjing2lianee 2009-01-22
  • 打赏
  • 举报
回复
还是个未解之迷哦
????????
servaywong 2008-06-01
  • 打赏
  • 举报
回复
下面是jdk1.5对ResultSet的解释,这是对第1小问的解释.


public interface ResultSet

A table of data representing a database result set, which is usually generated by executing a statement that queries the database.

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.
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. It is possible to produce ResultSet objects that are scrollable and/or updatable. The following code fragment, in which con is a valid Connection object, illustrates how to make a result set that is scrollable and insensitive to updates by others, and that is updatable. See ResultSet fields for other options.


Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");
// rs will be scrollable, will not show changes made by others,
// and will be updatable


The ResultSet interface provides getter methods (getBoolean, getLong, and so on) for retrieving column values from the current row. Values can be retrieved using either the index number of the column or the name of the column. In general, using the column index will be more efficient. Columns are numbered from 1. For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.
For the getter methods, a JDBC driver attempts to convert the underlying data to the Java type specified in the getter method and returns a suitable Java value. The JDBC specification has a table showing the allowable mappings from SQL types to Java types that can be used by the ResultSet getter methods.


Column names used as input to getter methods are case insensitive. When a getter method is called with a column name and several columns have the same name, the value of the first matching column will be returned. The column name option is designed to be used when column names are used in the SQL query that generated the result set. For columns that are NOT explicitly named in the query, it is best to use column numbers. If column names are used, there is no way for the programmer to guarantee that they actually refer to the intended columns.

servaywong 2008-06-01
  • 打赏
  • 举报
回复
我总觉得大家还是没有能够从根本上解决问题.问题问得很清楚了,我们把问题量化有两问的话,我们大家只是回答了第一小问,大家说得都很正确,确实是在第一条记录前面,但具体位置呢?
bojianyu 2008-06-01
  • 打赏
  • 举报
回复
rs刚开始所处的位置-->****************
第一条记录
第....条记录
****************

记录首尾都有一个“空行”
ORACLE800 2008-06-01
  • 打赏
  • 举报
回复
顶23楼~总算有人明白我的意思了 ^_^
可能有点钻牛角尖~但是 还是很好奇!!!!
A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.
也只是回答了第一个问题~
第一个问题太简单了~几乎人人都知道 ~但是第二个小问呢?
到底第一条记录的前面是在什么地方?
热烈的红颜 2008-05-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 java2000_net 的回复:]
一开始指针是在什么地方的?


在第一条的前面

-----> 指针
-> 第一条记录
-> 第二条记录
-> 最后一条记录,再next,会返回false
[/Quote]
就是这样的 非常正确
我给你形象点就是
开始的ResultSet rs;
此时 rs 相当于是悬空的 不指向任何一条记录
当rs.next()为true;就指向第一条了
依次指向下一条记录
..........................
当到达最后的时候 则rs.next又回到空中 则为 false
NULL_8757512 2008-05-31
  • 打赏
  • 举报
回复
没准就指的是第一条记录呢,当第一次调用next()的时候,系统判断是第一次调用,所以指针不往下移动而已。

胡说的啊 见笑^_^。
caicaizhao 2008-05-31
  • 打赏
  • 举报
回复
到底是哪不是很清楚,反正就是第一条记录的前面,我也不明白啊
ORACLE800 2008-05-31
  • 打赏
  • 举报
回复
呵呵 谢谢老紫竹等各位
我已经知道是在第一条记录前面,可是到底是哪里?我知道是前一个,问题是前一个的指针指到哪里?
比如说,是不是指向结果集ResultSet这个对象的初始位置?

可能我这个问题有点钻牛角尖了 ^_^
老紫竹 2008-05-26
  • 打赏
  • 举报
回复
早上在门外排队买早点,一共10个人。

好了,买早点的喊,下一位
第一个人进来了,处理完毕后 下一位
第二个人进来了.....


你知道指针在哪里了吗?在第一个人的前面,下一位就是那个第一个人。

awusoft 2008-05-26
  • 打赏
  • 举报
回复
晕死,就是一个位置好了.如果你要读取里边的记录,首先要把指针往下移,如果移不成功就表示没有下一条.
比如说:查询某条记录得到一个rs.刚才指针位于任何记录之前(注意:有可能有,有可能没有记录.)如果有记录,
那么第一次rs.next()就会是true,这时候就指向了第一条记录,依次取出其中 的各个字段,记住这时候指针是指向第一条记录了.然后循环继续执行rs.next()就是再往下移一次,判断后边还有没有记录,如果直接到没有后边的记录.
刚开始:
rs-->某个位置(你不用管太多,它就是在任何记录之前,你要取数据的话,首先要往后移一条再说,JDBC第一次就是使指针位于任何记录前)
第一条记录.
第二第记录.

代码:
while(rs.next())
{
rs.getString(1);
}

第一次进行循环体先往下移,这时候rs指向第一条记录,返回的是true,然后就是取第一个字段,取完后,继续执行rs.next(),判断后边还有没有,发现有,OK,返回true,这时候rs指向第二记录了.然后又是取第一个字段值,取完继续执行rs.next(),因为rs已经指向第二条记录,它后边没有了,所以返回的是false,循环也就结束了.
ORACLE800 2008-05-25
  • 打赏
  • 举报
回复
谢谢各位。
现在知道是在第一条记录的前面,问题是,第一条记录的前面是什么呢?是像13楼说的那样吗?
oyhj_nicholas 2008-05-24
  • 打赏
  • 举报
回复
一开始在返回的记录集的第一条记录之前,每读出一条记录,
游标移动到下一条记录处,当读取完所有的记录后,游标已经处于
最后一条记录之后了。
a6213136 2008-05-24
  • 打赏
  • 举报
回复
呵呵 看着有点影响 慢慢来
我也刚看到 .next()了
正想问下这个问题呢
ADMIN1359984276 2008-05-24
  • 打赏
  • 举报
回复
结果集无非不就是数组和集合吗。开始指针只是指向了这个实例,当判断该实例不为null时往下进行操作.
ADMIN1359984276 2008-05-24
  • 打赏
  • 举报
回复
呵呵,楼主有意思.
老紫竹 2008-05-23
  • 打赏
  • 举报
回复
一开始指针是在什么地方的?


在第一条的前面

-----> 指针
-> 第一条记录
-> 第二条记录
-> 最后一条记录,再next,会返回false
ayangtutu 2008-05-23
  • 打赏
  • 举报
回复
改正一下
next() 返回的是boolean 判断是否有下一条记录 开始时指针在第一条记录的前面
加载更多回复(8)

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧