将ResultSet移到第一个 [英] Moving ResultSet to first
本文介绍了将ResultSet移到第一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似于rs的结果集对象,我使用了代码片段来计算行数.
I have a resultset obejct as rs and i used the fragment of code to count the number of rows..
while(rs.next())
count++;
,我不得不再次使用相同的结果集来检索数据.我用的是方法
and i has to use the same resultset again to retrieve the data. I used the method
rs.beforefirst();
但它不起作用...控件未进入
but it is not working... control is not entering into
while(rs.next()){
cid=rs.getInt(1);
taskdate=rs.getString(2);
tasktime=rs.getString(3);
addr=rs.getString(4);
}
我必须根据我的查询返回4行.但这不是???
I has to return 4 rows according to my query.. but it doesn't ???
推荐答案
结果集是仅向前的.如果要导航回第一条记录,则需要创建可滚动结果集.
Resultset is the forward only . If you want to navigate back to the first record you need to create scrollable resultset .
// Create a scrollable result set
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
// Move cursor forward
while (resultSet.next()) {
// Get data at cursor
String s = resultSet.getString(1);
}
// Move cursor backward
while (resultSet.previous()) {
// Get data at cursor
String s = resultSet.getString(1);
}
// Move cursor before first record
resultSet.beforeFirst(); //required statement in this case
这篇关于将ResultSet移到第一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文