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

Recommended Answers

All 5 Replies

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.

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

Try changing this code:

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

to this:

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

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

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!");
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.