gpt4 book ai didi

java - 登录后如何向其他类(class)显示用户信息?

转载 作者:太空宇宙 更新时间:2023-11-04 07:20:51 25 4
gpt4 key购买 nike

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

登录类

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

最佳答案

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

类似于:

public static string UserName;
public static string Password;

并在评分系统中使用:

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

}

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

关于java - 登录后如何向其他类(class)显示用户信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19344612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com