0

我用场景构建器创建了一个表,在那里定义了我的所有列,所以我现在试图从我的数据库中填充列,我得到的只是我数据库中的第一行,当显示在 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();
    }
}
4

2 回答 2

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
于 2014-08-13T07:08:56.547 回答
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();
        }
    }

}
于 2013-07-29T19:39:14.917 回答