コード例 #1
0
  // Method for fetching all stores to drop down menu
  public void getStores() {

    // Local variables
    ResultSet rs;
    Statement stmt;
    String query = "SELECT namn FROM Butik ORDER BY namn ASC";

    try {

      stmt = con.createStatement();
      rs = stmt.executeQuery(query);

      while (rs.next()) {
        choiceBox2.getItems().add(rs.getString("namn"));
      }
      stmt.close();
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("Error fetching data");
    }
  }
コード例 #2
0
  // Method for displaying all available product types
  public void createStatement() {

    data = FXCollections.observableArrayList();

    // Local variables
    ResultSet rs;
    Statement stmt;
    String query = "SELECT namn FROM Produktgrupp ORDER BY namn ASC";

    try {

      stmt = con.createStatement();
      rs = stmt.executeQuery(query);

      // Clearing tables of old data
      tableview.getItems().clear();
      tableview.getColumns().clear();

      // Table columns added dynamically using ResultSet metadata.
      for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {

        // We are using non property style for making dynamic table
        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());
              }
            });

        tableview.getColumns().addAll(col);
        System.out.println("Column [" + i + "] ");
      }

      // Data added to ObservableList
      while (rs.next()) {

        // Iterate Row
        ObservableList<String> row = FXCollections.observableArrayList();
        for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {

          // Iterate Column
          row.add(rs.getString(i));
        }

        data.add(row);
      }

      // Data added to TableView
      tableview.setItems(data);

      // Close the variable stmt and release all resources bound to it
      // Any ResultSet associated to the Statement will be automatically closed too.
      stmt.close();
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("Error on Building Data");
    }
  }