K0NST-hacker
K0NST-hacker

Reputation: 1

SQL statement in Java DAO method is not checking if user exists properly

I have DAO method in Java which looks like this:

private boolean validateUser(String email, String username) throws SQLException {
    return stmt.execute(
        "SELECT NOT EXISTS" +
        "(SELECT id from Math_Hub.Users_Information " +
        "WHERE username = '" + username + "' OR email = '" + email + "')");
}

The method returns true even if username already exists in database. Why is that?

I tried to test it by hand and the following SQL statement

SELECT NOT EXISTS
(SELECT id from Math_Hub.Users_Information
WHERE username = 'Eren' OR email = '[email protected]')

This worked perfectly.

Upvotes: 0

Views: 316

Answers (1)

Chetna R
Chetna R

Reputation: 21

NOT EXISTS always return 1 if no row matches in the where clauses. Either use EXISTS or you can go with select query and later check if anything is received in the resultset( select * or select count(*)).

Upvotes: 0

Related Questions