以下のコードをコンパイルすると、(string) username と myList.get(0) が等しいことが示されますが、 equals 関数が false を返すのは、 password に対しても同じことが起こる理由です。
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;
}
}