Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does executeQuery(String sql) return when there are no results? [duplicate]

Tags:

java

sql

jdbc

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.

like image 429
Devashish Dixit Avatar asked Jun 13 '12 12:06

Devashish Dixit


People also ask

What type of value is returned by the executeQuery ()?

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.

What is the return type of executeQuery () method void?

executeQuery. Executes the given SQL statement, which returns a single ResultSet object.

What does PreparedStatement executeQuery return?

executeQuery. Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.

What is the return type of execute () executeQuery () and executeUpdate ()?

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.


2 Answers

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.

like image 88
Sandeep Pathak Avatar answered Sep 19 '22 01:09

Sandeep Pathak


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.

like image 26
jocelyn Avatar answered Sep 18 '22 01:09

jocelyn