D
D
Dred Wolf2021-03-11 16:38:33
Java
Dred Wolf, 2021-03-11 16:38:33

How to compare a string received from a database with another string in Java?

//в queryResult хранится объект resultSet
    queryResult.next();
// НЕОБХОДИМО: сравнить введенный пользователем login с логином в БД

            if (login.equals(queryResult.getString("login"))){
                System.out.println("ЛОГИН ПРАВИЛЬНЫЙ !");
                if(password.equals(queryResult.getString("password"))){
                    System.out.println("ЛОГИН И ПАРОЛЬ ВЕРНЫ !");
                    return true;
                }
                else{
                    System.out.println("НЕВЕРНЫЙ ПАРОЛЬ !");
                    return false;
                }
            }
            else{
                System.out.println("НЕВЕРНЫЙ ЛОГИН");
                return false;
            }

The error is that when entering the correct login, it still returns "INCORRECT LOGIN"
for example:
variable String login = "test"
queryResult.getString("login") - a query to the login column returns a String, "test"


login is compared with queryResult .getString("login") via login.equals and returns boolean, however somehow it returns false anyway

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-03-11
Hasanly @azerphoenix

Strings in Java are compared using equals(). Another thing is that the comparison returns false.
Exclude the following points:
- the entry in the database can be in uppercase or lowercase or camelCase, etc. Therefore, false is returned
- the source string or the string from the database may contain Cyrillic characters
- the encoding of the stored information in the database differs from utf8
if you can’t solve the issue, then drop the link to the git

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question