像数组列表中的计数器一样返回0

时间:2013-02-02 14:37:20

标签: java sql arraylist

我尝试为论坛帖子做类似的计数器。这是我的代码:

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

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

我在点击时插入的SQL statement

 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.有什么不对吗?我该如何解决? 提前谢谢。

2 个答案:

答案 0 :(得分:1)

如果您能够更改数据库设计,请执行此操作。具体来说,从表forumLikeDislike中删除类似Dislike_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

它将返回一行并提供您想要的答案。

答案 1 :(得分:0)

我不是100%肯定这些观点,因为我没有尝试过这些代码,但是我想让你检查一下这些代码:

  1. 获取数据

    ArrayList <Integer> likeArray = new ArrayList <Integer>(); for (int count = 0; count < likeArray.size();count++)

  2. 您的likeArray大小始终从零开始。所以,这意味着计数已经小于0,因此我认为它甚至不会进入循环......你检查过了吗?

    1. 为什么这样做:

      int count = 0;
      int newCount = count + 1;
      eForumLikeDislike amount = new eForumLikeDislike(userName,topicId,newCount);
      amount.likeCounter();
      
    2. 您的newCount将始终为1.您可以避免count = 0; newCount = count + 1,只有一行直接显示

          eForumLikeDislike amount = new eForumLikeDislike(userName,topicId, 1);
      

      或者你应该得到eForumLikeDislike,然后有一个addLike()方法,用username和topicId将它递增1。

      希望有所帮助。 干杯