我正在使用登录程序,用户使用staffID登录,我希望系统显示登录的员工姓名。
如何从staffID检索staffName?我已经尝试过,但我仍然得到staffName为null。
我的代码如下。前两个方法来自DA类,而最后一个方法是UI。
非常感谢任何帮助,在此先感谢:)
private static Staff convertToStaff(ResultSet rs) throws SQLException {
Staff staff;
int id = rs.getInt("id");
String staffID = rs.getString("staffID");
String staffName = rs.getString("staffName");
String staffPassword = rs.getString("staffPassword");
String staffNRIC = rs.getString("staffNRIC");
String staffGender = rs.getString("staffGender");
int staffContactNo = rs.getInt("staffContactNo");
String staffEmail = rs.getString("staffEmail");
String dob = rs.getString("dob");
String department = rs.getString("department");
staff = new Staff(id, staffID, staffName, staffNRIC, staffGender, staffContactNo, staffEmail, dob, department, staffPassword);
return staff;
}
public static Staff retrieveNameByStaffID(String staffID) {
ResultSet rs = null;
DBController db = new DBController();
String dbQuery;
PreparedStatement pstmt;
Staff staffName = null;
// step 1 - connect to database
db.getConnection();
// step 2 - declare the SQL statement
dbQuery = "SELECT staffName FROM staff WHERE staffID = ?";
pstmt = db.getPreparedStatement(dbQuery);
// step 3 - execute query
try {
pstmt.setString(1, staffID);
rs = pstmt.executeQuery();
if (rs.next()) { // first record found
staffName = convertToStaff(rs);
}
} catch (Exception e) {
e.printStackTrace();
}
db.terminate();
return staffName;
}
private void actionPerformedStaffLogin() {
String staffID = txtID.getText();
String staffPassword = passwordField.getText();
String adminID = txtID.getText();
String adminPassword = passwordField.getText();
Staff staffName = StaffDA.retrieveNameByStaffID(staffID);
try {
//StaffDA.logIn(staffID, staffPassword);
//StaffController.logIn(staffID, staffPassword);
if (StaffController.logIn(staffID, staffPassword) == 1) {
JOptionPane.showMessageDialog(null, "Welcome " + staffName);
System.out.println(staffName);
JPanel contentPane = new StaffMenu(HMSFrame);
HMSFrame.setContentPane(contentPane);
HMSFrame.setVisible(true);
} else if (StaffController.adminlogIn(adminID, adminPassword) == 1) {
JOptionPane.showMessageDialog(null, "Welcome " + adminID);
JPanel contentPane = new AdminMenu(HMSFrame);
HMSFrame.setContentPane(contentPane);
HMSFrame.setVisible(true);
} else if (StaffController.logIn(staffID, staffPassword) > 1) {
JOptionPane.showMessageDialog(null, "duplicate");
} else {
JOptionPane.showMessageDialog(null, "ID and Password mismatch");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
答案 0 :(得分:0)
更改此查询
dbQuery = "SELECT staffName FROM staff WHERE staffID = ?";
您只使用此查询获得staffName,但使用此
dbQuery = "SELECT * FROM staff WHERE staffID = ?";
你可以获得所有属性。
答案 1 :(得分:0)
您的重写代码必须像这样
private static Staff convertToStaff(ResultSet rs) throws SQLException {
Staff staff;
int id = rs.getInt("id");
String staffID = rs.getString("staffID");
String staffName = rs.getString("staffName");
String staffPassword = rs.getString("staffPassword");
String staffNRIC = rs.getString("staffNRIC");
String staffGender = rs.getString("staffGender");
int staffContactNo = rs.getInt("staffContactNo");
String staffEmail = rs.getString("staffEmail");
String dob = rs.getString("dob");
String department = rs.getString("department");
staff = new Staff(id, staffID, staffName, staffNRIC, staffGender, staffContactNo, staffEmail, dob, department, staffPassword);
return staff;
}
public static Staff retrieveNameByStaffID(String staffID) {
ResultSet rs = null;
DBController db = new DBController();
String dbQuery;
PreparedStatement pstmt;
Staff staffName = null;
// step 1 - connect to database
db.getConnection();
// step 2 - declare the SQL statement
dbQuery = "SELECT * FROM staff WHERE staffID = ?";
pstmt = db.getPreparedStatement(dbQuery);
// step 3 - execute query
try {
pstmt.setString(1, staffID);
rs = pstmt.executeQuery();
if (rs.next()) { // first record found
staffName = convertToStaff(rs);
}
} catch (Exception e) {
e.printStackTrace();
}
db.terminate();
return staffName;
}
private void actionPerformedStaffLogin() {
String staffID = txtID.getText();
String staffPassword = passwordField.getText();
String adminID = txtID.getText();
String adminPassword = passwordField.getText();
Staff staffName = StaffDA.retrieveNameByStaffID(staffID);
try {
//StaffDA.logIn(staffID, staffPassword);
//StaffController.logIn(staffID, staffPassword);
if (StaffController.logIn(staffID, staffPassword) == 1) {
JOptionPane.showMessageDialog(null, "Welcome " + staffName);
System.out.println(staffName);
JPanel contentPane = new StaffMenu(HMSFrame);
HMSFrame.setContentPane(contentPane);
HMSFrame.setVisible(true);
} else if (StaffController.adminlogIn(adminID, adminPassword) == 1) {
JOptionPane.showMessageDialog(null, "Welcome " + adminID);
JPanel contentPane = new AdminMenu(HMSFrame);
HMSFrame.setContentPane(contentPane);
HMSFrame.setVisible(true);
} else if (StaffController.logIn(staffID, staffPassword) > 1) {
JOptionPane.showMessageDialog(null, "duplicate");
} else {
JOptionPane.showMessageDialog(null, "ID and Password mismatch");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}