开发者

How can I get previous item in resultset?

Suppose we have this query:

ResultSet result = ("select * from table_name");

Now we can get information from result in this form

while(result.next())
{
  .
  .
  .
}

but in this form we can go forward. Now I want to know if it possible that result get back?

For example we get information name='sam', fname='nic'. And for next I want go 开发者_StackOverflowback and get this information again.

Is it possible?


Yes, JDBC supports that. You need to a scrollable resultset.

Something like this:

Statement st = conn.createStatement(
   ResultSet.TYPE_SCROLL_INSENSITIVE, 
   ResultSet.CONCUR_READ_ONLY
);
ResultSet rs = st.executeQuery("select * from table_name");

Then you just use ResultSet object rs to move within your results:

  1. first
  2. absolute
  3. last
  4. next
  5. previous

Are methods that will help you navigate.


When you create a Statement without passing parameters to it, it defaults to ResultSet.TYPE_FORWARD_ONLY.

Statement st = con.createStatement();

If you want to be able to scroll back you set the first parameter to either ResultSet.TYPE_SCROLL_INSENSITIVE or ResultSet.TYPE_SCROLL_SENSITIVE. (Insensitive or sensitive in regard to changes to the database by others.)

Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

CONCUR_UPDATABLE will allow you to update the ResultSet if you want, unless you do not need to, i which case you set it to CONCUR_READ_ONLY.

Read this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜