5
ResultSet zoeken = stat.executeQuery("SELECT * FROM leden WHERE naam = '" + text + "'");
if (zoeken.getRow() == 0){
   System.out.println("hi");
}
while( zoeken.next() ){
   System.out.println(zoeken.getString(1) + zoeken.getString(2) + zoeken.getString(3));
}

I am using this to check if the result zoeken is empty, but it throws an exception saying its illegal to do that regardless if it works or not.

What is a better solution to check if result is empty or not

6 Answers 6

11

// I am using this to check if the result"zoeken" is empty, but it throws an exception saying its illegal to do that. regardless if it works or not

Don't invoke ResultSet.getRow() before you invoke ResultSet.next().

From API:

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

       ResultSet zoeken = stat.executeQuery("SELECT * FROM leden WHERE naam = '" + text + "'");
       boolean val = zoeken.next(); //next() returns false if there are no-rows retrieved 
        if(val==false){
            System.out.println("zoken is empty"); //prints this message if your resultset is empty
         }
        while(val){// only runs when there are rows in the resultset
           System.out.println(zoeken.getString(1) + zoeken.getString(2) + zoeken.getString(3));
          val=zoeken.next(); //updating val again to check if there are rows in result set
        }

If your result set is empty zoeken.next() will return false in which case your while loop will not execute.

An Advice

use PreparedStatement Instead of simple Statement. simple statement would lead to SQL Injection and also would ease the pain for writing complex queries.Below is the sample code which use PreparedStatement.

String yourquery ="Select whatever From table where col1= ? and col2 =?"
PreparedStatement stmnt = Conn.preparedStatement(yourquery);
stmnt.setString(1, "val1");
stmnt.setString(2, "val2");
Sign up to request clarification or add additional context in comments.

6 Comments

I just read the API and it says getRow() should return 0 if there is no "current row". I didn't check but getRow() before a next() should return 0?
@alexvetter FROM API: the current row number; 0 if there is no current row and you only get rows when you invoke next()
yes but i need to display a message if there are no results, and i cant do that after the loop because it will display the message two times
val=zoeken.next(); // this confuses me? you declare val again...why?
@user1880803 please understand the code, refer to the comments and also check the API. its very useful :)
|
2

Use next() method

boolean hasNext = zoeken.next();

2 Comments

why cant i loop trough the rows if i use next()
because you are executing zoeken.getRow() before next() where cursor is not set to first row
2

Following code will work for you

if(!zoeken.first() && !zoeken.next())//if fist and next records are empty
{
System.out.println("Result set is empty");
}
else
{
System.out.println("Result set is not empty");
}

1 Comment

Welcome to SO! Code-only answers usually aren't helpful in the learning process. Please elaborate as to why this answer should solve the problem no matter how obvious it may be to you or others.
0

According to next() you must first use next and then getRow()

ResultSet zoeken = stat.executeQuery("SELECT * FROM leden WHERE naam = '" + text + "'");
while( zoeken.next() ){
    int rowNum = zoeken.getRow();
    System.out.println(zoeken.getString(1) + zoeken.getString(2) + zoeken.getString(3));
}

Comments

0

without using Result Set next() you cant check the size or resultSet period.

A ResultSet cursor is initially positioned before the first row.

Comments

0

I know the OP probably doesn't need the info any more but if someone stumbles across this, I would agree with everything @PermGenError has said although, I feel a more elegant solution for the given code but using prepared statements would be:

PreparedStatement ps = conn.prepareStatement("SELECT * FROM leden WHERE naam = ?");
ps.setString(title);
ResultSet rs = ps.executeQuery();

if (rs.next()) {
    do {
        System.out.println(zoeken.getString(1) + zoeken.getString(2) + zoeken.getString(3));
    } while (rs.next());
} else {
    System.out.println("hi");
}

I have written this freehand so excuse any mistakes or if it doesn't compile. I just feel this is clearer and cuts down on the usage of local variables. If there is at least one row, go print the row and keep doing this until there are no more rows otherwise say "hi". But I assume you would want to say something more useful than "hi" maybe a "No results found" message or something.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.