Search code examples
sqlitejavafxresultset

Javafx: Clearing a ResultSet for SQLite query so it won't keep previous results


I am taking the selected value in combobox 1 and using running a SQLite query based on that selection to populate combobox 2 using a result set. The query is fine but as it's the same resultset it keeps previous entries in the resultset.

Is there a way to clear the resultset each time to not pull through data I don't want?

Controller Class

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UpdateInventoryController implements Initializable {
    @FXML ComboBox brandOne;
    @FXML ComboBox flavourOne;

    String brandsInInventory;
    String flavoursInInventory;
    String selectedBrand = "";

    private static Connection con;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        populateBrandCombos();

        brandOne.getSelectionModel().selectedItemProperty().addListener( (options, oldValue, newValue) -> {
                    selectedBrand = String.valueOf(newValue);
                    if (selectedBrand != "") {
                        populateFlavourCombos();
                    }
                }
        );
    }

    public void populateBrandCombos() {
        try {
            con = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\PengStation420\\IdeaProjects\\PengVapour\\PVIM.sqlite");

            ResultSet rs = con.createStatement().executeQuery("SELECT " +
                    "DISTINCT Brand " +
                    "FROM Concentrates");

            while (rs.next()) {
                brandsInInventory = rs.getString("Brand");
                brandOne.getItems().addAll(brandsInInventory);
                brandTwo.getItems().addAll(brandsInInventory);
                brandThree.getItems().addAll(brandsInInventory);
                brandFour.getItems().addAll(brandsInInventory);
                brandFive.getItems().addAll(brandsInInventory);
                brandSix.getItems().addAll(brandsInInventory);
            }
        } catch (SQLException ex) {
            Logger.getLogger(InventoryController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @FXML
    public void populateFlavourCombos() {
        selectedBrand = brandOne.valueProperty().getValue().toString();

        try {
            con = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\PengStation420\\IdeaProjects\\PengVapour\\PVIM.sqlite");

            ResultSet rs = stmt.executeQuery("SELECT " +
                    "DISTINCT Flavour " +
                    "FROM Concentrates " +
                    "WHERE Concentrates.Brand " +
                    "LIKE '%" + selectedBrand + "%'");

            int rows = 0;
            while (rs.next()) {
                flavoursInInventory = rs.getString("Flavour");
                flavourOne.getItems().addAll(flavoursInInventory);
                rows++;
            }
            flavourOne.setVisibleRowCount(rows); // set new visibleRowCount value
        } catch (SQLException ex) {
            Logger.getLogger(InventoryController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Example Result:

Action 1: ComboBox 1 selection 1 pulls through 2 results. ComboBox 2 displays 2 reuslts.

Action 2: ComboBox 2 selection 2 pulls through 3 results. ComboBox displays Action 1 results and Action 2 results.

Desired result:

Action 1: ComboBox 1 selection 1 pulls through 2 results. ComboBox 2 displays 2 reuslts.

Action 2: ComboBox 2 selection 2 pulls through 3 results. ComboBox displays 3 results.


Solution

  • The results you are seeing happen because you never remove any items from the ComboBox.

    You should clear the ComboBox each time to achieve this functionality.

    Something similar to

    flavourOne.getItems().clear();
    

    This will remove all previous entries, and keep your basic functionality with flavourOne.getItems().add...