TookTheRook
TookTheRook

Reputation: 827

Check if word exists in within a database table in Java

So I have a database, within which there is a table with two columns: rightWord and wrongWord. I wish to select the rightWord value when the wrongWord value is equal to a variable declared in Java. I have the following code so far:

SELECT rightWord FROM dictionaryTable WHERE wrongWord = (?)

existingKeywords.next();
String rightWord = existsingKeywords.getString("rightWord");

That works and I am able to get the first value in rightWord column when wrongWord equals the Java variable. Now, how can I check if the any of the values in the wrongWord column does not match the Java variable? I have the above code in a for loop, and the value of the java variable is different every time.

Please help. Thanks!

Upvotes: 0

Views: 898

Answers (1)

Martin Algesten
Martin Algesten

Reputation: 13620

Assuming existingKeywords is your ResultSet. The next() function will return false when there is no value.

Typical usage:

 if (existingKeywords.next()) {
    String rightWord = existingKeywords.getString("rightWord");
 } else {
    // no right word...
 }

Upvotes: 1

Related Questions