如何使用Java在MySQL表中仅搜索一条特定记录?

时间:2019-03-16 06:16:06

标签: java mysql jdbc

我正在制作一个应用程序,试图使用存储在MySQL服务器中的数据登录用户。 我遇到的问题是,当我尝试使用正确的用户名和密码登录时,我的代码实际上是在检查“ adminData”表中的每条记录,并为我显示每条记录的对应结果! 我也附上我的代码,请看看! 因此,我想问一问,我只能搜索一条特定记录,而该记录只能恢复该特定记录! 请帮帮我..:)

btnSubmit.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        String query = "SELECT username, password FROM adminData";
        String username = null;
        String password = null;
        ;
        try
        {
            rs = st.executeQuery(query);
            String fieldWarning = "*all field are mandatory!";

            String enteredUsername = usernameField.getText();
            @SuppressWarnings("deprecation") String enteredPassword = passwordField.getText();

            usernameField.setCursor(null);
            fieldLabel.setText("");

            int userLen = enteredUsername.length();
            int passLen = enteredPassword.length();

            if (userLen != 0 && passLen != 0)
            {
                if (userLen <= 32 && passLen <= 32)
                {
                    while (rs.next())
                    {
                        username = rs.getString("username");
                        password = rs.getString("password");
                        if (enteredUsername.equals(username))
                        {
                            if (enteredPassword.contentEquals(password))
                            {
                                AdminDashboard frame = new AdminDashboard();
                                frame.setVisible(true);
                                frame.dispose();
                            }
                            else
                            {
                                JOptionPane.showMessageDialog(null, "Password Missmatch");
                            }

                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null, "Invalid User");
                            passwordField.setText("");
                            usernameField.setText("");
                            usernameField.setCursor(null);

                        }
                    }
                }
                else
                {
                    JOptionPane.showMessageDialog(null,
                        "Please enter a valid username or password! \n(Note: Only 32 char MAX)");
                    passwordField.setText("");
                    usernameField.setText("");
                }
            }
            else
            {
                fieldLabel.setText(fieldWarning);
                usernameField.setCursor(null);
            }
        }
        catch (SQLException e1)
        {
            JOptionPane.showMessageDialog(null, "Database Error: " + e1);
        }
    }
});

1 个答案:

答案 0 :(得分:0)

这是因为查询将返回整个表。如果您有用户名,则可以简单地调整查询以仅返回包含该特定用户而不是所有用户的详细信息的行。 因此,只需在查询之前分配enterUsername,然后将用户名条件添加到您的sql查询中即可。

btnSubmit.addActionListener(new ActionListener()
    {
public void actionPerformed(ActionEvent e)
{
    String enteredUsername = usernameField.getText();
    String query = "SELECT username, password FROM adminData WHERE username='"+enteredUsername+"'";
    String username = null;
    String password = null;
    ;
    try
    {
        rs = st.executeQuery(query);
        String fieldWarning = "*all field are mandatory!";

        @SuppressWarnings("deprecation") String enteredPassword = passwordField.getText();

        usernameField.setCursor(null);
        fieldLabel.setText("");

        int userLen = enteredUsername.length();
        int passLen = enteredPassword.length();

        if (userLen != 0 && passLen != 0)
        {
            if (userLen <= 32 && passLen <= 32)
            {
                while (rs.next())
                {
                    username = rs.getString("username");
                    password = rs.getString("password");
                    if (enteredUsername.equals(username))
                    {
                        if (enteredPassword.contentEquals(password))
                        {
                            AdminDashboard frame = new AdminDashboard();
                            frame.setVisible(true);
                            frame.dispose();
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null, "Password Missmatch");
                        }

                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, "Invalid User");
                        passwordField.setText("");
                        usernameField.setText("");
                        usernameField.setCursor(null);

                    }
                }
            }
            else
            {
                JOptionPane.showMessageDialog(null,
                    "Please enter a valid username or password! \n(Note: Only 32 char MAX)");
                passwordField.setText("");
                usernameField.setText("");
            }
        }
        else
        {
            fieldLabel.setText(fieldWarning);
            usernameField.setCursor(null);
        }
    }
    catch (SQLException e1)
    {
        JOptionPane.showMessageDialog(null, "Database Error: " + e1);
    }
}
    });

现在,只要您的用户名是唯一的,它将始终返回一条记录。