如何根据登录信息为员工或经理显示不同的JFrame

时间:2023-01-22 12:59:15

ok am creating a java application on netbeans with mysql database on sqlworkbench, i created a login jframe and a login table in the database . to check wether username and password exit in database or not and it proceeds if true and shows the employee jframe for example. how can i make it show a different jframe for the manger if he/she enters their username and password . like i should add a column to the table of the login info that determines wether this account for employee or manager . but when clicking on the button on the login jframe how its going to know ? like should i except such a query but how its going to compare then show for example if jframe for employee was named jframe1 and jframe for manager was jframe2

在使用sqlworkbench中的mysql数据库在netbeans上创建java应用程序时,我在数据库中创建了一个登录jframe和一个登录表。检查数据库中是否存在用户名和密码退出,如果是正确的,它将继续执行,并显示employee jframe。如果经理输入了他们的用户名和密码,我如何让它显示不同的jframe ?类似地,我应该向登录信息表中添加一个列,该列确定员工或经理的这个帐户。但是当点击登录jframe上的按钮时,它怎么知道呢?例如,除了这样的查询,我还应该如何进行比较,例如,如果employee的jframe被命名为jframe1,而manager的jframe是jframe2,则该如何进行比较。

        String sql;
        String S=jTextField1.getText();
        String S1=jPasswordField1.getText();
        String S3=jComboBox1.getSelectedItem().toString();
        sql="SELECT * from user_pass where username='"+S+"' and password='"+S1+"' and type='"+S3+"'";
        ResultSet rs=stmt.executeQuery(sql);
        if(rs.next()){

1 个解决方案

#1


4  

You can try a solution like the one below. The code is incomplete but will get you to your solution.

你可以试试下面的方法。代码是不完整的,但是会让您找到您的解决方案。

public class MySqlApp extends JFrame
{
    CardLayout cardLayout = new CardLayout();

    JPanel main = new JPanel();
    JPanel employee = new JPanel();
    JPanel manager = new JPanel();

    public MySqlApp()
    {
        setLayout(new BorderLayout());
        main.setLayout(cardLayout);
        main.add(employee, "employee");
        main.add(manager, "manager");

        add(main, BorderLayout.CENTER);

        String sql="SELECT * from user_pass where username='"+S+"' and password='"+S1+"' and type='"+S3+"'";
        ResultSet rs=stmt.executeQuery(sql);
        if(rs.next())
        {
            if("manager".equalsIgnoreCase(rs.getString("AccountType")))
            {
                cardLayout.show(main, "manager");
            }
            else
            {
                cardLayout.show(main, "employee");
            }
        }
    }
}

#1


4  

You can try a solution like the one below. The code is incomplete but will get you to your solution.

你可以试试下面的方法。代码是不完整的,但是会让您找到您的解决方案。

public class MySqlApp extends JFrame
{
    CardLayout cardLayout = new CardLayout();

    JPanel main = new JPanel();
    JPanel employee = new JPanel();
    JPanel manager = new JPanel();

    public MySqlApp()
    {
        setLayout(new BorderLayout());
        main.setLayout(cardLayout);
        main.add(employee, "employee");
        main.add(manager, "manager");

        add(main, BorderLayout.CENTER);

        String sql="SELECT * from user_pass where username='"+S+"' and password='"+S1+"' and type='"+S3+"'";
        ResultSet rs=stmt.executeQuery(sql);
        if(rs.next())
        {
            if("manager".equalsIgnoreCase(rs.getString("AccountType")))
            {
                cardLayout.show(main, "manager");
            }
            else
            {
                cardLayout.show(main, "employee");
            }
        }
    }
}