首页 > 解决方案 > 用Sql中的第一个组合框字符串值填充第二个组合框

问题描述

我想ComboBox在 Query 中传递 First 的 String 值来填充 Second ComboBox,因此查询中的条件是这样的:SELECT NIT FROM ENTIDAD WHERE NOMBRE='+FirstComboboxStringValue+',所以我有两个方法,在第一个中填充 Name Value,在第二个中填充 Nit 值,我需要从该ComboBox查询中的第一个传递 Name 值,有以下方法:

public void llenadocombobox2() {
        Connection conn=null;
        try {
            ObservableList<String> listacombonombre= FXCollections.observableArrayList();
            String consulta = "select nombre from entidad";
            conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=GLOSASNINO", "sa", "123");
            PreparedStatement ps =conn.prepareStatement(consulta);
            ResultSet rs = ps.executeQuery();
             while ( rs.next() ) 
             {  
               listacombonombre.add(rs.getString("nombre"));
             }
          entidad.setItems(listacombonombre);  
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void llenadocombobox3() {
         llenadocombobox2();
        String FirstComboboxStringValue= entidad.getSelectionModel().getSelectedItem();
        Connection conn=null;
        try {
            ObservableList<String> listacombonit= FXCollections.observableArrayList();
            String consulta = "select nit from entidad where nombre='"+FirstComboboxStringValue+"'";
            conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=GLOSASNINO", "sa", "123");
            PreparedStatement ps =conn.prepareStatement(consulta);
             ResultSet rs = ps.executeQuery();
             while ( rs.next() ) 
             {  
               listacombonit.add(rs.getString("nit"));
             }

          nit.setItems(listacombonit);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

如果有人可以在这里给出方向可能会有所帮助。问候。

标签: javajavafx

解决方案


这是一个涵盖您的问题的演示。您应该根据第一个ComboBox选择查询数据库。使用查询结果填充第二个ComboBox. <- 这部分应该发生在第一个ComboBox valueProperty change侦听器中。

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ComboBoxExperiments extends Application
{

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        primaryStage.setTitle("ComboBox Experiment 1");

        ComboBox<String> comboBox = new ComboBox();
        comboBox.getItems().add("Choice 1");
        comboBox.getItems().add("Choice 2");
        comboBox.getItems().add("Choice 3");

        ComboBox<String> comboBox2 = new ComboBox();

        comboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
            comboBox2.getItems().setAll(queryFakeDB(newValue));
            comboBox2.getSelectionModel().selectFirst();
        });

        comboBox.getSelectionModel().selectFirst();
        comboBox2.getSelectionModel().selectFirst();

        VBox vBox = new VBox(comboBox, comboBox2);
        Scene scene = new Scene(vBox, 200, 120);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args)
    {
        Application.launch(args);
    }

    public List<String> queryFakeDB(String comboBox1Selection)
    {
        //Connect to fake database
        //Query fake database
        //Store results in a List
        //This simulates getting results from db based on combobox1 selection
        List<String> results = new ArrayList();
        switch (comboBox1Selection) {
            case "Choice 1":
                results.add("A");
                results.add("B");
                results.add("C");
                break;
            case "Choice 2":
                results.add("a");
                results.add("b");
                results.add("c");
                break;
            case "Choice 3":
                results.add("X");
                results.add("Y");
                results.add("Z");
                break;
        }

        //return result(s)
        return results;
    }
}

推荐阅读