SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to find out if a ResultSet is empty?

    How to find out if a ResultSet is empty?

    Code Java:
    String sql = "SELECT * FROM MEMBERS WHERE NAME='John'";
    ResultSet rs = statement.executeQuery(sql);

  2. #2
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    List businessObjectList = new ArrayList();
    
    String sql = "SELECT * FROM MEMBERS WHERE NAME='John'";
    ResultSet rs = statement.executeQuery(sql);
    
    while(rs.next) {
    
      BusinessObject businessObject = new BusinessObject();
      businessObject.setName(rs.getString("name));
    
    }
    
    if(businessObjectList.size() == 0) {
    
      throw new Exception("YO, The ResultSet was EMPTY.  CALL IT SUPPORT ASAP");
    }
    Something like that might work.

    BusinessObject is just a place holder in this example. You would have to create your own domain or model object to represent what was coming back from the database.

    Best of Luck.
    baby steps... baby steps...

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a long thread about the topic on sun forums

  4. #4
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by avci84 View Post
    There is a long thread about the topic on sun forums
    That thread does have quite a bit of discussion in it (over several years too), but it also contains quite a bit of poor programming examples.

    Touching and moving the cursor all over the place is typically a bad idea.

    The current "best practice" as far as I understand it for checking to see if a result set is null or not is to work with the result set like its not empty. Iterate through the result set using the .next() method and then checking your results after that. IF you didn't produce anything after iterating through the result set. The result set was empty.

    This was more elegantly stated in the posts in the above thread by Dr. Clap (twice) and duffymo.

    Best of Luck.
    baby steps... baby steps...

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,399
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just FYI, you can avoid using sql if u use hibernate/jpa/ejb3.0 entity bean.

  6. #6
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check
    if(rs.next())
    {
    //resultset not null
    }
    else
    {
    //Resultset is null means empty
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •