1

I am new. Trying to do a database retrieve demo to login a system, here is my function code:

I will call goLogin function and pass in the input id and password for validation and I will also get all the id from Database for checking purpose. After ID is correct, only go check the password.

public void goLogin(String id, String pass){

String[99] allID = getAllIDFromDB();    

for(int i=0;i<allID.length;i++){

   if(allID[i]==id){

    String passwordDB = getPasswordFromDB(id);

    if(pass==password){
           System.out.println("Correct Password");
    }else{
           System.out.println("Wrong Password");
    }


   }

}

My peers say I was using too much if else and I can shorten the code and make the program better, and I faced some issue on looping for example when ID and Password are correct, the program will still continue the loop.

Is there any suggestion to make this function better?

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Seng Seng
  • 13
  • 2
  • 1
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Guy Mar 15 '18 at 11:56

3 Answers3

1

First of all, Why retrieve all the user IDs from the database instead make sql query to retrieve the row of this user based on this id.

something like this:

Select * from `users` where id = {id};

And if you want to stop looping a wrong password was found, add break in the else scope.

Adel
  • 23
  • 7
0

Yes, you could even do:

Select * from `users` where id = {id} and password = {password}

Also, to compare Strings in Java, you should use string1.equals(string2), not the == operator.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

In my opinion, the main issue of your program is your logic to implement the Login Function.

Login Function implementation can be implemented with various pattern, Based on your program code I will assume you just want a Most Basic Login Function that allow the program to do validation on user input ID and Password.

Actually, this Basic validation can be done in your Database Query. You can just take the user input ID and Password and let Database Query to do filtering and determine if the user input ID and Password is valid or invalid.

First use this Query:

Select DATABASEID From Database Where DATABASEID=inputID and DATABASEPASSWORD=inputPassword;

Java Code:

public void goLogin(String id, String pass){

// Since i changed the Query, you need to pass in the ID and Password to let the Query to filtering

String DatabaseID = getIDFromDB(id, pass);    

   // Simple Logic, If DatabaseID have value which mean the ID and Password is correct
   // Because the Database Query will return null if the ID and Password is Wrong

   if(DatabaseID!=null){
     System.out.println("ID and Password is Correct.");   
   }else{
     System.out.println("ID or Password is Incorrect.");
   }

}

This Logic is very simple, but also come with some drawback, the only Advantage Compare to your code are:

  1. Still able to do basic validation on User Input ID and Password.
  2. Remove for loop and other unnecessary if else.
  3. Only access database for once.

Hope this would help.

I am a Student
  • 1,570
  • 4
  • 21
  • 36