我正在使用sqlite进行登录,但为了比较我使用以下代码的登录和数据库字词:
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;
}
控制台说:
Conecçãopentruelecida!
deu1 word - word
操作成功完成
假
2个相同的单词显得不同,我没注意到什么?帮助请
答案 0 :(得分:2)
你的布尔逻辑是关闭的
(rs.next() || !pass.equalsIgnoreCase(password))
应该是
(rs.next() && !pass.equalsIgnoreCase(password))
带有||
的当有一行时,它不会评估下一个条件(短路)。请注意,您的else
也是似是而非 - 因为如果没有行,那么您似乎也将其设置为成功。