我想在查询中传递第一个ComboBox
的字符串值以填充第二个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();
}
}
如果任何人都可以在这里进行介绍会很有帮助。问候。
答案 0 :(得分:0)
这是一个涵盖您的问题的演示。您应该基于第一个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;
}
}