Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 234587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:11:48+00:00 2026-05-11T20:11:48+00:00

I am writing a website using JSP, JSTL, Servlets and JavaBeans. At one point

  • 0

I am writing a website using JSP, JSTL, Servlets and JavaBeans.

At one point of my code, I am trying to use an ArrayList of objects, and a strange thing is happening: when I add the first object it is fine, and when I add a second object it adds it in the second place, but the object at index(0) gets the same values as the object at index(1).

Maybe a problem is in the

ArrayList<Article> articleList = new ArrayList<Article>();
Article newArticle = new Article();

Since articleList is ArrayList of Article class.

Can somebody point me to what I am doing wrong?

Below is my code:

public ArrayList<Article> getArticles()
{
    baseIO mySql = new baseIO();
    ArrayList<Article> articleList = new ArrayList<Article>();
    int articleId = 0;

    try
    {
        String sql =
            "select * from jsp_blog_article order by article_id Desc Limit 3";
        con = (Connection)mySql.getConnection();
        pstmt = (PreparedStatement) con.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            Article newArticle = new Article();
            newArticle.setArticleAuthor(rs.getString("article_name"));
            newArticle.setArticleBody(rs.getString("article_body"));
            newArticle.setArticleAuthor(rs.getString("article_author"));
            newArticle.setArticleDate(rs.getString("article_date"));
            articleId = Integer.parseInt(rs.getString("article_id"));
            newArticle.setArticleId(String.valueOf(articleId));
            newArticle.setArticleComments(this.getCommentsNum(articleId));
            articleList.add(newArticle);
        }
        con.close();
        pstmt.close();
    }
    catch(Exception e)
    {
        return null;
    }

    return articleList;
}

And the Article class

package objects;

import java.io.Serializable;

public class Article implements Serializable{
    private String articleName;
    private String articleBody;
    private String articleAuthor;
    private String articleComments;
    private String articleDate;
    private String articleId;

    public Article()
    {

    }

    // all the getters and setters in place, but it is too long
    // so i am not going to post them in forum

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:11:48+00:00Added an answer on May 11, 2026 at 8:11 pm

    I would try it this way and see what this does.

    int x = 0;
    while (rs.next()) {
        articleList.add(new Article());
        articleList.get(x).setArticleName(rs.getString("article_name"));
        articleList.get(x).setArticleBody(rs.getString("article_body"));
        articleList.get(x).setArticleAuthor(rs.getString("article_author"));
        articleList.get(x).setArticleDate(rs.getString("article_date"));
        articleList.get(x).setArticleId(rs.getString("article_id"));
        articleList.get(x).setArticleComments(this.getCommentsNum(articleId));
        x++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a website in JSP using Struts and Hibernate. I'm looking for a
I often use the website www.cplusplus.com as a reference when writing C code. I
So, if you are writing a website using Java and JSP's and didn't want
I'm writing a website using JSP. I want to have the website available in
I'm writing a website that will sell items from one of my classes. It
I'm writing a piece of code that requires the DOM of a website to
Conceptually speaking how would one go about writing an achievement system for a website
I'm writing a MVC website and using jqGrid. I have managed to get it
I'm using C# .NET 4 and MSSQL. I'm writing a code that downloads the
I am writing a website using Django. I need to push the web site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.