为什么等于功能不起作用?

时间:2013-02-03 15:02:06

标签: java equals

当我编译下面的代码时,它显示(字符串)用户名和myList.get(0)等于但是等于函数返回false为什么密码也会发生同样的情况。

btnLogIn.addActionListener(new ActionListener()
 {
   public void actionPerformed(ActionEvent e) {

    String Username=(String) textField.getText();   //fatch the user name from text field
    String Password=(String) textField_1.getText();  //fatch password frof text field
    databaseconnection connect = new databaseconnection(); // databaseconnection class object to connect to data base
    ArrayList myList = connect.search(Username,Password);  //serch the username and password in data base
            System.out.println((String)myList.get(0));  //for testing 
            System.out.println((String)myList.get(1));  //for testing
            System.out.println(Username);               //for testing
            System.out.println(Password);               //for testing
            System.out.println(Username.equals(myList.get(0)));  //for testing 
            System.out.println(Password.equals((String)myList.get(1)));   //for testing
            if(Username.equals(myList.get(0))&&Password.equals((String)myList.get(1))){
            System.out.println("Hello"+Username);
            }
        }
    });

enter image description here

这是我的数据库连接类

   import java.sql.*;
   import java.util.ArrayList; 

 public class databaseconnection{
Statement stmt  ;
ResultSet rs ;
Connection conn;
ArrayList<String> temp = new ArrayList<String>();

public void getconnection(){

try{


    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    Connection conn = DriverManager.getConnection("jdbc:odbc:Database1","","");

    stmt = conn.createStatement();


}
catch(Exception e){
    System.out.println("connection error");
}
}
public ArrayList  search(String Username,String Password){

    getconnection();
    try{
    rs = stmt.executeQuery("select username,password from login where username = \'"+Username+"\'");
    if(rs.next()){
    String tempString=rs.getString("username");
    temp.add(tempString);
    tempString= rs.getString("password");
    temp.add(tempString);
    }
    }
    catch(Exception e){
    System.out.println("search error");
    }
    return temp;
  }

  }

2 个答案:

答案 0 :(得分:1)

我把你的代码重写为:

String Username = "user";
String Password = "pass";

List<String> myList = new ArrayList<>(); 
myList.add("user");
myList.add("pass");

System.out.println(Username);
System.out.println(Password);
System.out.println(myList.get(0));
System.out.println(myList.get(1));
System.out.println(Username.equals(myList.get(0)));
System.out.println(Password.equals(myList.get(1)));

if (Username.equals(myList.get(0)) && Password.equals(myList.get(1))) {
    System.out.println("Hello, " + Username);
}

现在它有效。请注意List声明后的<String>。这称为generics,并表示只能将Strings放入列表中。它还消除了任何容易出错的显式转换需求,并且只有在您确定可以安全地执行时才应该这样做。

如果您根据此更改了代码但仍未获得正确的结果,请确保连接中的值正确且不包含任何类型的不可见字符。


此外,在Java中,变量名通常在lowerCamelCase中。班级名称应在UpperCamelCase中。请参阅Java naming conventions

Don't forget to close your connections! Java 7 automatic resource management可以帮助您。在一般情况下,您还应该处理任何连接错误。

根据他们的工作来命名变量是一个好主意。因此,您的代码看起来应该更像这样:

public void actionPerformed(ActionEvent e) {
    String username = "user";
    String password = "pass";

    List<String> dtbSearchResults;
    try (DatabaseConnection connection = new DatabaseConnection()) {
        dtbSearchResults = connection.search(username, password); 
    } catch (SomeExceptionYouReallyShouldHandle e) {
        // seriously, handle it here
    }

    System.out.println(username);
    System.out.println(password);
    System.out.println(dtbSearchResults.get(0));
    System.out.println(dtbSearchResults.get(1));
    System.out.println(username.equals(dtbSearchResults.get(0)));
    System.out.println(password.equals(dtbSearchResults.get(1)));

    if (username.equals(dtbSearchResults.get(0)) && password.equals(dtbSearchResults.get(1))) {
        System.out.println("Hello, " + username);
    }
}

它仍然不完美,因为search()方法应该返回类型UserCredentials的实例或List<UserCredentials>的实例,基于它应该做什么。 UserCredentials将如下所示:

public class UserCredentials {

    private final String username;
    private final String password;

    public UserCredentials(String username, String password) {
        // maybe some validity checks
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

}

实现此类后,您的代码将如下所示(假设search()只返回一个结果):

public void actionPerformed(ActionEvent e) {
    String username = "user";
    String password = "pass";

    UserCredentials user;
    try (DatabaseConnection connection = new DatabaseConnection()) {
        user = connection.search(username, password); 
    } catch (SomeExceptionYouReallyShouldHandle e) {
        // seriously, handle it here
    }

    System.out.println(username);
    System.out.println(password);
    System.out.println(user.getUsername());
    System.out.println(user.getPassword());
    System.out.println(username.equals(user.getUsername()));
    System.out.println(password.equals(user.getPassword()));

    if (username.equals(user.getUsername())
            && password.equals(user.getPassword())) {
        System.out.println("Hello, " + username);
    }
}

如果您有任何其他问题,请提出其他问题。

答案 1 :(得分:0)

将myList定义为类型ArrayList<String>