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