user1879242
user1879242

Reputation: 13

Java SQL only show right record

For a school exercise I need to get some information from a database.

We need to scan a RFID card and lookup in the database. But when I try to print out the line in the IDE I get more results than I want.

The RFID tag something like: 2R KL MZ 89 and if I try to get it from the database I get: Access Granted. (because that is the record that is right) and Access Denied. (because that is the record which is wrong.

Long story short: How do I get ONLY Access Granted. when the tag is correct and ONLY Access Denied.

Code:

while (rs.next()) {

        String number = rs.getString("number");

        if (number.equals(key)) {
            System.out.println("Access Granted.");
        } else {
            System.out.println("Access Denied");
        }
    }

Table:

My table is only id, which is auto increment, and the other row is called number which contains two records: the right tag and the wrong one.

Upvotes: 1

Views: 117

Answers (1)

Jean-Philippe Briend
Jean-Philippe Briend

Reputation: 3515

You should use a query like :

select 1 from myTable where RF_ID = key

Using this, you will have a line if the key is present, no line else.

If you just have to do a lookup for a particular value in the table, looping over multiple lines is useless.

Upvotes: 1

Related Questions