我用场景构建器创建了一个表,在那里定义了我的所有列,所以我现在试图从我的数据库中填充列,我得到的只是我数据库中的第一行,当显示在 tableView 上时,所有其他值都被复制。请我需要帮助,我似乎无法找到我所缺少的。
public void populateTable() {
final String username = "root";
final String password = "joshua";
final String bd_url = "jdbc:mysql://localhost:3306/Bus_billing_system?zeroDateTimeBehavior=convertToNull";
try {
connection = DriverManager.getConnection(bd_url, username, password);
getAllStatement = connection.createStatement();
resultSet = getAllStatement.executeQuery("select * from RouteDB");
//from column in table
fromColumn.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(0).toString());
}
});
//to column in table
toColumn.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(1).toString());
}
});
//date of departure column in table
dateOfDepartureColumn.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(2).toString());
}
});
//time of departure column in table
timeOfDepartureColumn.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(3).toString());
}
});
//price column
priceColumn.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(4).toString());
}
});
allRouteTable.getColumns().setAll(fromColumn, toColumn, dateOfDepartureColumn, timeOfDepartureColumn, priceColumn);
while (resultSet.next()) {
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
routes.add(resultSet.getString(i));
}
data.add(routes);
}
allRouteTable.setItems(data);
} catch (SQLException e) {
e.printStackTrace();
}
}