I’m not sure why, but for some reason, the following code skips the else condition. I’ve tried just about everything I can think of, including switching the code blocks, but it still skips the else part. Basically, I want this method to return String temp = "no" if String docID that is passed to the method is not found in the FILESTATUS database, and String temp = "yes" if it is found.
static String checkDocID(String docID)
{
String temp = null;
System.out.println("Checking if data already exists in database...");
try
{
Main.stmt = Main.con.createStatement();
String command = "SELECT * FROM FILESTATUS WHERE ID='" + docID + "'";
ResultSet queryResult = Main.stmt.executeQuery(command);
if (!queryResult.next())
{
temp = "no";
}
else
{
while (queryResult.next())
{
String result = queryResult.getString("ID");
if (result.equals(docID))
{
temp = "yes";
break;
}
}
}
Main.stmt.close();
}
catch (Exception ex) {ex.printStackTrace();}
return temp;
}
Thanks!
Because you end up calling queryResult.next() in the “if” and again in the while, you’re skipping the first result. If there is only one result, the while loop will never execute.
If I can make a couple of suggestions:
result.equals(docID)since the query already assured that.