Skip to Content

resultSet.previous() not working

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Posted on Aug 28, 2007 at 07:08 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 28, 2007 at 08:31 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 30, 2007 at 03:10 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.