0

首先,我将给出我正在处理的代码。

“保存”按钮的操作代码。

    private void saveBtActionPerformed(java.awt.event.ActionEvent evt) {                                       
    try {
        Production production=new Production(batchNoValueLabel.getText(), productIDCombo.getSelectedItem(), rawMaterialUsedCombo.getSelectedItem(), dateValueLabel.getText(), rawMaterialBatchCombo.getSelectedItem(), weightInitialSpinner.getValue(), beforeWeightSpinner.getValue(), afterWeightSpinner.getValue(), finalWeightSpinner.getValue(), packingWeightSpinner.getValue(), noOfUnitSpinner.getValue(), wastageSpinner.getValue());
        int res=ProductionController.addBatch(production);
            if(res==1){
                JOptionPane.showMessageDialog(this, "New Batch Added!");
                String nextid = IDGeneration.getNextid("B", "production", "productionBatchID");
                batchNoValueLabel.setText(nextid);  
                productIDCombo.removeAllItems();
                ArrayList<String> getProductsDetails = ProductsController.getProductID();
                for (String detail : getProductsDetails) {
                    productIDCombo.addItem(detail);
                }                    
            }else{
                JOptionPane.showMessageDialog(this, "New Product Adding Failed!");
            }
    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(ProductsForm.class.getName()).log(Level.SEVERE, null, ex);
    }
}

然后,模型类。(Getter 和 Setter 都有,这里就不贴了)

    public Production(String productionBatchID, Object finishedMaterialID, Object rawMaterialID, String productionDate, Object rawMatBatchID, Object initialWeight, Object beforeWeight, Object afterWeight, Object finalWeight, Object packingWeight, Object noOfUnits, Object wastage) {
    this.productionBatchID = productionBatchID;
    this.finishedMaterialID = (String) finishedMaterialID;
    this.rawMaterialID = (String) rawMaterialID;
    this.productionDate = productionDate;
    this.rawMatBatchID = (String) rawMatBatchID;
    this.initialWeight = (int) initialWeight;
    this.beforeWeight = (int) beforeWeight;
    this.afterWeight = (int) afterWeight;
    this.finalWeight = (int) finalWeight;
    this.packingWeight = (int) packingWeight;
    this.noOfUnits = (int) noOfUnits;
    this.wastage = (int) wastage;
}

最后是控制器类。

public class ProductionController {

public static int addBatch(Production production) throws ClassNotFoundException, SQLException {
    Connection conn=DBConnection.getConnection();
    Statement stm=conn.createStatement();
    String sql="insert into production (productionBatchID, finishedMaterialID, rawMaterialID, productionDate, rawMatBatchID, initialWeight, beforeWeight, finalWeight, packingWeight, noOfUnits, wastage) values ('"+production.getProductionBatchID()+"','"+production.getFinishedMaterialID()+"','"+production.getRawMaterialID()+"','"+production.getProductionDate()+"','"+production.getRawMatBatchID()+"','"+production.getInitialWeight()+"','"+production.getBeforeWeight()+"','"+production.getAfterWeight()+"','"+production.getFinalWeight()+"','"+production.getPackingWeight()+"','"+production.getNoOfUnits()+"','"+production.getWastage()+"');";
    int rowCount=stm.executeUpdate(sql);
    return rowCount;
}

}

而且,当我在 GUI 中输入数据并尝试将数据发送到数据库时,它会发出“java.sql.SQLException:列计数与第 1 行的值计数不匹配”错误。如何解决这个问题?哦,这是 MySQL 表:

create table production(productionBatchID VARCHAR(4) NOT NULL,finishedMaterialID VARCHAR(4) NOT NULL,rawMaterialID VARCHAR(4) NOT NULL,productionDate VARCHAR(15),rawMatBatchID VARCHAR(4),initialWeight INT(5),beforeWeight INT(5),afterWeight INT(5),finalWeight INT(5),packingWeight INT(5),noOfUnits INT(5),wastage INT(5),CONSTRAINT PRIMARY KEY (productionBatchID))ENGINE=INNODB;

如何解决这个问题?

4

2 回答 2

1

您的 VALUES 子句有

'"+production.getAfterWeight()+"','"+production.getFinalWeight()+

但是您在列列表中只有 finalWeight 。

错误告诉你这个

insert into production (
    productionBatchID, finishedMaterialID, 
    rawMaterialID, productionDate, 
    rawMatBatchID, initialWeight, 
    beforeWeight, finalWeight, 
    packingWeight, noOfUnits, 
    wastage)
values (
    '"+production.getProductionBatchID()+"','"+production.getFinishedMaterialID()+"','"+
    production.getRawMaterialID()+"','"+production.getProductionDate()+"','"+
    production.getRawMatBatchID()+"','"+production.getInitialWeight()+"','"+
    production.getBeforeWeight()+"','"+
                   production.getAfterWeight()+"','"+
                             production.getFinalWeight()+"','"+  there are 3 here
    production.getPackingWeight()+"','"+production.getNoOfUnits()+"','"+
    production.getWastage()+"');";

此外,请参数化您的查询以减轻 SQL 注入风险

于 2013-05-15T14:01:47.787 回答
0

您在 INSERT 查询中使用的列数不匹配,“beforeWeight”是缺少值的列,下面是更正后的查询:

String sql="insert into production (productionBatchID, finishedMaterialID, rawMaterialID, productionDate, rawMatBatchID, initialWeight, **beforeWeight**, finalWeight, packingWeight, noOfUnits, wastage) values ('"+production.getProductionBatchID()+"','"+production.getFinishedMaterialID()+"','"+production.getRawMaterialID()+"','"+production.getProductionDate()+"','"+production.getRawMatBatchID()+"','"+production.getInitialWeight()+"','"+production.getBeforeWeight()+"','"+production.getAfterWeight()+"','"+production.getBeforeWeight()+"','"+production.getFinalWeight()+"','"+production.getPackingWeight()+"','"+production.getNoOfUnits()+"','"+production.getWastage()+"');";
于 2013-05-15T14:05:04.253 回答