I've been working on JavaFX app for some archive, I made a simple user interface app with add, edit, delete, update and search methods, the app works fine, the MySQL database is correctly connected and the methods are being applied correctly my one issue is the Tableview in my app is blank, not showing the data in my database here is the code of my controller class:
@FXML
private TableView<users> table_users;
@FXML
private TableColumn<users, Integer> col_id;
@FXML
private TableColumn<users, String> col_username;
@FXML
private TableColumn<users, String> col_napel;
@FXML
private TableColumn<users, String> col_nserie;
@FXML
private TableColumn<users, String> col_fc;
@FXML
private TableColumn<users, String> col_nc;
@FXML
private TableColumn<users, String> col_du;
@FXML
private TableColumn<users, String> col_ms;
@FXML
private TableColumn<users, String> col_gp;
@FXML
private TableColumn<users, String> col_pw;
@FXML
private TextField filterField;
ObservableList<users> listM;
ObservableList<users> dataList;
int index = -1;
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
@Override
public void initialize(URL url, ResourceBundle rb) {
col_id.setCellValueFactory(new PropertyValueFactory<users,Integer>("id"));
col_username.setCellValueFactory(new PropertyValueFactory<users,String>("username"));
col_napel.setCellValueFactory(new PropertyValueFactory<users,String>("napel"));
col_nserie.setCellValueFactory(new PropertyValueFactory<users,String>("nserie"));
col_fc.setCellValueFactory(new PropertyValueFactory<users,String>("fc"));
col_nc.setCellValueFactory(new PropertyValueFactory<users,String>("nc"));
col_du.setCellValueFactory(new PropertyValueFactory<users,String>("du"));
col_ms.setCellValueFactory(new PropertyValueFactory<users,String>("ms"));
col_gp.setCellValueFactory(new PropertyValueFactory<users,String>("gp"));
col_pw.setCellValueFactory(new PropertyValueFactory<users,String>("pw"));
listM = mysqlConnect.getDatausers();
table_users.setItems(listM);
UpdateTable();
search_user();
}
}
here is the code on my constructor class:
public class users {
int id;
String username, napel, nserie, fc, nc, du, ms, gp, pw;
public users(int id, String username, String napel, String nserie, String fc, String nc, String du, String ms,
String gp, String pw) {
super();
this.id = id;
this.username = username;
this.napel = napel;
this.nserie = nserie;
this.fc = fc;
this.nc = nc;
this.du = du;
this.ms = ms;
this.gp = gp;
this.pw = pw;
//the setters and getters are pretty straightforward
}
and here is the database related part of connection class :
public static ObservableList<users> getDatausers(){
Connection conn = ConnectDb();
ObservableList<users> list = FXCollections.observableArrayList();
try {
PreparedStatement ps = conn.prepareStatement("select * from users");
ResultSet rs = ps.executeQuery();
while (rs.next()){
list.add(new users(Integer.parseInt(rs.getString("id")), rs.getString("username"), rs.getString("napel"), rs.getString("nserie"), rs.getString("fc"), rs.getString("nc"), rs.getString("du"), rs.getString("ms"), rs.getString("gp"), rs.getString("pw")));
}
} catch (Exception e) {
}
return list;
}