如何将“结果集”一一加载到表视图中?

时间:2019-03-17 08:09:19

标签: java postgresql tableview out-of-memory resultset

我已经从数据库的5个表中加载了数百万行数据。我想将加载的数据显示到表格视图中。加载所有记录需要很长时间(有时它们都无法放入内存中)。我想要创建它,以便可以加载resultSet,显示它,加载第二个,等等。这可能吗?到目前为止,我的代码是:

@FXML
    private TableView<Vysledok_hladania> table;

@FXML
private TableColumn<Vysledok_hladania, String> Nazov_hotela, Adresa, Krajina, Mesto, Hviezdicky, Cena, Typ_izby, Pocet_izieb;

@FXML
    void vyhladajInfo(ActionEvent event){
        table.getItems().clear();
        try {
            Connection con = Runner.getConnection();
            con.setAutoCommit(false);
            String query = "SELECT h.\"Nazov hotela\", k.\"Nazov krajiny\", m.\"Nazov mesta\", h.\"Adresa hotela\", h.\"Hviezdicky\", p.\"Cena pobytu\", i.\"Typ izby\", h.\"Pocet izieb\" FROM hotel h " +
                    "inner  JOIN izba i ON i.\"ID hotela\" = h.\"ID hotela\" " +
                    "inner JOIN krajina k ON k.\"ID krajiny\" = h.\"ID krajiny\" " +
                    "inner JOIN mesto m ON m.\"ID mesta\" = h.\"ID mesta\" " +
                    "inner JOIN pobyt p ON p.\"ID hotela\" = h.\"ID hotela\" " +
                    "WHERE h.\"Nazov hotela\" = ? " +
                    "AND k.\"Nazov krajiny\" = ? " +
                    "AND h.\"Hviezdicky\" = ? " +
                    "AND i.\"Pocet posteli\" >= ? " +
                    "AND p.\"Cena pobytu\" <= ? ";
                    //"AND p.\"Datum od\" = ? " +
                    //"AND p.\"Datum do\" <= ?";

            int i = 1;

            PreparedStatement pst = con.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            pst.setFetchSize(50);
            pst.setString(1,Text_nazov.getText());
            pst.setString(2,(String)krajina.getValue());
            int pocet_hviezdiciek = Integer.parseInt((String)hviezdicky.getValue());
            pst.setInt(3,pocet_hviezdiciek);
            int pocet_osob = Integer.parseInt((String)osoby.getValue());
            pst.setInt(4, pocet_osob);
            double cena_pobytu = Double.parseDouble(Text_cena.getText());
            pst.setDouble(5, cena_pobytu);
//            Date datum_OD = Date.valueOf(date_od.getValue());
//            pst.setDate(6, datum_OD);
//            Date datum_DO = Date.valueOf(date_do.getValue());
//            pst.setDate(7, datum_DO);
            ResultSet rs = pst.executeQuery();
            System.out.println(pst);

            Nazov_hotela.setCellValueFactory(new PropertyValueFactory<>("Meno_hotela"));
            Krajina.setCellValueFactory(new PropertyValueFactory<>("Krajina"));
            Mesto.setCellValueFactory(new PropertyValueFactory<>("Mesto"));
            Adresa.setCellValueFactory(new PropertyValueFactory<>("Adresa"));
            Hviezdicky.setCellValueFactory(new PropertyValueFactory<>("Hviezdicky"));
            Cena.setCellValueFactory(new PropertyValueFactory<>("Cena"));
            Typ_izby.setCellValueFactory(new PropertyValueFactory<>("Typ_izby"));
            Pocet_izieb.setCellValueFactory(new PropertyValueFactory<>("Pocet_izieb"));


            while(rs.next()){
                oblist.add(new Vysledok_hladania(rs.getString("Nazov hotela"),
                        rs.getString("Nazov krajiny"),
                        rs.getString("Nazov mesta"),
                        rs.getString("Adresa hotela"),
                        rs.getInt("Hviezdicky"),
                        rs.getDouble("Cena pobytu"),
                        rs.getString("Typ izby"),
                        rs.getInt("Pocet izieb")));
                System.out.println(i++);
            }

            rs.close();
            pst.close();
            con.close();

        } catch(SQLException ex){
            Logger.getLogger(Result_Controller.class.getName()).log(Level.SEVERE, null, ex);
        }

        table.setItems(oblist);
    }

0 个答案:

没有答案