while循环多次返回错误消息

时间:2013-12-04 13:18:47

标签: java jdbc database-connection

我有一个登录页面,当输入登录详细信息时,while循环访问数据库以验证输入,但如果我在数据库中有3条记录并且输入不正确,则错误消息显示3次,如果输入是更正错误消息显示两次。我知道为什么会发生这种情况(由于while循环)但我无法弄清楚如何抵消这种情况。代码如下:

package securitySystem;

import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;

public class loginPage extends JFrame {

public static void main (String args[]){
    loginPage gui= new loginPage ();
    gui.setSize (400, 400);
    gui.setLocationRelativeTo(null);
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setVisible(true);
    gui.setTitle("Login Page");

}   

JLabel lblUserName= new JLabel("UserName:");
JTextField txtUserName= new JTextField(15);
JLabel lblPassword= new JLabel("Password:");
JTextField txtPassword= new JTextField(15);
JButton btnForgotten= new JButton("Forgotten Login");
JButton btnLogin= new JButton("Login");

public loginPage (){
    setLayout (null);

    //JLabel lblUserName= new JLabel("UserName:");
    lblUserName.setBounds(100,100,110,30);
    add(lblUserName);

    //JTextField txtUserName= new JTextField(15);
    txtUserName.setBounds(170,100,110,30);
    add(txtUserName);

    //JLabel lblPassword= new JLabel("Password:");
    lblPassword.setBounds(100,150,110,30);
    add(lblPassword);

    //JTextField txtPassword= new JTextField(15);
    txtPassword.setBounds(170,150,110,30);
    add(txtPassword);

    //JButton btnLogin= new JButton("Login");
    btnLogin.setBounds(100,300, 70, 30);
    add(btnLogin);
    actionlogin();

    //JButton btnForgotten= new JButton("Forgotten Login");
    btnForgotten.setBounds(175,300, 130, 30);
    add(btnForgotten);


}

public void actionlogin()
{
    btnLogin.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent ae) 
        {

            String username = txtUserName.getText();
            String password = txtPassword.getText();

            String databaseUsername = "";
            String databasePassword = "";

            String dataSourceName = "securitySystem";
            String dbUrl = "jdbc:odbc:" + dataSourceName;

            try{
                //Type of connection driver used    
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                //Connection variable or object param: dbPath, userName, password
                Connection con = DriverManager.getConnection(dbUrl, "", "");

                Statement statement = con.createStatement();

                ResultSet rs = statement.executeQuery("select username, password  from employee");



                while(rs.next())
                {
                    if(username.equals(rs.getString("username")) && password.equals(rs.getString("password")))
                    {
                        adminMenu gui =new adminMenu();
                        gui.setSize (400, 400);
                        gui.setLocationRelativeTo(null);
                        gui.setVisible(true);
                        dispose();
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null,"The username or password that you have entered are incorrect");
                        txtUserName.setText("");
                        txtPassword.setText("");
                        txtUserName.requestFocus();
                    }       
                }

                statement.close();
                con.close();
            }catch (Exception e) {
                try {
                    throw e;
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }                       
        }
    });
}

}

4 个答案:

答案 0 :(得分:1)

一旦失败,您需要添加break;语句以退出循环。

else
{
    JOptionPane.showMessageDialog(null,"The username or password that you have entered are incorrect");
    txtUserName.setText("");
    txtPassword.setText("");
    txtUserName.requestFocus();
    break; // <-- this is needed
} 

你的逻辑看起来也很糟糕。不应该更像(伪代码):

if (record matches username) {
  if (password correct) {
    login();
  } else {
    show an error
  }
} else {
  go around loop again
}

答案 1 :(得分:0)

你应该用一个标志来解决这个问题,比break更清晰。

            boolean nomatches=true;
            while(rs.next())
            {
                if(username.equals(rs.getString("username")) && password.equals(rs.getString("password")))
                {
                    adminMenu gui =new adminMenu();
                    gui.setSize (400, 400);
                    gui.setLocationRelativeTo(null);
                    gui.setVisible(true);
                    dispose();
                    nomatches=false;
                }    
            }

            if(nomatches) {
                    JOptionPane.showMessageDialog(null,"The username or password that you have entered are incorrect");
                    txtUserName.setText("");
                    txtPassword.setText("");
                    txtUserName.requestFocus();
            }

答案 2 :(得分:0)

我认为这不仅仅是一个“休息”声明,你在这里有一个逻辑错误。您说“如果输入正确,则会显示两次错误消息”。因此我们需要退出循环或者在成功时停止迭代,因此在成功案例中添加“break”语句。如果你不想打破循环那么,你可以简单地使用'continue'和一个标志变量。如果案例成功,请将标志更新为1.(记住在创建时将标志初始化为0)。在继续成功案例之前,检查flag的值,取决于它,中断或继续下一次迭代。

答案 3 :(得分:0)

嗯,我不明白为什么你这样检查登录。使用另一个sql-query更舒服:

'select count(*) from employee where username = @username and password = @password'

用输入值替换@username@password,如果语句的结果为0,则输入不正确,如果结果= 1正确。

使用此解决方案,您不需要while循环。