Get a value of type int in ComboBox JavaFX

I would like to retrieve a value in a combobox which is of type int in order to perform an insert in an sql table thanks to a query. However, I can't do it, does anyone have a solution?

Here is my ComboBox and I try to get the value in "DBUtils.ajouterPersonnelMembre" :

package eu.hautil.pigeonnier;

import java.net.URL;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;



public class APMCheckBoxController implements Initializable {

    @FXML
    private ComboBox id_role;

    @FXML
    private TextField nom;

    @FXML
    private TextField prenom;

    @FXML
    final DatePicker date_arrivee = new DatePicker(LocalDate.now());

    @FXML
    final DatePicker date_depart = new DatePicker(LocalDate.now());

    /*final DatePicker date_depart = new DatePicker(LocalDate.now());
    Instant instant_depart = date_depart.toInstant();
    LocalDate localDateDepart = instant_depart.atZone(ZoneId.systemDefault()).toLocalDate();*/

    @FXML
    private Button submit_button;

    // Drop Down Menu
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        ObservableList<Integer> list = FXCollections.observableArrayList(1, 2, 3, 4, 5, 6);
        id_role.setItems(list);

        // End of the Drop Down Menu

        submit_button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                DBUtils.ajouterPersonnelMembre(event, id_role.getItems(), nom.getText(), prenom.getText(),  date_arrivee, date_depart);
            }
        });

    }
}

And my function "ajouterPersonnelMembre" :

public static void ajouterPersonnelMembre(ActionEvent event, int id_role, String nom, String prenom, DatePicker date_arrivee, DatePicker date_depart) throws SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/le_pigeonnier", "root", "root");
            preparedStatement = connection.prepareStatement("INSERT INTO personnel(id_role, nom, prenom, date_arrivee, date_depart) VALUES (?,?,?,?,?)");
            preparedStatement.setInt(1, id_role);
            preparedStatement.setString(2, nom);
            preparedStatement.setString(3, prenom);
            preparedStatement.setDate(4, date_arrivee);
            preparedStatement.setDate(5, date_depart);

            resultSet = preparedStatement.executeQuery();

            if(resultSet != null){
                System.out.println("Membre du personel bien ajouté !");
                changeScene(event, "/fxml/menu.fxml", "", "");
                resultSet.close();
            } else{
                System.out.println("Une erreur est survenue, le membre du personnel n'a pas pu être ajouté");
                changeScene(event, "/fxml/sample.fxml", "", "");
                resultSet.close();
            }

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

Thanks in advance ;)


Solution 1:

Make sure you use the generics not the raw type: Instead of:

@FXML
private ComboBox id_role;

use

@FXML
private ComboBox<Integer> id_role;

or better when following the Java naming conventions:

use

@FXML
private ComboBox<Integer> idRole;

and likely you want to call something like idRole.getValue() instead of idRole.getItems().