Получение значений TextField в базу данных как тип int - JavaFx

Я получаю значения из интерфейса и обрабатываю их в контроллере. Строковые значения успешно сохраняются в базе данных, но целочисленные значения не могут быть сохранены.

@FXML
private TextField productId;
@FXML
private TextArea resultArea;
@FXML
private TextArea description;
@FXML
private TextField newUnitPrice;
@FXML
private TextField title;
@FXML
private TextField newQuantity;
@FXML
private TextField unitPrice;
@FXML
private ChoiceBox<String> type;
@FXML
private TextField quantity;

ObservableList<String> productType = FXCollections.observableArrayList("Food","Construction","Electric","Grocery");

int unitPriceInt;
int quantityInt;
int newUnitPriceInt;
int productIdInt;
int newQuantityInt;


public void initComponents() {
    this.unitPriceInt = Integer.parseInt(unitPrice.getText());
    this.quantityInt = Integer.parseInt(quantity.getText());
    this.newUnitPriceInt = Integer.parseInt(newUnitPrice.getText());
    this.newUnitPriceInt = Integer.parseInt(newQuantity.getText());
    this.productIdInt = Integer.parseInt(productId.getText());

}

//Insert a product to the DB
@FXML
private void insertProduct(ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
    try {
        ProductDAO.insertProduct(title.getText(), type.getSelectionModel().getSelectedItem(), description.getText(), unitPriceInt, quantityInt);
        resultArea.setText("Product inserted! \n");
    } catch (SQLException e) {
        resultArea.setText("Problem occurred while inserting product item" + e);
        throw e;
    }
}

Даже если строки преобразуются в тип Integer, значения не связаны с переменными. Они сохраняются как 0.

0 ответов

Другие вопросы по тегам