Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC2.0 feature. rs.absolute and relative( )

 
popeye tb
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can Pls anyone help me out wats wrong with the following code.
--------------------------------------
ResultSet srs = stmt.executeQuery("SELECT * FROM COFFEES");
srs.absolute(4);
int rowNum = srs.getRow(); // rowNum should be 4
System.out.println("rowNum should be 4 :" + rowNum);
srs.relative(-2);
rowNum = srs.getRow(); // rowNum should be 2
System.out.println("rowNum should be 2 :" + rowNum);
srs.relative(2);
rowNum = srs.getRow(); // rowNum should be 4
System.out.println("rowNum should be 4 :" + rowNum);
--------------------------------------------
The output i get is correct in this case..as,
rowNum should be 4 : 4
rowNum should be 2 : 2
rowNum should be 4 : 4
But once i change the line below :
srs.relative(-2);
to
srs.relative(-3);
I get the following error :
SQL Exception
SQL State : null
Message : Cursor is positioned before the resultset.
Shouldnt the out be as below :
rowNum should be 4 : 4
rowNum should be 2 : 1
rowNum should be 4 : 4
The second line pointing to row one..
Thanks.
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No clue why that's not working... Scrollable resultsets are always a pain.
Oh, and the final output should be:
 
popeye tb
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya true, the output shd be 4 1 3. But it always show the error instead. Thanks anyway.
 
popeye tb
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i noticed something.. when i compile the code as below :
--------------------------------------
ResultSet srs = stmt.executeQuery("SELECT * FROM COFFEES");
srs.absolute(4);
int rowNum = srs.getRow(); // rowNum should be 4
System.out.println("rowNum should be 4 :" + rowNum);
srs.relative(-2);
srs.previous()
rowNum = srs.getRow(); // rowNum should be 2
System.out.println("rowNum should be 2 :" + rowNum);
srs.relative(2);
rowNum = srs.getRow(); // rowNum should be 4
System.out.println("rowNum should be 4 :" + rowNum);
--------------------------------------------
the second line is able to point to the first row. The output is :
rowNum should be 4 : 4
rowNum should be 2 : 1
rowNum should be 4 : 3
but if i use,
srs.relative(-1);
instead of,
srs.previous();
i get the same error :
rowNum should be 4 : 4
rowNum should be 2 : 0
SQL Exception
SQL State : null
Message : Cursor is positioned before the resultset.
Pls HELP!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic