gpt4 book ai didi

java - 就像数组列表中的计数器不断返回 0

转载 作者:行者123 更新时间:2023-12-01 14:52:56 25 4
gpt4 key购买 nike

我尝试对论坛主题进行类似的计数器。这是我的代码:

当点击like按钮时,它会将值插入数据库。

public void submitLike(ActionEvent e) {
int count = 0;
int newCount = count + 1;
eForumLikeDislike amount = new eForumLikeDislike(userName,topicId,newCount);
amount.likeCounter();
}

我插入的SQL语句就像点击时一样。

 public void SetUpLikeAmount() {
int likes = 0;
ArrayList <Integer> likeArray = new ArrayList <Integer>();
for (int count = 0; count < likeArray.size();count++){
// Set Up Database Source
db.setUp("IT Innovation Project");
String sql = "Select likeDislike_likes from forumLikeDislike WHERE topic_id = "
+ topicId + "";
ResultSet resultSet = null;
// Call readRequest to get the result
resultSet = db.readRequest(sql);
try {
while (resultSet.next()) {
likeArray.add(Integer.parseInt(resultSet.getString("likeDislike_likes")));
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}
likes += likeArray.get(count);
}
jLabel_like.setText(Integer.toString(likes));
}

我尝试显示某个线程的总点赞数的方法。

public void SetUpLikeAmount() {
int likes = 0;
ArrayList <Integer> likeArray = new ArrayList <Integer>();
for (int count = 0; count < likeArray.size();count++) {
// Set Up Database Source
db.setUp("IT Innovation Project");
String sql = "Select likeDislike_likes from forumLikeDislike WHERE topic_id = "
+ topicId + "";
ResultSet resultSet = null;
// Call readRequest to get the result
resultSet = db.readRequest(sql);
try {
while (resultSet.next()) {
likes = Integer.parseInt(resultSet.getString("likeDislike_likes"));
likes += likeArray.get(count);
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}

}
jLabel_like.setText(Integer.toString(likes));
}

但是,金额一直返回0。有什么问题吗?我该如何修复它?
提前致谢。

最佳答案

如果您能够更改数据库设计,请这样做。具体来说,从表forumLikeDislike 中删除likeDislike_likes 字段。

完成后(或者即使没有),您可以替换此查询,

Select likeDislike_likes 
from forumLikeDislike
WHERE topic_id = the topicId variable you submit

使用此查询:

select count(*) likes
from forumLikeDislike
WHERE topic_id = the topicId variable you submit

它将返回一行,其中包含您想要的答案。

关于java - 就像数组列表中的计数器不断返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14662967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com