0

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

enter image description here

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

  }
8
  • 5
    Dude a cast here or there maybe, but you are Count Castula. Break that habit... Commented Feb 3, 2013 at 15:07
  • i tried it but result is same Commented Feb 3, 2013 at 15:08
  • It's not going to change the results. Why are the lists not parameterized? Commented Feb 3, 2013 at 15:08
  • 2
    @Rob +1 for "Count Castula" I laughed out loud at that (valid) technical point. Commented Feb 3, 2013 at 15:11
  • 2
    Try surrounding your printouts with quotes, to make sure there are no trailing spaces for example: System.out.println("\"" + Username + "\"") (terrible name for a variable, BTW). You can also print lengths, in case there are unprintable characters. Commented Feb 3, 2013 at 15:14

2 Answers 2

1

I rewrote your code as:

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

And now it works. Note the <String> after the List declaration. That's called generics and says that only Strings can 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:

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

It's still not perfect, because the search() method should return an instance of type UserCredentials or an instance of List<UserCredentials> based on what it should exactly do. UserCredentials would then look like this:

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

}

With this class implemented, your code would look like this (assuming search() should return only one result):

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

Please ask any additional questions if you have any.

Sign up to request clarification or add additional context in comments.

4 Comments

@RaviKumarMistry Try to tackle you new problem for a while and then start a new question with it if you're unsuccessful.
compiler is showing warning for uncheacked or unsafe operation
@RaviKumarMistry On which line, where? I bet the problem lies in the declaration of the search() method. Change its return type to List<String> and the surrounding code to support that. If you're unable to do that, please edit your question and add your search() method.
@RaviKumarMistry Exactly what I said. Change to public ArrayList search(String Username, String Password) {} to public List<String> search(String Username, String Password) {}.
0

Define myList as type ArrayList<String>

5 Comments

@RaviKumarMistry When you add <String> after you are saying that only objects of type String may be placed in the list. Using this will mean you dont have to use casts everywhere.
And you don't have to worry about targeting all your equals comparisons.
What does type does your search method return? May I suggest you take a look at the java generics tutorial?
@RaviKumarMistry find connect.search() method, and change its return type to ArrayList<String>
Username is a String. A get call on a raw type list is returning an Object. So you compate an Object and a String, that will not be equal. Setting the type of the list as PoiXen says will help, alternative stor the value from in a temporary variable of type String. String tmpUser = myList.get(0).toString(); tmpUser.equals(Username);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.