0

我正在制作一个系统,用于验证数据库中已经定义的凭据与在 jTextfield 中输入的凭据。当我在 if-else 语句中输入条件时,它说不能字符串不能转换为布尔值。请帮忙。

 Connection con = (Connection) DriverManager.getConnection (host, uName, uPass);
        Statement stmt = (Statement) con.createStatement ();
        //login is a table in my database,password a tuple
        String SQL = "SELECT password FROM login;";
        ResultSet password = stmt.executeQuery (SQL);

        String pass = adminpassword.getText();
        String sqlpass = password.getString("password");
//error shows on if else below
        if(sqlpass = pass){
            JOptionPane.showMessageDialog(null,"You have entered an incorrect password,please try again.","Incorrect Password",JOptionPane.WARNING_MESSAGE);
        }
        else{
            String passconfirm = "SELECT admin_id WHERE password = 77353";
            ResultSet confirmpass = stmt.executeQuery(passconfirm);
            JOptionPane.showMessageDialog(null,"Welcome","Welcome " + confirmpass,JOptionPane.INFORMATION_MESSAGE);
            Countyinfo proceed = new Countyinfo();
            proceed.setVisible(true);
        }
        }
        catch(SQLException err){
         System.out.println (err.getMessage());
        };
4

2 回答 2

1

相等的布尔运算符是==, not =(它是赋值运算符)。赋值操作将始终评估分配给其左值的值。

这就是为什么sqlpass = pass评估为String. sqlpass == pass将评估为boolean.

编辑:不过,比较Strings 的标准方法是使用如下.equals方法:

if(sqlpass.equals(pass))

于 2013-11-08T23:57:19.480 回答
1

您的if语句表达式中有一个赋值,它返回 aString而不是 required boolean

if (sqlpass = pass) {

应该

if (sqlpass.equals(pass)) {

比较Strings==操作员比较Object参考而不是内容)

于 2013-11-08T23:57:35.970 回答