gpt4 book ai didi

mysql - 字符串的比较不会返回 true,即使它们是相同的

转载 作者:行者123 更新时间:2023-11-29 12:32:54 25 4
gpt4 key购买 nike

我目前正在创建一个 super 简单的登录系统,仅用于处理中的教育目的。

简而言之,我正在做的是将使用controllerP5库创建的2个文本字段的输入与我使用BezierSQlib库连接的MySQL数据库中的信息进行比较。所有代码都包含在名为 DbHandler 的类内的一个方法中,该类包含与数据库相关的所有代码。

但是最后的 if 语句(将数据库检索到的密码与用户输入的密码进行比较)将不起作用。即使两个字符串相同,它们也不会返回 true。我尝试输入“if('1234 == '1234')”,结果返回 true。

但是如果我这样做“if(_rPass == '1234')”,其中 _rPass 是数据库中的密码,并且密码设置为 1234,它仍然返回 false。

void loginCheck(String name, String password){ //This is the method that checks if the login information is correct
String _rPass; //where we store the retrieved password from the database

if(_msql.connect()){ //if there´s connection to the database
println("connection"); //we write out there´s a connection in the prompt

_msql.query("SELECT password FROM users WHERE username='" + name + "'"); //a query is done to find the matching password to the username

_msql.next(); //selects the next row in the retrieved table

if(_msql.getString(1) == null){ //if there´s no user with that username in the user table
println("wrong username"); //the username must be wrong
} //end if
else{ //if something is returned!

_rPass = _msql.getString(1); //the password from the database is stored in _rPass

if(_rPass == password) { //!!!THIS WILL NOT RETURN TRUE NO MATTER WHAT!!!
println("succesfull login!"); //if the strings are the same we´re logged in!
}

else{
println("wrong password");
}
}
}

}

以下代码是“DbHandler”类的一部分,该类控制与数据库相关的所有代码,并通过方法在另一个名为“interface”的类中调用。

上面的方法被激活的部分是这样的:

      if(btn_login.isPositionWithinButton(mouseX,mouseY)){ //checks if the login button has been clicked
DbHandler.loginCheck( cp5.get(Textfield.class,"username").getText(), cp5.get(Textfield.class,"password").getText() ); //the loginCheck method is called with the arguments being the text from the textFields
}

提前致谢!

最佳答案

使用字符串。 equal 方法而不是 ==。

As == 比较引用,因此如果引用不同,它将返回 false。

要比较字符串的值,请使用 equal 方法。

我。 e.如果 (_rpass.equals(密码)){}

关于mysql - 字符串的比较不会返回 true,即使它们是相同的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27214699/

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