我想从 to 传递stuId值,它在登录后出现在表单旁边。LoginForm
IndoorCategoriesForm
Home
Categories
我试过重载方法IndoorCategoriesForm
String id;
//overload the constructor
public IndoorCategoriesForm(String stuId){
initComponents();
//get student id from the login
this.id = stuId
labelStudentId.setText(id);
}
然后使用该getText()
方法获取表内的值。当我运行项目时,数据库中的Enrolled表显示了SportId的值,但StudentId为空。
室内类别表格:
private void btnEnrollBasketBallActionPerformed(java.awt.event.ActionEvent evt) {
PreparedStatement pst;
//query to enrol the user
String enrollUserQuery = "INSERT INTO `Enrolled`(`StuId`, `SpId`) VALUES (?, ?)";
//get student id from the login text field
String stuId = labelStudentId.getText();
//basketball sport id
String basketball = "1002";
try {
pst = DbConnection.getConnection().prepareStatement(enrollUserQuery);
pst.setString(1, stuId);
pst.setString(2, basketball);
if (pst.executeUpdate() != 0){
//if enrolling successfull, show enroll success form
EnrollSuccessfullForm esf = new EnrollSuccessfullForm();
esf.setVisible(true);
esf.pack();
esf.setLocationRelativeTo(null);
esf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.dispose();
}
else{
JOptionPane.showMessageDialog(null, "You have already enrolled");
}
} catch (SQLException ex){
Logger.getLogger(IndoorCategoriesForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
登录表单:
private void buttonLogInActionPerformed(java.awt.event.ActionEvent evt) {
PreparedStatement pst;
ResultSet rs;
//get stu id and password
String stuId = jTextFieldId.getText();
String pass = String.valueOf(jPasswordField.getPassword());
//check if the stuId exist in the database
String userLoginQuery = "SELECT * FROM `Student` WHERE `Stu_Id` = ? AND `Stu_Password` = ?";
if(stuId.trim().equals(""))
{
JOptionPane.showMessageDialog(null, "Please enter a user ID", "Empty Field", 2);
}
else if(pass.trim().equals("")) {
JOptionPane.showMessageDialog(null, "Please enter a password", "Empty Field", 2);
}
else
{
try {
pst = DbConnection.getConnection().prepareStatement(userLoginQuery);
pst.setString(1, stuId);
pst.setString(2, pass);
rs = pst.executeQuery();
if(rs.next()){
//get value from the student Id to pass to Indoor categories form
new IndoorCategoriesForm(stuId).setVisible(false);
//shows the home page
HomeForm hf = new HomeForm();
hf.setVisible(true);
hf.pack();
hf.setLocationRelativeTo(null);
hf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.dispose();
}
else {
JOptionPane.showMessageDialog(null, "Invalid user Id or pasword", "Login Error", 2);
}
} catch (SQLException ex) {
Logger.getLogger(LogInForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
}