我从界面获取值并在控制器中处理它们。 字符串值成功保存到数据库,但无法保存整数值。
@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。