Question
Rewrite Listing 35.1, SQLClient.java, to display the SQL commands result in a TableView, as shown in the following figures. For example, as shown below, a
Rewrite Listing 35.1, SQLClient.java, to display the SQL commands result in a TableView, as shown in the following figures. For example, as shown below, a user can type two SQL commands like select and Insert statements. Please note that the new inserted record should be displayed in the TableView when Execute SQL Command button is clicked. (Note: for the TableView class, refer to Ch. 31). In addition, you need to re-write the clear button to clear the TableView contents as shown in the figure below. Hint: In your solution, please use populateTableView method provided below.
private void populateTableView(ResultSet rs, TableView tableView) { //empty the table view from the content of the previous statement tableView.getColumns().clear(); ObservableList data = FXCollections.observableArrayList(); try { for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) { final int j = i; TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1)); col.setCellValueFactory(TextFieldTableCell.forTableColumn()); col.setCellValueFactory(new Callback,ObservableValue>() { @Override public ObservableValue call(CellDataFeaturesparam) { if (param == null || param.getValue() == null || param.getValue().get(j) == null) { return null; } return new SimpleStringProperty(param.getValue().get(j).toString()); } }); tableView.getColumns().addAll(col); } while (rs.next()) { ObservableList row = FXCollections.observableArrayList(); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { row.add(rs.getString(i)); } System.out.println("Row [1] added " + row); data.add(row); } tableView.setItems(data); } catch (Exception e) { e.printStackTrace(); System.out.println("Error on Building Data"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started