简体   繁体   中英

User login to database from application

This one has been bugging me for a little bit and I can't for the life of me figure it out:

I have to get my application to collect input from the user as a means of logging in to a database. My issue is, only the first user is actually allowed in according to the code. What am I doing wrong?

CODE

for(int i=0;i<numRows;i++)
        {
            String Usernames = rs.getString("Username");
            String Password = rs.getString("Password");
            String getAcc = rs.getString("UserType");
            rs.next();
            if(Usernames.trim().equals(UsernameIn)&&Password.trim().equals(PasswordIn))
            {
                start.writeToFile("ConToDatabase Class|Username: "+UsernameIn+" and Password: "+PasswordIn+" Accepted");
                if(getAcc.trim().equals("admin"))
                {
                    start.writeToFile("ConToDatabase Class|Authenticated user identified as \"admin\", Entering AdminUser Class|UTypeAdmin Method");
                    clear();
                    AdminUser.UTypeAdmin(args, UsernameIn, rs, dbTable(), path);
                }
                if(getAcc.trim().equals("standard"))
                {
                    start.writeToFile("ConToDatabase Class|Authenticated user identified as \"standard\", Entering StandardUser Class|UTypeStandard Method");
                    clear();
                    StandardUser.UTypeStandard(args, UsernameIn, rs, dbTable(),path);
                }
            }
            else
            {
                start.writeToFile("ConToDatabase Class|User invalid, reset application");
                System.out.println("Invalid User, Resetting...");
                Thread.sleep(2000);
                ConToDatabase.clearandreset(args);
            }
        }

I think that the problem is the code in your ELSE block.

You want to test all the users before say that the user is invalid.

With the first user you dont have that problem because you only arrived to the IF block code, but with all the other users you gonna pass by the ELSE block at least once.

You can do something like this:

boolean founded = false;
for(int i=0;i<numRows;i++)
{
    String Usernames = rs.getString("Username");
    String Password = rs.getString("Password");
    String getAcc = rs.getString("UserType");
    rs.next();

    if(Usernames.trim().equals(UsernameIn)&&Password.trim().equals(PasswordIn))
    {
        founded = true;
        start.writeToFile("ConToDatabase Class|Username: "+UsernameIn+" and Password: "+PasswordIn+" Accepted");

        if(getAcc.trim().equals("admin"))
        {
            start.writeToFile("ConToDatabase Class|Authenticated user identified as \"admin\", Entering AdminUser Class|UTypeAdmin Method");
            clear();
            AdminUser.UTypeAdmin(args, UsernameIn, rs, dbTable(), path);
        }

        if(getAcc.trim().equals("standard"))
        {
            start.writeToFile("ConToDatabase Class|Authenticated user identified as \"standard\", Entering StandardUser Class|UTypeStandard Method");
            clear();
            StandardUser.UTypeStandard(args, UsernameIn, rs, dbTable(),path);
        }
    }
}

if(!founded)
{ 
  start.writeToFile("ConToDatabase Class|User invalid, reset application");
  System.out.println("Invalid User, Resetting...");
  Thread.sleep(2000);
  ConToDatabase.clearandreset(args);
}

You want to pay attention at the comments in your answer too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM