Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ResultSet

 
Lakshmi Devi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which method should i use to find the End of ResultSet while looping through it in JDBC 1.0?
 
Bill Ross
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ResultSet.next() method returns a boolean true or false indicating whether it is at the end of the result set or not. Therefore, you can just use a while loop to check your result set. Example:
<PRE>
Assumes a valid Connection (con) object instance...
try{
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
//get some results
int iOne = rs.getInt(1);
int iTwo = rs.getInt(2);
storeOrUseYourResultsSomeWhere(iOne, iTwo);
}
}catch(SQLException sqle){
System.out.println("SQLException: " + sqle);
finally{
try{
if(stmt != null)
stmt.close();
if (con != null)
con.close;
}catch(SQLException sqle){
System.out.println("SQLException closing stmt and con: " + sqle);
}
}
</PRE>
Real code would do better logging than using System.out.println but it is good for initial trouble shooting. Sun Java Tutorial has a good beginner to middlin' tutorial on JDBC including a bunch of sample code similar to this. It should get you well on your way.
BillR
 
Lakshmi Devi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it worked.
Thank you very much Bill Ross.
Lakshmi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic