Javafx:清除 SQLite 查询的结果集,因此它不会保留以前的结果
Javafx: Clearing a ResultSet for SQLite query so it won't keep previous results
我正在使用组合框 1 中的选定值,并使用 运行 基于该选择的 SQLite 查询来使用结果集填充组合框 2。查询没问题,但由于它是同一个结果集,所以它会在结果集中保留以前的条目。
有没有办法每次都清除结果集,从而不提取我不想要的数据?
控制器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);
}
}
}
示例结果:
操作 1:ComboBox 1 选择 1 提取 2 个结果。 ComboBox 2 显示 2 个结果。
操作 2:ComboBox 2 选择 2 拉出 3 个结果。 ComboBox 显示操作 1 结果和操作 2 结果。
想要的结果:
操作 1:ComboBox 1 选择 1 提取 2 个结果。 ComboBox 2 显示 2 个结果。
操作 2:ComboBox 2 选择 2 拉出 3 个结果。 ComboBox 显示 3 个结果。
您看到的结果是因为您从未从 ComboBox
中删除任何项目。
您应该每次都清除 ComboBox
以实现此功能。
类似于
flavourOne.getItems().clear();
这将删除所有以前的条目,并保留 flavourOne.getItems().add...
的基本功能
我正在使用组合框 1 中的选定值,并使用 运行 基于该选择的 SQLite 查询来使用结果集填充组合框 2。查询没问题,但由于它是同一个结果集,所以它会在结果集中保留以前的条目。
有没有办法每次都清除结果集,从而不提取我不想要的数据?
控制器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);
}
}
}
示例结果:
操作 1:ComboBox 1 选择 1 提取 2 个结果。 ComboBox 2 显示 2 个结果。
操作 2:ComboBox 2 选择 2 拉出 3 个结果。 ComboBox 显示操作 1 结果和操作 2 结果。
想要的结果:
操作 1:ComboBox 1 选择 1 提取 2 个结果。 ComboBox 2 显示 2 个结果。
操作 2:ComboBox 2 选择 2 拉出 3 个结果。 ComboBox 显示 3 个结果。
您看到的结果是因为您从未从 ComboBox
中删除任何项目。
您应该每次都清除 ComboBox
以实现此功能。
类似于
flavourOne.getItems().clear();
这将删除所有以前的条目,并保留 flavourOne.getItems().add...