我在为我的评级系统进行验证时遇到了一些问题。所以基本上我要做的是一个用户只能为每个产品投票一次,要么投票,要么投票,我要做的是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方法的validate方法:
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,我选择了没有任何费率记录的产品。然而,它不起作用。我想知道我在哪里错误编码。希望我对我的问题的解释很清楚。
提前致谢。
答案 0 :(得分:0)
试试这个
String dbQuery = "SELECT * FROM sm_productrate WHERE productID =" + prodID +
" AND custName = '" + custName + "' AND (productRateUp = 1 OR productRateDown = 1)";