The most likely reason you don't enter the while loop is because the condition for the while is false the first time it is tested. In your case, you have:
Since you don't get into the while loop, this suggests that your query gives you zero results. Similarly you say the loop does get entered into when you change the query, indicating that with the new query you do get results, and so the while loop gets executed.
How do you fix it? Query for for data you know exists (even if it means entering fake data) and see if it works. If is still doesn't work then check your final query
string with parameters included to see what it is sending the database. Then pull out a DB Querying tool and send the same string. If you get the same results modify the String in the DB Querying tool until you get the desired results, and reformat the query string in
Java to do the same thing. If you get different results in the DB Querying tool then check out formatting to see what it handles differently than your Java code and make your Java code mimic the tool's output.