gpt4 book ai didi

java - Else 语句不在 SQL 数据库的 while 循环内执行

转载 作者:行者123 更新时间:2023-12-01 22:23:01 25 4
gpt4 key购买 nike

Else 语句在 while 循环内不起作用

public void loginButton() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/finditdb","root","root");
Statement stmt = con.createStatement();
String usernameInput = usernameField.getText();
String passwordInput = passwordField.getText();
ResultSet rs = stmt.executeQuery("SELECT * FROM finditdb.login WHERE username like '" + usernameInput +"' and password like '" + passwordInput +"'");
while(rs.next()){
String usernameDb = rs.getString("username");
String passwordDb = rs.getString("password");
if(usernameInput.equals(usernameDb) && passwordInput.equals(passwordDb)){
System.out.println("Access Granted..");
}
else {
System.out.println("Access denied..");
}
}
}

最佳答案

You could try this and let me know if it works the way you want it to.

while(true){     
if(rs.next()){
String usernameDb = rs.getString("username");
String passwordDb = rs.getString("password");
if(usernameInput.equals(usernameDb) && passwordInput.equals(passwordDb)){
System.out.println("Access Granted..");
break;
}

}

else{
System.out.println("Access denied..");
break;
}
}

我们没有在 while 循环 括号内提到任何退出条件,因此循环将继续运行,直到没有更多记录可供提取(否则将打印 Access Denied 并中断)或者如果授予访问权限(再次中断)。

关于java - Else 语句不在 SQL 数据库的 while 循环内执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58576563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com