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?
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With