Reputation: 11
I'm having problems on how to connect my two classes. What I want to happen is, when the user logs in, the GradingSystem
class will run and automatically display the user's information based on my MS ACCESS database. Somehow, I have no idea how to call or connect the Login class to the other class.
Login class
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) {
}
GradingSystem class
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) {
}
Upvotes: 1
Views: 189
Reputation: 1064
You can use some static fields in your login class, and fill them after logging in, then in GradingSystem class you can use them.
something like:
public static string UserName;
public static string Password;
and in GradingSystem use:
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) {
}
don't forget to fill them after logging in.
Upvotes: 1