我正在使用FX和SQL进行Java项目。在这个Java项目中,我有一个商店,可以在其中向数据库添加产品。

现在,我也希望可以更新产品。我只能更新产品的价格,而不能更新品牌名称和类型名称。当我想更新品牌名称或类型名称时,在控制台中收到错误消息。
java - 如何解决错误“数据截断:第1行的id_brand列超出范围的值”?-LMLPHP

这是更新产品的代码:

 public int editProduct(Double price, int brand, int type,  int id) {
    String sql = "UPDATE products SET price=?, id_brand=?, id_type=? WHERE id=?";
    try {
        Class.forName(DRIVER);
        Connection con = DriverManager.getConnection(DB_URL, USER, PASS);
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setDouble(1, price);
        stmt.setInt(2, brand);
        stmt.setInt(3, type);
        stmt.setInt(4, id);
        int rows = stmt.executeUpdate();
        getProducts();
        con.close();
        return rows;
    } catch (ClassNotFoundException | SQLException ex) {
        System.out.println("SQL error updaten product");
        System.out.println(ex.getMessage());
    }
    return -1;
}


--

    public class EditProductController {

    @FXML TextField productId, productBrand, productType, productPrice;
    @FXML Label berichtMelding;
    @FXML Button saveButton;

    private StoreDataAccesObject storeDDA;

    public void fill (Product data) {
        berichtMelding.setText(Integer.toString(data.getId()));

        productType.setText(data.getType());
        productBrand.setText(data.getBrand());
        productPrice.setText(Double.toString(data.getPrice()));
    }

    public void updateProductButton() {
        storeDDA = new StoreDataAccesObject("noorderpoort", "toets");

        ArrayList<String> errorList = new ArrayList<>();
        String brand = productBrand.getText();
        String type = productType.getText();
        String price = productPrice.getText();
        String id = berichtMelding.getText();

        int idBrand;
        int idType;

        if (brand.trim().length() <= 0 || type.trim().length() <= 0 || price.trim().length() <= 0) {
            errorList.add("Alle velden moeten ingevuld zijn.");
        } else {

            if (storeDDA.nameExists("brands", brand) > 0) { //checking if brand exists in database.
                idBrand = storeDDA.nameExists("brands", brand);
            } else {
                idBrand = storeDDA.nameExists("brands", brand);
                if (idBrand < 0) {
                    errorList.add("Brand niet opgeslagen");
                }
            }

            if (storeDDA.nameExists("types", type) > 0) {
                idType = storeDDA.nameExists("types", type);
            } else {
                idType = storeDDA.nameExists("types", type);
                if (idType < 0) {
                    errorList.add("Type niet opgeslagen");
                }
            }

            if (storeDDA.editProduct(Double.parseDouble(price), idBrand, idType, Integer.parseInt(id)) <= 0) {
                berichtMelding.setText("Product niet geupdate.");
            } else {
                Stage editProduct = (Stage) saveButton.getScene().getWindow();
                editProduct.close();
            }
        }

        if (!errorList.isEmpty()) {
            String errorText = "";
            for (String error : errorList) {
                errorText += error;
            }
            berichtMelding.setText(errorText);
        }
    }
}


表格:
java - 如何解决错误“数据截断:第1行的id_brand列超出范围的值”?-LMLPHP

我希望有人可以帮助我解决这个问题,因为我已经坚持了两天。我自己也做了研究。

最佳答案

'数据截断:第1行'id_brand'列的值超出范围吗?


您要存储在id_brand中的数据不适合。例如,字符串可能太长或数字太大。

检查无符号id_brand列的值是否符合范围。

10-08 08:07
查看更多