0

在对我的评级系统进行验证时,我遇到了一些问题。所以基本上我想做的是一个用户只能为每个产品投票一次,要么投赞成票,要么投反对票,我在 3 层做。

我的 sql 语句根据每个 productID 和用户名验证投票记录:

public boolean validateRate(){
    boolean result = false;
    ResultSet rs = null;
    DBController db = new DBController();
    db.getConnection();
    String dbQuery = "SELECT * FROM sm_productrate WHERE productID =" + prodID + " AND custName = '" + custName + "' AND productRateUp = 1 OR productRateDown = 1";
    rs = db.readRequest(dbQuery);
    try {
        if (rs.next()) {
            result = true;
        }else{
            result = false;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    db.terminate();
    return result;
 }

这是我的提交喜欢和不喜欢方法以及上面调用sql方法的验证方法:

 public boolean submitLike(CreateReviewAndRateUI panel,int row){
    String custName = panel.getUserLoggedLbl().getText();
    shopManagement.entity.Product product = new shopManagement.entity.Product(row, custName);
    boolean result = product.validateRate();
    if(result == true){
         Dialogs.showErrorDialog(null, "You have already voted once for this product", "Duplicate rate record found", ""); 
         result = true;
    }else{
        product.submitLike();
        result = false;
    }
    return result;
}

public boolean submitDislike(CreateReviewAndRateUI panel,int row){
    String custName = panel.getUserLoggedLbl().getText();
    shopManagement.entity.Product product = new shopManagement.entity.Product(row, custName);
    boolean result = product.validateRate();
    if(result == true){
        Dialogs.showErrorDialog(null, "You have already voted once for this product", "Duplicate rate record found", "");    
    }else{
        product.submitDislike(); 
    }       
    return result;
}

public boolean validateRate(CreateReviewAndRateUI panel, int row){
    String custName = panel.getUserLoggedLbl().getText();
    Product product = new Product(row, custName);
    boolean result = product.validateRate();
   return result;
}

这是当按钮点击时,它会相应地调用上面的方法。

@FXML
public void submitLike(ActionEvent event){
    int row = Integer.parseInt(getGetRowLbl().getText());
    CreateReviewAndRateController controller = new CreateReviewAndRateController();
    boolean result = controller.submitLike(myPane,row);

    if(!result){
        Dialogs.showInformationDialog(null, "Up vote has been successfully sunmitted",
                "Successful Submission", "");
        displayRate(row);
        getLikeBtn().setDisable(true);
        getDislikeBtn().setDisable(true);
    }
}

@FXML
public void submitDislike(ActionEvent event){
    int row = Integer.parseInt(getGetRowLbl().getText());
    CreateReviewAndRateController controller = new CreateReviewAndRateController();
    boolean result = controller.submitDislike(myPane,row);
     if(!result){
        Dialogs.showInformationDialog(null, "Down vote has been successfully sunmitted",
                "Successful Submission", "");
        displayRate(row);
        getLikeBtn().setDisable(true);
        getDislikeBtn().setDisable(true);
    }
}

然而,发生了一件非常奇怪的事情。例如,我第一个输入汇率的用户是 Dean。一切正常,它可以将费率记录添加到数据库中,可以毫无问题地进行验证。但是当我更改登录的用户时,我尝试在数据库中插入一个费率记录,它不起作用,它一直提示我发现重复记录错误消息,但实际上数据库中没有第二个用户的费率记录。然后,我再次以第一个用户 Dean 登录,并选择了一个没有任何费率记录的产品。然而,它也不起作用。我想知道我在哪里编码错误。希望我对我的问题的解释足够清楚。

提前致谢。

4

1 回答 1

0

尝试这个

String dbQuery = "SELECT * FROM sm_productrate WHERE productID =" + prodID + 
    " AND custName = '" + custName + "' AND (productRateUp = 1 OR productRateDown = 1)";
于 2013-07-06T05:29:13.083 回答