1

I am working on a project for school which requires a very basic log in function (nothing too fancy, seeing as how the passwords will remain in plain text). I have a test database and have thoroughly tested that all values that are being referenced and pulled from the database can also be displayed properly. You will see in the code that the type for each variable being compared is of the String type. My question to you is, why aren't the variables userOut and un equal? (I have even tried using the .toString() method on each of them individually, and well as together to try and get them "equal").

    public static void main(String[] args) throws SQLException {

    try {

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "Hsiri0758");
        System.out.println("Database connected...");

        Statement stat = conn.createStatement();
        Statement pwstat = conn.createStatement();

        Scanner scan = new Scanner(System.in);
        System.out.println("Username: ");
        String un = scan.nextLine();
        System.out.println("Password: ");
        String pw = scan.nextLine();

        ResultSet unRset = stat.executeQuery("SELECT username FROM accounts WHERE username = \"" + un + "\";");
        unRset.next();
        String userOut = unRset.getString(1);

        ResultSet pwRset = pwstat.executeQuery("SELECT password FROM accounts WHERE password = \"" + pw + "\";");
        pwRset.next();
        String pwordOut = pwRset.getString(1);

        if (userOut == un) {
            System.out.println("Welcome " + un + "!");
        }
        else {
            System.out.println("Invalid username.");
        }

        conn.close();
        }
        catch (SQLException ex) {
        ex.printStackTrace();
        }

    }
4

7 回答 7

2

除了像1你必须将值与equals()java中的方法进行比较这样的原语之外。

因此,在您的代码中,这意味着:userOut.equals(un).

最后你会得到:

if (userOut.equals(un)) {
    System.out.println("Welcome " + un + "!");
}

如果您有机会,您应该将已知String的输入与输入进行比较,因为您可以避免NullPointerExceptions。

一个常见的问题:

public void someMethod(String someParameter) {
    if(someParameter.equals(MY_CONSTANT)) {
        // ...
    }
}

这应该重构为:

public void someMethod(String someParameter) {
    if(MY_CONSTANT.equals(someParameter)) {
        // ...
    }
}
于 2013-11-12T15:47:20.780 回答
2

将此行替换 if (userOut == un) {if (userOut.equals( un)){

请参阅以获取更多信息==.equals()

于 2013-11-12T15:46:28.360 回答
0

这是因为 String 是一个类,而不是数据类型。您实际上只是在比较两个类引用,而不是实际的字符串。您必须使用类设计器提供的 equal() 来比较两个 String 实例的内容:

userOut.equals(un)
于 2013-11-12T15:55:21.077 回答
0

要比较 java 中的对象,请使用 .equals() 方法而不是“==”运算符

替换以下代码

 if (userOut == un) 

 if (userOut.equals(un)) 

如果您想忽略大小写,请使用如下所示

 if (userOut.equalsIgnoreCase(un)) 
于 2013-11-12T15:47:54.387 回答
0

在 Java 中使用==运算符时,您只是查看引用变量是否引用相同的字符串。因此,在您的情况下,这将始终评估为错误。

要简单地比较字符串,请尝试userOut.equals(un). 这将检查字符串的实际内容以查看它们是否相等。如果是,它将返回 true。

欲了解更多信息,请查看:
http ://www.thejavageek.com/2013/07/27/string-comparison-with-equals-and-assignment-operator/

于 2013-11-12T15:51:07.773 回答
0

如果您正在比较字符串,请尝试使用equals而不是==

您还可以使用equalsIgnoreCase来避免case sensitive问题

于 2013-11-12T15:47:08.410 回答
0

== 用于客户端脚本,如 javascript 或 jquery。服务器端需要equals或equalsIgnoreCase。

于 2013-11-12T15:52:31.280 回答