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
Don’t invoke ResultSet.getRow() before you invoke ResultSet.next().
From API:
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.