We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,644 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Need help to match user credentials from database

Hi every one!!
I need help to match user data from database inorder to log in to admin panel. Here is what I am trying. it works ok but it is giving dialog box containing text "Sorry! You have no access to Database"
here is the code. Please help

 String user = jTextField1.getText();
            String pwd = new String(jPasswordField1.getPassword());
            while (rs.next()) {
                String uname = rs.getString("User name");
//Username is the coloumn name in the database table 
                String password = rs.getString("Password");
                if ((user.equals(uname)) && (pwd.equals(password))) {
                    admin_panel p = new admin_panel();
                    p.setVisible(true);
                } else if ((!user.equals(uname)) && (!pwd.equals(password))) {                  // System.out.println("Sorry! You have no access to Database");
                    JOptionPane.showMessageDialog(null, "Sorry! You have no access to Database");
                }
                //new login().setVisible(true);
            };

I need to use vriables in while loop, outside while loop. please guide how to do this

3
Contributors
5
Replies
15 Hours
Discussion Span
9 Months Ago
Last Updated
7
Views
Question
Answered
FaisalSarfraz
Newbie Poster
17 posts since May 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1

It looks like the problem is that you're testing every single user name in the table, and every one that is wrong will result in the "no access" dialog showing.

The naive fix would be to hold off showing the dialog until you've checked all the users and then only show it if you didn't match the user name and password anywhere.

A much better solution is to first select the row where the user name matches--there should only be one, right? Then see if the password matches the one in the database for that user. If you can find the user name and the password matches, you've authenticated someone; otherwise (i.e., can't find user name or password doesn't match), then deny access.

Really there's no need to loop over the entire table (I'm assuming that's what rs represents). If this is an assignment, then it's a really horrible example of how not to work with a database.

gusano79
Master Poster
710 posts since May 2004
Reputation Points: 193
Solved Threads: 111
Skill Endorsements: 6

Really there's no need to loop over the entire table (I'm assuming that's what rs represents). If this is an assignment, then it's a really horrible example of how not to work with a database.

I am totally new in java programming. so please mention the correct code that I have to replace with this.
I shall be thankful to you

FaisalSarfraz
Newbie Poster
17 posts since May 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1

Try changing this code:

else if ((!user.equals(uname)) && (!pwd.equals(password)))

to this:

else if ((user.equals(uname)) && (!pwd.equals(password)))
rubberman
Posting Maven
2,704 posts since Mar 2010
Reputation Points: 378
Solved Threads: 316
Skill Endorsements: 53

Try changing this code:

else if ((!user.equals(uname)) && (!pwd.equals(password)))
to this:

else if ((user.equals(uname)) && (!pwd.equals(password)))

I have tried this but this one do not show error when username and password both are entered wrong. What to do in that case

FaisalSarfraz
Newbie Poster
17 posts since May 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1

for all those who were also looking for this or by chance came across thi, here is the corrected code and it works :)

  String user=jTextField1.getText();
String pwd= new String (jPasswordField1.getPassword());
int tmp=0;
while(rs.next()) {
String uname=rs.getString("user name");
String password=rs.getString("password");
if ((user.equals(uname)) && (pwd.equals(password))) {
new admin_panel().setVisible(true);
tmp++;
    }
}
if (tmp==0) {
JOptionPane.showMessageDialog(null, "Username and Password not in database!");
   }
FaisalSarfraz
Newbie Poster
17 posts since May 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1
Question Answered as of 9 Months Ago by gusano79 and rubberman

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.1041 seconds using 2.71MB