java - SQL 登录验证 - 检查用户类型

标签 java mysql sql authentication verification

下面的程序当前检查用户登录详细信息是否正确。然后进入下一部分,检查用户是否是管理员。目前,我正在尝试通过在我的数据库中将所有普通用户列为“user_admin”[null]来解决此问题,而管理员将被列为 1在数据库中。我环顾四周,似乎没有人在我正在做的方法中询问与此相关的问题。

如果您查看我的代码,您会发现普通用户应指向 UserPanel,而管理员应指向 AdminPanel。这是因为我的程序设置为人们可以查看帐户,而管理员可以编辑帐户。

这是我正在从事的一个拼贴项目,因此程序很简单。

JLabel lblNewLabel = new JLabel("");
    lblNewLabel.setIcon(new ImageIcon(PanelLogin.class.getResource("/image/Login.png")));
    lblNewLabel.setBounds(118, 115, 100, 26);
    lblNewLabel.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            lblNewLabel.setIcon(new ImageIcon(PanelLogin.class.getResource("/image/Loginv2.png")));
        }
        @Override
        public void mouseExited(MouseEvent e) {
            lblNewLabel.setIcon(new ImageIcon(PanelLogin.class.getResource("/image/Login.png")));
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            if (LoginAttempts < 3 ) {
            try {
                String Host = "removed";
                String Name = "removed";
                String Pass = "removed";

                Connection conn = DriverManager.getConnection( Host, Name, Pass );  
                PreparedStatement pst = conn.prepareStatement("SELECT `user_name`, `user_pass` FROM `table_1` WHERE `user_name` = ? AND `user_pass` = ?");
                pst.setString(1, textID.getText());
                pst.setString(2, String.valueOf(passwordField.getPassword()));
                ResultSet Result = pst.executeQuery();
                if (Result.next()) {
                    String user =Result.getString("user_name");
                    try {
                        PreparedStatement pst2 = conn.prepareStatement("SELECT `user_admin` FROM `table_1` WHERE `user_name` = ?");
                        pst2.setString(1, user);
                        if (Result.next()) {
                            frmLotusLogin.dispose();
                            new UserPanel(user).frame.setVisible(true);
                        }
                        else {
                            frmLotusLogin.dispose();
                            new AdminPanelMain(user).frmLotusSecurity.setVisible(true);
                        }

                    }
                    catch (Exception exc){

                    }
                }
                else {
                    JOptionPane.showMessageDialog(null, "Incorrect Username/Password");
                    LoginAttempts = LoginAttempts + 1;
                }
            }
            catch (Exception ex) {
                System.out.println(ex);
                JOptionPane.showMessageDialog(null, "An error occurred. Your Username/Password could be incorrect, "
                        + "If error contiues to appear please contact support! Error Number: L1");
            }
            }
            else {
                JOptionPane.showMessageDialog(null, "You Have Entered The Wrong Password Too Many Times, You are now locked out!");
            }
        }
    });

提前感谢任何发现我做错了什么或需要更改才能正确验证的人。如果有人需要有关正在发生的事情的更多信息,请随时询问。

第一次建议之后

try {
                String Host = "";
                String Name = "";
                String Pass = "";

                Connection conn = DriverManager.getConnection( Host, Name, Pass );  
                PreparedStatement pst = conn.prepareStatement("SELECT `user_name`, `user_pass` FROM `table_1` WHERE `user_name` = ? AND `user_pass` = ?");
                pst.setString(1, textID.getText());
                pst.setString(2, String.valueOf(passwordField.getPassword()));
                ResultSet Result = pst.executeQuery();
                    if (Result.next()) {
                        String user = Result.getString("user_name");
                        try {
                            PreparedStatement pst2 = conn.prepareStatement("SELECT `user_admin` FROM `table_1` WHERE `user_name` = ?");
                            pst2.setString(1, user);
                            ResultSet Result2 = pst2.executeQuery();  // added
                            if (Result2.next()) { // modified
                                System.out.println("Test");
                                frmLotusLogin.dispose();
                                new UserPanel(user).frame.setVisible(true);
                            } else {
                                frmLotusLogin.dispose();
                                new AdminPanelMain(user).frmLotusSecurity.setVisible(true);
                            }

                        } catch (Exception exc) {

                          // do something here !

                        }
                }
                else {
                    JOptionPane.showMessageDialog(null, "Incorrect Username/Password");
                    LoginAttempts = LoginAttempts + 1;
                }
            }
            catch (Exception ex) {
                System.out.println(ex);
                JOptionPane.showMessageDialog(null, "An error occurred. Your Username/Password could be incorrect, "
                        + "If error contiues to appear please contact support! Error Number: L1");
            }
            }
            else {
                JOptionPane.showMessageDialog(null, "You Have Entered The Wrong Password Too Many Times, You are now locked out!");
            }
        }
    });

Here is the view of the table I'm using

这就是我的表格当前的样子,您可以看到我正在做什么来声明用户是否是管理员

最佳答案

您永远不会执行第二条语句,当然也不会使用它的结果(因为您仍然使用第一个结果集)。

另外,当你捕获异常时,永远不要忽略它们,除非你确定它根本不重要。

最后,您应该在使用完语句/连接后关闭它们(我没有将这部分添加到下面的代码中)。

        ResultSet Result = pst.executeQuery();
        if (Result.next()) {
            String user = Result.getString("user_name");
            try {
                PreparedStatement pst2 = conn
                        .prepareStatement("SELECT `user_admin` FROM `table_1` WHERE `user_name` = ? AND `user_admin` = 1");

                pst2.setString(1, user);
                ResultSet Result2 = pst2.executeQuery();  // added
                if (Result2.next()) { // modified
                    frmLotusLogin.dispose();
                    new AdminPanel(user).frame.setVisible(true);
                } else {
                    frmLotusLogin.dispose();
                    new MainUserPanel(user).frmLotusSecurity.setVisible(true);
                }

            } catch (Exception exc) {

              // do something here !

            }

关于java - SQL 登录验证 - 检查用户类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37294234/

相关文章:

java - 异步类无法打印出 JSON 提要的内容

java - Action 监听器,执行 Gui

Mysql TIMEDIFF NULL 值

mysql - 如何构建 MySQL 查询以获取具有所有选定功能的产品?

php - 批量插入数据库

java - 如何在 QuickFIX/J 中对传出消息中的字段进行排序

java - dynamodb 我如何模拟映射器?

mysql - 有前途的 Node 服务器代码

php - mysql 获取表中的最后一行

java - Spring/Thymeleaf : Property or field cannot be found on null, 但仍在渲染