0

嘿伙计们,我有一个 GUI,用户可以在其中输入用户名和密码。我希望它检查数据库中的匹配信息,如果成功,他们被授予访问权限,如果没有,他们被拒绝。所以我的问题是当我单击登录时如何让我的 Gui 使用 JDBC?

这是我的登录 gui。

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

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

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) {
                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);
            }
        });
    }
}

这是我的 JDBC

import java.sql.*;

public class usernamecheck 
{
    // database URL                              

    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 main(String args[], String username, String password)
            throws SQLException {

        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
} //end class
4

1 回答 1

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[])

{ 日志框架=新日志();frame.setSize(500,500); frame.setLocationRelativeTo(null); frame.setVisible(true); } }

公共类 usernamecheck { 静态最终字符串 DATABASE_URL = "jdbc:mysql://localhost:3306/test"; 静态最终字符串 USERNAME = "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

}

于 2013-09-21T17:52:53.500 回答