0

我目前正在制作一个 gui 程序,但我对如何设置程序一无所知,所以当用户输入用户名和密码,然后单击登录按钮时,它会检查数据库中的用户名和密码,并且如果用户名和密码在数据库中,他们接受,如果没有拒绝。

这是我的欢迎屏幕,在我打算整理它的那一刻它很糟糕。

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

/**
 *
 * @author 
 */
public class Welcome extends JFrame {

    private JButton Existing, New, Exit;
    private JLabel Welcome, Date, Version;
    private JPanel WelcomeP;
    private JMenuItem jmiNew, jmiExisting, jmiExit, jmiAbout;

    public Welcome() {
        //create menu bar
        JMenuBar regMenuBar = new JMenuBar();

        //set menu bar to the applet
        setJMenuBar(regMenuBar);
        //add menu "operation" to menu bar
        JMenu optionsMenu = new JMenu("Options");
        optionsMenu.setMnemonic('O');
        regMenuBar.add(optionsMenu);

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

        //add menu items with mnemonics to menu "options"
        optionsMenu.add(jmiNew = new JMenuItem("New", 'N'));
        optionsMenu.add(jmiExisting = new JMenuItem("Existing", 'E'));
        optionsMenu.addSeparator();
        optionsMenu.add(jmiExit = new JMenuItem("Exit", 'E'));


        Container c = getContentPane();
        c.setLayout(new BorderLayout());

        WelcomeP = new JPanel();
        WelcomeP.setLayout(new GridLayout(2, 1));


        Welcome = new JLabel("Welcome");
        Date = new JLabel("Date: 01/10/2013");
        Version = new JLabel("Version 0.1");
        Exit = new JButton("Exit");
        Existing = new JButton("Existing User");
        New = new JButton("New User");
        
        WelcomeP.add(Welcome);
        WelcomeP.add(Date);
        WelcomeP.add(Version);
        WelcomeP.add(Existing);
        WelcomeP.add(Exit);
        WelcomeP.add(New);


        Exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        New.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                registerInterface regFace = new registerInterface();
                regFace.setVisible(true);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        Existing.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                log login = new log();
                login.setVisible(true);
                login.setSize(500, 300);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        jmiExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        jmiNew.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                registerInterface regFace = new registerInterface();
                regFace.setVisible(true);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        jmiExisting.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                log shoeFace = new log();
                shoeFace.setVisible(true);
                shoeFace.setSize(500, 300);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        //listner for about menuitem
        jmiAbout.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(null, 
                    "Program Dedicated to researchers of eAgriculture"
                                        + "\n Assignment for University", 
                        "About", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        c.add(WelcomeP, BorderLayout.CENTER);

        setSize(500, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        Welcome app = new Welcome();
    }
}

当我单击现有用户时,它会将我带到登录面板:

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

我已经在netbeans中设置了一个JDBC,并添加了带有用户名和密码的表,并用模拟的用户名和密码填充了这些表,我还想在他们单击新用户时制作它,他们可以向数据库添加新信息,以便他们可以长期使用新帐户。

下面是我的 JDBC

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 

public class JDBC { 
    private Connection con = null;
    private String query = null; 
    private ResultSet rs; 
    private String url = "jdbc:derby://localhost:1527/Assignment";    
    private String usrName = "root";
    private String pwd = "root";
    
    public JDBC() throws Exception { 
        try { 
            con = DriverManager.getConnection(url, usrName, pwd); 
            query = "SELECT USERNAME, PASSWORD from root.PERSON";
            PreparedStatement stm = con.prepareStatement(query); 
            rs = stm.executeQuery(); 
            while (rs.next()) { 
                String USER = rs.getString("USERNAME"); 
                String PW = rs.getString("PASSWORD"); 
                
                System.out.println(" USERNAME: " + 
                        USER + " PASSWORD: " + PW); 
            } 
            close(); 
        } catch (Exception e) {           
        }
    }
    private void close() { 
        try { 
            if (rs != null) { 
                rs.close(); 
            } 
            if (con != null) {
                con.close(); 
            }
        } catch (Exception e) { 
        }
    }
    
    public static void main(String[] args) throws Exception { 
        JDBC dao = new JDBC();
    } 
} 
4

3 回答 3

1

有关 SQL 的信息,请阅读JDBC 数据库访问教程。

编辑:

你为什么要阅读所有用户名和密码?对于登录检查,您将查询数据库以查看在用户名/密码文本字段中输入的值是否在数据库中找到。

所以查询会是这样的:

query = "select userid from person where userid = ? and password = ?";
PreparedStatement stm = con.prepareStatement(query); 
stmt.setString(1, useridTextField.getText());
stmt.setString(2, passwordTextField.getPassword().toString());

因此,您的 SQL 方法需要访问文本字段中的数据。

然后,如果 ResultSet 不为空,您就知道为给定的用户 ID 输入了正确的密码。

再次,阅读教程以了解正确的 SQL。

于 2013-09-21T04:40:33.563 回答
0

您应该学习编写 DAO 和连接管理器类,然后将它们插入您的 gui 代码中。以下是您可以使用的方法:

  1. 创建连接管理器类以使用 jdbc 构造创建数据库连接。
  2. 编写DAO类对Login表数据等表数据进行操作,适当调用如LoginDAO
  3. 如果需要对获取的数据执行某些业务逻辑或操作,请添加服务类。
  4. 最后形成你的动作监听器调用服务类来完成你的任务

上述方法完全受 MVC 架构的影响,编写模块化和松散耦合的代码。

于 2013-09-21T04:42:41.833 回答
0

这是一小段代码,用于检查用户名和密码,用于 mysql 数据库

public void check(String username,String password)
{
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","username","password");
        PreparedStatement pt=con.prepareStatement("select username,password from authentication_table where username=?");
        pt.setString(1, username);
        ResultSet rs=pt.executeQuery();
        String orgUname="",orPass="";
        while(rs.next())
        {
            orgUname=rs.getString("username");
            orPass=rs.getString("password");

        }
        if(orPass.equals(password))
        {
            //do something
        }
        else
        {
            //do something
        }
    }
    catch(Exception e)
    {

    }
于 2013-09-21T05:51:32.197 回答