需要实现JDBC检查用户名和密码到我的Gui

时间:2013-09-21 17:38:36

标签: java user-interface jdbc

嘿伙计们我有一个用户输入用户名和密码的GUI。我希望它检查数据库中的匹配信息,如果成功,他们被授予访问权限,如果没有,则被拒绝。所以我的问题是当我点击Login?

时如何让我的Gui使用JDBC?

1 个答案:

答案 0 :(得分:0)

将main方法移至log类。在loginButton.addActionListener方法内调用方法来检查用户名和密码

public class log extends JFrame{

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

log() {
    //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);
            log.this.dispose();
            log.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);
            log.this.dispose();
            log.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{
            usernamecheck.checkLogin(jtfUsername.getText(),jtfPassword.getText());
            }catch(SQLException se){

            }
            MainMenu mainmenu = new MainMenu();
            mainmenu.setVisible(true);
            mainmenu.setSize(500, 500);
            mainmenu.setLocationRelativeTo(null);
            registerInterface regFace = new registerInterface();
            regFace.setVisible(false);
            log.this.dispose();
            log.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);
            log.this.dispose();
            log.this.setVisible(false);   
        }
    });
}
public static void main(String arg[])

{       log frame = new log();       frame.setSize(500500);       frame.setLocationRelativeTo(NULL);       frame.setVisible(真);   } }

public class usernamecheck {     static final String DATABASE_URL =“jdbc:mysql:// localhost:3306 / test”;     static final String USERNAME =“root”;     static final String PASSWORD =“root”;

// launch the application
public static void checkLogin(String username, String password)
        throws SQLException {
    System.out.print("dfdF");

    Connection connection = null; // manages connection
    PreparedStatement pt = null; // manages prepared statement

    // 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,password from person where userName=?");

        // process query results
        pt.setString(1, username);
        ResultSet rs = pt.executeQuery();
        String orgUname = "", orPass = "";
        while (rs.next()) {
            orgUname = rs.getString("userName");
            orPass = rs.getString("password");
        } //end while
        if (orPass.equals(password)) {
            //do something
            rs.close();
        } else {
            //do something
        }
    }//end try
    catch (Exception e) {
    } //end catch  
} //end main

}