Mysql 重复记录的验证

Mysql 重复记录的验证,mysql,validation,javafx,Mysql,Validation,Javafx,我在验证我的评级系统时遇到了一些问题。所以基本上我想做的是,每个产品只有一个用户可以投一次票,要么投赞成票,要么投反对票,我分三层做 基于每个productID和用户名验证投票记录的我的sql语句: public boolean validateRate(){ boolean result = false; ResultSet rs = null; DBController db = new DBController(); db.getConnection();

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

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

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登录,然后我选择了一个没有任何费率记录的产品。然而,它也不起作用。我想知道我在哪里编错了代码。希望我对我的问题的解释足够清楚

提前谢谢。

试试这个

String dbQuery = "SELECT * FROM sm_productrate WHERE productID =" + prodID + 
    " AND custName = '" + custName + "' AND (productRateUp = 1 OR productRateDown = 1)";