#1
  1. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0

    Check to see if no rows returned


    I want to check to see if my result set is empty, without having an error come up.
    Currently i'm using try/catch to catch oracle errors, and I don't want to stop using it because i still want to know if there's any errors.

    This is how it'd be done in php:
    PHP Code:
    $sql "SELECT * from table";
    $query mysql_query($sql);
    if(
    mysql_num_rows($query) == 0)
    {
          echo 
    "Uh oh, no rows returned!";

  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Just make sure the ResultSet isn't null before you do something with it.

    if ( rs != null ) {
    -james
  4. #3
  5. I played Richard the Third
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Den Haag, Netherlands
    Posts
    122
    Rep Power
    12
    Empty or null?

    rs != null is usually to check nothing bad has happened.

    To see if your resultset is not empty you should use the iteration

    Code:
    try {
      ResultsSet rs = sqlstatement.executeQuery(); // something like that
    
      // to loop through the resultset 
      while(rs.next()) {
        String str_1 = rs.getString(1);
      }
    
      // or if your expecting only one return value
     
      if(rs.next()) {
        ....
      }
    
    } catch(SqlException sqlEx) {
      sqlEx.printStackTrace(System.out);
    }
    just remember rs.next() changes the resultsetpointer, rs != null does not influence it.
    Now is the winter of our discontent made glorious summer by this son of York.
  6. #4
  7. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Want i want to check is that the result set had no rows returned.
  8. #5
  9. I played Richard the Third
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Den Haag, Netherlands
    Posts
    122
    Rep Power
    12
    in that case:

    the rs.next()-method returns a boolean that is false when there's no more records to be read. If the first rs.next() return false, that would mean there aren't any at all.

    So
    Code:
    if(!rs.next()) {
     System.out.println("No Records");
    }
    Now is the winter of our discontent made glorious summer by this son of York.
  10. #6
  11. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    sweet, thanks.
  12. #7
  13. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Hrm... i've hit a snag.

    How can i test rs.next without upping the pointer?
    Code:
    		if(!rs.next())
    		{
    			out.println("<tr>\n <td><font size=\"2\" face=\"Arial\">No events found.</font></td>\n</tr>");
    		}
    		else
    		{
    			while(rs.next())
    			{
    				// stuff
    			}
    		}
    That's causing it to miss the 1st row in the result set.
  14. #8
  15. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Use a do/while maybe?
    Code:
    		if(!rs.next())
    		{
    			out.println("<tr>\n <td><font size=\"2\" face=\"Arial\">No events found.</font></td>\n</tr>");
    		}
    		else
    		{
    			do 
    			{
    				// stuff
    			} while(rs.next());
    		}
  16. #9
  17. I played Richard the Third
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Den Haag, Netherlands
    Posts
    122
    Rep Power
    12
    yep, good point
    Now is the winter of our discontent made glorious summer by this son of York.
  18. #10
  19. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Ahh, cool thx. I think that's the first time in my 4 years of programming i've actually found a use for one of those.
  20. #11
  21. I played Richard the Third
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Den Haag, Netherlands
    Posts
    122
    Rep Power
    12
    Lol, I used a switch-statement two months ago
    Now is the winter of our discontent made glorious summer by this son of York.

IMN logo majestic logo threadwatch logo seochat tools logo