这是我的服务

我尝试了各种方法,但是在比较false条件内的字符串时,它总是返回if,每次if(pwd==dbpw)也会返回false,就像下面的代码一样。

    public boolean authenticate(String email,String pwd){

        boolean result=false;
        String dbpw="";
        DBConnect myDB=new DBConnect();

        Connection con=myDB.getConnection();

        try {
            Statement mySt=con.createStatement();
            //ResultSet myRes=mySt.executeQuery("select pwd from admin where email=\'"+email+"\'");
            ResultSet myRes=mySt.executeQuery("select pwd from admin where email=\'sankasumadura@gmail.com\'");


            while(myRes.next()){
            dbpw=myRes.getString("pwd");
            }



            if(pwd.equals(dbpw)){
                result=true;
            }else{
                result=false;
            }



        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
                }
        finally{


                return result;
                }





    }


我尝试使用更多的输入,但始终输出为false,但是数据库检索正确的值,并且在if条件内进行比较时会发生该问题。
请帮我。

最佳答案

您可以检查您的可疑内容是否包含空格:

pwd=request.getParameter("pwd").toString().trim();


返回之前,您可以将值打印到控制台:

System.out.println("pwd:" + pwd + ",dbpw:" + dbpw + ",isequal:" + pwd.equals(dbpw));
if (pwd.equals(dbpw)) {
    result = true;
} else {
    result = false;
}

10-07 12:57
查看更多