0

I have a problem with ResultSet last() function. some times it shows the correct data. in some other time it shows another result.

consider i have no data on the table, then it shows row count is 0, but some time it doesn't show any of value.

here my code:

if(rs.last()){
int rsCount = rs.getRow();
System.out.println("ResultSet Count is: "+rsCount);
}

please give me the solution for my problem.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • what you have in 'rs' object ? – Muhammad Suleman Jun 04 '15 at 11:16
  • Why do you want the count at all? Just process the `ResultSet`. Also: please clarify what you mean with _"but some time it doesn't show any of value."_ – Mark Rotteveel Jun 04 '15 at 11:17
  • look at http://stackoverflow.com/questions/8292256/get-number-of-rows-returned-by-resultset-in-java?rq=1 – Sarit Adhikari Jun 04 '15 at 11:18
  • i need count of a row in resultset, if there is no row in a result set, then i want to insert some data into table. so that i am using rs.last() function. –  Jun 04 '15 at 11:19
  • That still doesn't explain your problem; my guess is you get an `SQLException` that you are swallowing, but you don't provide enough context, nor enough supporting information. As to your use case: why not just select a count, or process the result set and take specific action if no rows were processed, or - if your database supports it - use `MERGE`. Methods like `last()` only work on scrollable result sets which are - usually - not the default. – Mark Rotteveel Jun 04 '15 at 11:22
  • ya thanks Sarit Adhikari, now my problem is solved. i am also using the same, that you are suggested. but what is the problem with last() function in ResultSet. –  Jun 04 '15 at 11:23

1 Answers1

-1
rs.last;
int rsCount=rs.getRow();//number of row

call rs.beforeFirst(); and then while'(rs.next()) {...} if you want to process every value in the ResultSet

Biratus
  • 23
  • 1
  • 1
  • 8