Inserting data to JavaFX TableView without intermediate class

后端 未结 2 2052
故里飘歌
故里飘歌 2021-01-13 01:36

I\'m newbie Java programmer. I would like to insert ResultSet data to JavaFX TableView, but i wouldn\'t use intermediate class. Can i insert ResultSet rows to TableView rows

2条回答
  •  青春惊慌失措
    2021-01-13 02:30

    You need something to hold the data; that something (which you call the "intermediate class") is going to be the data type for your TableView.

    It doesn't necessarily have to be a class you create, but if you just use something general, your code is going to be much harder to understand. For example, you could just use a List to hold each row:

    TableView> unitsTableView = new TableView<>();
    idUnit = new TableColumn, String>("Id");
    idUnit.setCellValueFactory(new Callback, String>, ObservableValue>() {
        @Override
        public ObservableValue call(CellDataFeatures, String>> data) {
            return new ReadOnlyStringWrapper(data.getValue().get(0)) ;
        }
    });
    shortNameUnit = new TableColumn, String>("Short Name");
    shortNameUnit.setCellValueFactory(new Callback, String>, ObservableValue>() {
        @Override
        public ObservableValue call(CellDataFeatures, String>> data) {
            return new ReadOnlyStringWrapper(data.getValue().get(1)) ;
        }
    });
    nameUnit = new TableColumn, String>("Name");
    nameUnit.setCellValueFactory(new Callback, String>, ObservableValue>() {
        @Override
        public ObservableValue call(CellDataFeatures, String>> data) {
            return new ReadOnlyStringWrapper(data.getValue().get(2)) ;
        }
    });
    

    and then

    public static ObservableList> getUnits() {
    
        final ObservableList> data = FXCollections.observableArrayList();
        if (openConnection()) {
            try {
                rs = st.executeQuery("SELECT * FROM units");
                while (rs.next()) {
                    List unit = new ArrayList<>();
                    unit.add(rs.getString("id_unit"));
                    unit.add(rs.getString("short_name"));
                    unit.add(rs.getString("name"));
                    data.add(unit);
                }
            } catch (Exception ex) {
                Logger.getLogger(SQLConnect.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        closeConnection();
        return data;
    }
    

    But now your code is almost completely devoid of the semantics of the data you are representing.

    Note that you can't use a ResultSet directly, for a number of reasons. One is that the data in the TableView are represented by a random-access list. While you can think of ResultSet as representing a list of rows of data, it doesn't implement the List interface, and doesn't have any random-access functionality (there's no guaranteed way to get row number 5, etc.). Another reason is that a ResultSet requires (or at least may require, depending on the JDBC driver implementation) a live database resource. So you would risk locking your database table for the entire time the TableView is in scope.

    Why do you want to avoid creating the data representation class?

提交回复
热议问题