我在验证我的评级系统时遇到一些问题。所以基本上我想做的是一个用户只能为每个产品投票一次,要么投赞成票,要么投反对票,而我在 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登录,选择了一个没有任何费率记录的产品。然而,它的效果并不好。我想知道我哪里编码错误了。希望我对问题的解释足够清楚。
提前致谢。
最佳答案
试试这个
String dbQuery = "SELECT * FROM sm_productrate WHERE productID =" + prodID +
" AND custName = '" + custName + "' AND (productRateUp = 1 OR productRateDown = 1)";
关于mysql - 验证重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17499748/