when i compile the code below it shows that the (string) username and myList.get(0) are equal to but the equals function returning false why same also happen for 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);
}
}
});

this is my databaseconnection class
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;
}
}
I rewrote your code as:
And now it works. Note the
<String>after the List declaration. That’s called generics and says that onlyStringscan be put into the list. It also removes any need of explicit casting which is error-prone and should be done only in cases when you are sure that you can do it safely.If you changed your code according to this and still didn’t get the right results, make sure the values from your connection are right and don’t contain any sort of invisible characters.
Also, in Java, variable names are usually in lowerCamelCase. Class names should be in UpperCamelCase. See Java naming conventions.
Don’t forget to close your connections! Java 7 automatic resource management helps you with that. In a general case, you also should handle any connection errors.
And it’s a good idea to name your variables by what they do. Therefore, your code should look more like this:
It’s still not perfect, because the
search()method should return an instance of typeUserCredentialsor an instance ofList<UserCredentials>based on what it should exactly do.UserCredentialswould then look like this:With this class implemented, your code would look like this (assuming
search()should return only one result):Please ask any additional questions if you have any.