java - 用于用户身份验证的 JDBC 无法与 gui 一起使用,即使输入正确的用户名和密码也会阻止访问

标签 java mysql jdbc

我已经设置了一个 Gui 和一个 JBDC。我在表中列出了用户名:admin 和密码:admin,当我将这些输入到 gui 登录中时,它仍然将我锁定,我不确定为什么它将我锁定...下面是我的代码

 public class Login extends JFrame {

    private JTextField jtfUsername, jtfPassword;
    private JButton backButton, loginButton;
    private JMenuItem jmiLogin, jmiBack, jmiHelp, jmiAbout;

    Login() {
        //create menu bar
        JMenuBar jmb = new JMenuBar();

        //set menu bar to the applet
        setJMenuBar(jmb);

        //add menu "operation" to menu bar
        JMenu optionsMenu = new JMenu("Options");
        optionsMenu.setMnemonic('O');
        jmb.add(optionsMenu);

        //add menu "help"
        JMenu helpMenu = new JMenu("Help");
        helpMenu.setMnemonic('H');
        helpMenu.add(jmiAbout = new JMenuItem("About", 'A'));
        jmb.add(helpMenu);

        //add menu items with mnemonics to menu "options"
        optionsMenu.add(jmiLogin = new JMenuItem("Login", 'L'));
        optionsMenu.addSeparator();
        optionsMenu.add(jmiBack = new JMenuItem("Back", 'B'));

        //panel p1 to holds text fields
        JPanel p1 = new JPanel(new GridLayout(2, 2));
        p1.add(new JLabel("Username"));
        p1.add(jtfUsername = new JTextField(15));
        p1.add(new JLabel("Password"));
        p1.add(jtfPassword = new JPasswordField(15));

        //panel p2 to holds buttons
        JPanel p2 = new JPanel(new FlowLayout());
        p2.add(backButton = new JButton("Back"));
        p2.add(loginButton = new JButton("Login"));

        //Panel with image??????

        //add panels to frame
        JPanel panel = new JPanel(new GridLayout(2, 1));
        panel.add(p1, BorderLayout.CENTER);
        panel.add(p2, BorderLayout.SOUTH);
        add(panel, BorderLayout.CENTER);
        setTitle("Main Page");


        //listners for exit menuitem and button
        jmiBack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Welcome welcome = new Welcome();
                welcome.setVisible(true);
                welcome.setSize(500, 500);
                welcome.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                Login.this.dispose();
                Login.this.setVisible(false);
            }
        });

        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Welcome welcome = new Welcome();
                welcome.setVisible(true);
                welcome.setSize(500, 500);
                welcome.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                Login.this.dispose();
                Login.this.setVisible(false);
            }
        });

        //listner for about menuitem
        jmiAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "This is the login panel"
                        + "\n Assignment for University",
                        "About", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        //action listeners for Login in button and menu item
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
    boolean loggedIn = usernamecheck.checkLogin(jtfUsername.getText(), jtfPassword.getText());
    if (!loggedIn) {
        JOptionPane.showMessageDialog(null,"Sorry, wrong credentials"); 
        //displayErrorMessage("Sorry, wrong credentials");
        return;
    }
}
                
catch (SQLException se) {
    se.printStackTrace();
    JOptionPane.showMessageDialog(null,
    "Sorry, couldn't check your credentials. Check the logs and report the problem to an administrator.");
    return;
}
                
                MainMenu mainmenu = new MainMenu();
                mainmenu.setVisible(true);
                mainmenu.setSize(500, 500);
                mainmenu.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                Login.this.dispose();
                Login.this.setVisible(false);
            }
        });

        jmiLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MainMenu mainmenu = new MainMenu();
                mainmenu.setVisible(true);
                mainmenu.setSize(500, 500);
                mainmenu.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                Login.this.dispose();
                Login.this.setVisible(false);
            }
        });
    }

    public static void main(String arg[]) {
        Login frame = new Login();
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
class usernamecheck {

    static final String DATABASE_URL = "jdbc:mysql://localhost:3306/database";
    static final String USERNAME = "root";
    static final String PASSWORD = "root";


   // launch the application
    public static boolean checkLogin(String username, String password)
            throws SQLException {
        System.out.print("Running Check Login \n");

        Connection connection = null; // manages connection
        PreparedStatement pt = null; // manages prepared statement
        Statement stmt = null;
        String query="select userName from person where userName = ? and password = ?";


        // connect to database usernames and query database
        try {

            // establish connection to database
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection(DATABASE_URL, "root", "root");

            // query database
            pt = con.prepareStatement("select userName from person where userName = ? and password = ?");
           
            // process query results
            pt.setString(1, username);
            ResultSet rs = pt.executeQuery(query);
            String orgUname = "", orPass = "";
            if (rs.next()) {
                orgUname = rs.getString("userName");
                orPass = rs.getString("password");
            } //end while
            if (rs.next() && password.equals(rs.getString("password")) && username.equals(rs.getString("userName"))) {
                //do something
                return true;
            } else {
                //do something
                return false;
            }
        }//end try
        catch (Exception e) {
        } //end catch  
        return false;
    } //end main
}

最佳答案

在您的 checkLogin(String username, String password) 方法中, 您将 PreparedStatement 设置为参数,但忘记设置密码

           // query database
            pt = con.prepareStatement("select userName from person where userName = ? and password = ?");

            // process query results
            pt.setString(1, username);
            ??

关于java - 用于用户身份验证的 JDBC 无法与 gui 一起使用,即使输入正确的用户名和密码也会阻止访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19077721/

相关文章:

java - 从数组中删除元素然后减小数组大小的最有效方法

php - 如何在yii2中显示关系数据

php - 生成部分填充的表格

java - 如何在 sqljdbc4.jar 中配置 Java Kerberos

java - jdbc Rowset 在哪里使用?

java - 类构造函数不区分 args... 和 args[][]

Java Web Start 安全错误

java - 向父类中定义的字段添加注释

mysql - 如何更改 mysql wamp 3.1.7 中的登录有效性

java - 仅更新表单中的单个字段,而 Struts 2 中的其他信息保持不变