on 08-28-2007 7:46 AM
Hi
I have a requirement where based on a condition i need to traverse to the previous record in the database. But i am not able to use it. It throws following exception"Invalid operation for forward only resultset : previous".
The code that i have is:
while(resultSet.next())
{
String abc= resultSet.getString(8);
while(resultSet.getString(8).equals(abc))
{
IBS ibsAcctB= new IBS();
ibsAcctB.setIbsAcct(resultSet.getString(9));
ibsAcctResult.add(ibsAcctB);
resultSet.next();
count++;
}
AccountBean AcctBean = new AccountBean();
AcctBean.setibsAccounts(ibsAcctResult);
AcctBean.setAcct(abc);
AccountResult.add(sapAcctB);
resultSet.previous();
this.setAccountResult(AccountResult);
this.setUserId(resultSet.getString(1));
this.setUserName(resultSet.getString(2));
this.setUserCountry(resultSet.getString(3));
this.setUserCompany(resultSet.getString(4));
this.setUserType(resultSet.getString(5));
this.setLanguage(resultSet.getString(6));
}
In the above piece of code, when the condition is not satisfied, cursor moves out of the inner loop, and then the next iteration of the outer loop starts. but here, when the user once moves out of the inner loop, resultSet.next() has incremented the counter. At that point, the outer counter is again incremented. Which results in one record getting skipped. So i wanted to decrement the counter by 1 in case the inner condition is not satisfied. But i am not able to.
If anyone has worked on it, please let me know..
Thanks & regards,
Anupreet
Hi
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");
This is the standard format you can use
while viewing through the ResultSet in your while loop
you can use rs.previous()
but be care full while using rs.previous()
first check "sFirst()" or isBeforeFirst()
Which returns boolean if it is "true" dont use previous()
Regards
Abhijith YS
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anupreet,
As Omri stated, you may use the following code to set the cursor's scrolling mode insensitive.
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");
While using the previous() method for the ResultSet, use the same while condition to check if ResultSet.previous() returns true.
Regards,
Rekha Malavathu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anupreet Kaur Chhokar,
See javadoc for resultSet: http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html
default cursor is forward only.
Try to change it
Omri
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
74 | |
9 | |
7 | |
6 | |
6 | |
6 | |
6 | |
6 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.