I try to do the like counter for forum threads. This is my code:
When like button is on click, it will insert the value into database.
public void submitLike(ActionEvent e) {
int count = 0;
int newCount = count + 1;
eForumLikeDislike amount = new eForumLikeDislike(userName,topicId,newCount);
amount.likeCounter();
}
The SQL statement which I insert like when it is on click.
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));
}
The method which I try to display the total likes of certain thread.
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));
}
However, the amount keep returning me 0. Is there something wrong? How can I fix it?
Thanks in advance.
If you are able to change your database design, do so. Specifically, drop the field likeDislike_likes from table forumLikeDislike.
Once you have done that (or even if you don’t), you can replace this query,
with this query:
It will return one row with the answer you want.