我创建了一个带有场景构建器的表,在那里定义了我的所有列,所以我现在尝试从我的数据库中填充列,所有我得到的是我的数据库中的第一行,当显示时,所有其他值都被复制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();
}
}
答案 0 :(得分:1)
试试这个。可能会有所帮助。
@FXML private void SearchButton()
{
Connection c ;
datamem = FXCollections.observableArrayList();
try
{
c = Dao.getCon();
String SQL =SELECT * FROM `Member`;
ResultSet rs = c.createStatement().executeQuery(SQL);
if(table.getColumns().isEmpty())
{
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++)
{
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
table.getColumns().addAll(col);
}//for
}//if
while(rs.next())
{
ObservableList<String> row = FXCollections.observableArrayList();
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++)
{
row.add(rs.getString(i));
}// for
datamem.add(row);
}//while
table.setItems(datamem);
}//try
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Problem in Search Button "+e);
}
}//else
}//else
} //search method
答案 1 :(得分:0)
新代码
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>>() {
@Override
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>>() {
@Override
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>>() {
@Override
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>>() {
@Override
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>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(4).toString());
}
});
//availableSpace column
availableSpaceColumn.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(5).toString());
}
});
allRouteTable.getColumns().setAll(fromColumn, toColumn, dateOfDepartureColumn, timeOfDepartureColumn, priceColumn, availableSpaceColumn);
while (resultSet.next()) {
//This is the correction made defining the routes object after while loop
routes = FXCollections.observableArrayList();
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();
}
finally {
try {
connection.close();
getAllStatement.close();
resultSet.close();
} catch (SQLException er) {
er.printStackTrace();
}
}
}