I am just trying to see if I am able to get data from MS Access into a table using a TableView in FXML, but none of the data from my table in MS Access appears in the TableView. However, if I display just the content of my table as a message dialogue, all the correct information appears. Please can someone show me where I am going wrong.
I watched this Youtube video 1, and followed it step by step, as well as other sources, but nothing fixes my issue.
@FXML
private TableView<modelTable> table;
@FXML
private TableColumn<modelTable, StringProperty> colStudentName;
@FXML
private TableColumn<modelTable, StringProperty> colSurname;
@FXML
private TableColumn<modelTable, IntegerProperty> colGrade;
@FXML
private TableColumn<modelTable, StringProperty> colEmail;
ObservableList<modelTable> list = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
Connection connection = DBConnect.getConnection();
ResultSet rs = connection.createStatement().executeQuery("SELECT * FROM data");
while (rs.next()) {
//If I run the JOptionPane on its own, all the data is displayed
//JOptionPane.showMessageDialog(null, (rs.getString("StudentName")+ "\t" + rs.getString("Surname") + "\t" + rs.getInt("Grade") + "\t" + rs.getString("Email")));
list.add(new modelTable(rs.getString("StudentName"), rs.getString("Surname"), rs.getInt("Grade"), rs.getString("Email")));
}
} catch (SQLException ex) {
Logger.getLogger(TableController.class.getName()).log(Level.SEVERE, null, ex);
}
colStudentName.setCellValueFactory(new PropertyValueFactory<>("StudentName"));
colSurname.setCellValueFactory(new PropertyValueFactory<>("Surname"));
colGrade.setCellValueFactory(new PropertyValueFactory<>("Grade"));
colEmail.setCellValueFactory(new PropertyValueFactory<>("Email"));
table.setItems(list);
}
}
public class modelTable {
private StringProperty colName, colSurname, colEmail;
private IntegerProperty colGrade;
public modelTable(String colName, String colSurname, int colGrade, String colEmail) {
this.colName = new SimpleStringProperty(colName);
this.colSurname = new SimpleStringProperty(colSurname);
this.colEmail = new SimpleStringProperty(colEmail);
this.colGrade = new SimpleIntegerProperty(colGrade);
}
public String getColName() {
return colName.get();
}
public void setColName(String colName) {
this.colName.set(colName);
}
public String getColSurname() {
return colSurname.get();
}
public void setColSurname(String colSurname) {
this.colSurname.set(colSurname);
}
public String getColEmail() {
return colEmail.get();
}
public void setColEmail(String colEmail) {
this.colEmail.set(colEmail);
}
public int getColGrade() {
return colGrade.get();
}
public void setColGrade(int colGrade) {
this.colGrade.set(colGrade);
}
}
public class DBConnect {
public static Connection getConnection() throws SQLException{
Connection connection = DriverManager.getConnection("jdbc:ucanaccess://C://Users//Huzaifah//Desktop//School work 2019//testing table.accdb");
return connection;
}
I expect the data to be in the TableView when I run the program, but it's empty. No error message is given, only an empty table.
I tried using this same database in just a simple program without a TableView and it works perfectly fine.