Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:50:17+00:00 2026-06-18T05:50:17+00:00

when i compile the code below it shows that the (string) username and myList.get(0)

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

  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:50:18+00:00Added an answer on June 18, 2026 at 5:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't get the code below to compile (see errors). Advice on correction would
The code below compiled in Debug configuration in VS2005 SP1 shows two messages with
How can this code compile? The code below in the operator int CAN access
I’m getting system error when I try to compile the code below on Visual
My code below won't compile. What am i doing wrong? I'm basically trying to
I try to compile code, that beggins with: #include<stdlib.h> #include<GL/gl.h> #include<glaux.h> with command: cc
Assuming that: The C# source code below is compiled under .NET 2.0 (CLR 2.0);
I have code as shown below for detecting if a string has any matches
The sample code below shows what I am trying to do: template<int NX, int
When I try to compile my VB.NET web project, I get an error that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.