Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 back and get this information again.

Is it possible?

like image 811
Mahdi_Nine Avatar asked Dec 04 '22 22:12

Mahdi_Nine


2 Answers

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.

like image 163
Pablo Santa Cruz Avatar answered Dec 10 '22 10:12

Pablo Santa Cruz


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.

like image 43
Costis Aivalis Avatar answered Dec 10 '22 09:12

Costis Aivalis