Possible Duplicate:
How to check if resultset has one row or more?
What will executeQuery(String sql)
return when the result of the SQL Query is zero rows>? If it returns a ResultSet
Object, how will we detect that the SQL Query has returned nothing.
Assume the SQL Query to be a SELECT
statement.
executeQuery : Returns one ResultSet object. executeUpdate : Returns an integer representing the number of rows affected by the SQL statement. Use this method if you are using INSERT , DELETE , or UPDATE SQL statements.
executeQuery. Executes the given SQL statement, which returns a single ResultSet object.
executeQuery. Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
executeUpdate() : This method is used for execution of DML statement(INSERT, UPDATE and DELETE) which is return int value, count of the affected rows. executeQuery() : This method is used to retrieve data from database using SELECT query.
Did you check ResultSet's next method . Initially the ResultSet's cursor points to before the first row, the very first call to next() returns false implies there was no data in the ResultSet. See How do I get the size of a ResultSet? , since there is no direct size() or length() method for Resultsets in Java.
The next() method of the resultSet moves the cursor to the next row and returns a boolean that indicates if a data has been read or not. Usually it's used with a while loop
while (myresultset.next()){
//some statement;
}
In your case the fist call of the next method will return false if no data matche the query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With