add data from the database to the combobox

0

Hi, I'm trying to select the data in the genre table with the query select sexo from genero in a combobox, but the combobox does not return anything, this is the code that I have which is the following.

public void llenadocombobox() {
        Connection conn=null;
        ObservableList <String> listacombo= FXCollections.observableArrayList();

        String consulta = "select sexo from genero";
        try {

            conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=prueba", "sa", "123");
            Statement ps =conn.createStatement();
             ResultSet rs =ps.executeQuery(consulta);

            while(rs.next()) {

                listacombo.add(rs.getString("sexo"));


            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        combo.getItems().addAll(listacombo);
    }

I declare a observablelist to save the data and then display them in the combobox. some help would be great. thanks.

    
asked by Jhon Mark 23.10.2018 в 23:48
source

1 answer

0

For this case this variable is not necessary:

ObservableList <String> listacombo= FXCollections.observableArrayList();

you can do the combobox update in a simple and dynamic way

// patrones es una instancia de ComboBox<String> patrones;
patrones.getItems().addAll("XML Clientes -> ",
                               "XML Proveedores ->",
                               "XML Complementos ->",
                               "XML SAT -> revisar"
                               );
    
answered by 26.11.2018 в 02:31