0

当我编译下面的代码时,它显示(字符串)用户名和 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);
            }
        }
    });

在此处输入图像描述

这是我的数据库连接类

   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;
  }

  }
4

2 回答 2

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);
}

现在它可以工作了。注意<String>List 声明之后的内容。这称为泛型,并表示只能Strings放入列表中。它还消除了任何容易出错的显式转换的需要,并且只有在您确定可以安全地执行它的情况下才应该这样做。

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


此外,在 Java 中,变量名通常采用lowerCamelCase 格式。类名应该是UpperCamelCase。请参阅Java 命名约定

不要忘记关闭您的连接! Java 7 自动资源管理可以帮助您。在一般情况下,您还应该处理任何连接错误。

通过变量的作用来命名变量是个好主意。因此,您的代码应该看起来更像这样:

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);
    }
}

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

于 2013-02-03T15:42:04.770 回答
0

将 myList 定义为类型ArrayList<String>

于 2013-02-03T15:05:39.067 回答