当前,我有一个方法在场景加载后填充表,但是现在只有一列根本没有加载,这是另一张表的用户名列,我怀疑其中存在错误我的sql语句,但是我可能错了,并且该错误在其他地方。当前输出的结果在提供的https://imgur.com/iX8beaH
链接中初始化方法
@FXML
public void initialize()
{
try{
searchStaff();
} catch(SQLException ex)
{
System.out.println("Error occurred while getting employees information from DB.\n" + ex);
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
staffTableView.setOnMouseClicked((MouseEvent event) -> {
if (event.getClickCount() > 0) {
Staff staff = new Staff();
Users users = new Users();
staff = staffTableView.getSelectionModel().getSelectedItem(); // *** get selection from TableView ***
staffIDtxtfld.setText(staff.getStaffID());
fnametxtfld.setText(staff.getFname());
lnametxtfld.setText(staff.getLname());
fullNametxtfld.setText(staff.getFullName());
userRoleBox.setValue(staff.getRole());
taskPreftxtfld.setText(staff.getTaskPref());
usernametxtfld.setText(users.getUsername());
}
});
}
搜索人员
@FXML
private void searchStaff() throws SQLException, ClassNotFoundException{
try{
staffIDColumn.setCellValueFactory(new PropertyValueFactory<>("staffID"));
fnameColumn.setCellValueFactory(new PropertyValueFactory<>("fname"));
lnameColumn.setCellValueFactory(new PropertyValueFactory<>("lname"));
fullNameColumn.setCellValueFactory(new PropertyValueFactory<>("fullName"));
taskPrefColumn.setCellValueFactory(new PropertyValueFactory<>("taskPref"));
roleColumn.setCellValueFactory(new PropertyValueFactory<>("role"));
userColumn.setCellValueFactory(new PropertyValueFactory<>("username"));
ObservableList<Staff> staffData = EditUsersDAO.getStaffList();
populateStaff(staffData);
} catch (SQLException e){
System.out.println("Error");
}
}
填充StaffTable
@FXML
private void populateStaff (ObservableList<Staff> staffData) throws ClassNotFoundException {
staffTableView.setItems(staffData);
}
获取staffList
public static ObservableList<Staff> getStaffList() throws SQLException, ClassNotFoundException{
Connection conn = DBConnection.getConnection();
try
{
String sql = "SELECT * FROM Staff INNER JOIN Login ON Staff.staffID = Login.staff_ID";
ResultSet rs = conn.createStatement().executeQuery(sql);
ObservableList<Staff> staffList = setStaffList(rs);
return staffList;
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
设置人员列表
private static ObservableList<Staff> setStaffList(ResultSet rs) throws SQLException, ClassNotFoundException {
ObservableList<Staff> staffList = FXCollections.observableArrayList();
while (rs.next()){
Staff staff = new Staff();
Users users = new Users();
users.setUsername(rs.getString("username"));
staff.setStaffID(rs.getString("staffID"));
staff.setFname(rs.getString("fname"));
staff.setLname(rs.getString("lname"));
staff.setRole(rs.getString("role"));
staff.setTaskPref(rs.getString("task_Preference"));
staff.setFullName(rs.getString("full_Name"));
staffList.add(staff);
}
return staffList;
}
更新: Users.java
public class Users {
public SimpleStringProperty username;
public SimpleStringProperty password;
public SimpleStringProperty role;
public SimpleStringProperty staff_ID;
public Users(){
this.staff_ID = new SimpleStringProperty();
this.password = new SimpleStringProperty();
this.username = new SimpleStringProperty();
this.role = new SimpleStringProperty();
}
public StringProperty staffIDProperty(){
return staff_ID;
}
public StringProperty usernameProperty(){
return username;
}
public StringProperty passwordProperty(){
return password;
}
public StringProperty roleProperty(){
return role;
}
/**
* @return the staff_ID
*/
public String getStaffID() {
return staff_ID.get();
}
/**
* @param staffID the staff_ID to set
*/
public void setStaffID(String staffID) {
this.staff_ID.set(staffID);
}
/**
* @return the username
*/
public String getUsername() {
return username.get();
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username.set(username);
}
/**
* @return the password
*/
public String getPassword() {
return password.get();
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password.set(password);
}
/**
* @return the role
*/
public String getRole() {
return role.get();
}
/**
* @param role the role to set
*/
public void setRole(String role) {
this.role.set(role);
}
}