1

我在如何连接我的两个班级时遇到问题。我想要发生的是,当用户登录时,GradingSystem该类将运行并根据我的 MS ACCESS 数据库自动显示用户的信息。不知何故,我不知道如何调用登录类或将登录类连接到其他类。

登录课程

private class ButtonHandler implements ActionListener{
  @Override
  public void actionPerformed(ActionEvent e) {
    DatabaseDriver driver = new DatabaseDriver();
    Object source = e.getSource();

    if (source == btnLogin) {
      try {
        String username = txtUserName.getText();
        String password = String.valueOf(txtPassword.getPassword());
        String query =
               "Select username,password from [useraccount] where [username]='"
               + username + "' and [password]='" + password+"'";
        driver.selectQuery(query);
        ResultSet rsLogin = driver.rs;
        if (rsLogin.next()) {
          String logo = "img/seal_TIP.png";
          java.net.URL addURL = getClass().getClassLoader().getResource(logo);
          ImageIcon image1 = new ImageIcon(addURL);
          JOptionPane.showMessageDialog(null, "Welcome to your TIP account.",
                     "Login Success", JOptionPane.INFORMATION_MESSAGE, image1);
          dispose();
          new GradingSystem().run();
        } else {
          JOptionPane.showMessageDialog(null, "Incorrect Username or Password.",
                     "User Not Found!", JOptionPane.INFORMATION_MESSAGE);
        }
      } catch (SQLException ex) {
      }

分级系统类

DatabaseDriver driver = new DatabaseDriver();

 String query = "Select lastName from [useraccount] where [username]='"
                 +login.username+"' and [password]='"+login.password+"'";
 driver.selectQuery(query);
 ResultSet rsGradingSystem = driver.rs;
   try {
     if (rsGradingSystem.next()) {
       JLabel studName= new JLabel("Name: ");
       studName.setBounds(15,60,150,20);
       add(studName);
     }
   } catch (SQLException ex) {
   }
4

1 回答 1

1

您可以在登录类中使用一些静态字段,并在登录后填写它们,然后在 GradingSystem 类中使用它们。

就像是:

public static string UserName;
public static string Password;

在 GradingSystem 中使用:

DatabaseDriver driver = new DatabaseDriver();

 String query = "Select lastName from [useraccount] where [username]='"+Login.UserName+"' and [password]='"+Login.Password+"'";
 driver.selectQuery(query);
 ResultSet rsGradingSystem = driver.rs;
    try {
        if (rsGradingSystem.next()) {
           JLabel studName= new JLabel("Name: ");
           studName.setBounds(15,60,150,20);
           add(studName);
        }
    } catch (SQLException ex) {

    }

登录后不要忘记填写它们。

于 2013-10-13T11:26:29.953 回答