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 に答える