Reputation: 1093
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;
}
}
Upvotes: 0
Views: 1030
Reputation: 38414
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.
Upvotes: 1