简体   繁体   中英

Two identical words are different

I am using sqlite to do a login, however to compare the login and database words I used the following code:

public static boolean result(String nome, String pass) {
Connection c = null;
Statement stmt = null;
boolean bo = false;
try {
    Class.forName("org.sqlite.JDBC");
    c = DriverManager
            .getConnection("jdbc:sqlite:C:/Users/M4UR0/Desktop/Pastas/Programação/Java/Albamoura/data.db");
    c.setAutoCommit(false);

    stmt = c.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM COMPANY where NAME = '" + nome + "';"); //

    String password = rs.getString("PASSWORD");
    if (rs.next() || !pass.equalsIgnoreCase(password)) {
        bo = false;
        System.out.println("deu1 " + password + " - " + pass);
    } else {
        System.out.println("Entrou!");
        bo = true;
    }

    rs.close();
    stmt.close();
    c.close();
} catch (Exception e) {
    System.err.println(e.getClass().getName() + ": " + e.getMessage());
    System.exit(0);
    bo = false;
}
System.out.println("Operation done successfully");
return bo;

}

And console say:

Conecção estabelecida!

deu1 word - word

Operation done successfully

false

2 identical words appear as different, did not I notice something? Help pls

Your boolean logic is off here

(rs.next() || !pass.equalsIgnoreCase(password))

that should be

(rs.next() && !pass.equalsIgnoreCase(password))

with an || it doesn't evaluate the next condition (short-circuits) when you have a row. Note that your else is also specious - because if there is no row then you appear to also set it to success.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM