The next()
moves the cursor froward one row from its current position in the resultset
. so its evident that if(rs.next())
means that if the next row is not null
(means if it exist), Go Ahead.
Now w.r.t your problem,
ResultSet rs = stmt.executeQuery(sql); //This is wrong
^
note that executeQuery(String) is used in case you use a sql-query as string.
Whereas when you use a PreparedStatement, use executeQuery() which executes the SQL query in this PreparedStatement
object and returns the ResultSet
object generated by the query.
Solution :
Use : ResultSet rs = stmt.executeQuery();