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 7870149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:32:00+00:00 2026-06-03T01:32:00+00:00

I’ve got this user class (this is a reduced version for the example, the

  • 0

I’ve got this user class (this is a reduced version for the example, the real one has more parameters but they are implemented the same way):

public class User {

    private int _userID;
    private String _fullName;

    public User(){

    }

    public User(int userID, String fullName){
        this._userID = userID;

        this._fullName = fullName;
    }

    int getUserID(){
        return this._userID;
    }

    String getFullName(){
        return this._fullName;
    }
    void setUserID(int userID){
        this._userID = userID;
    }

    void setFullName(String fullName){
        this._fullName = fullName;
    }
}

And I want to retrieve a list from my MS SQL Server of this type of objects in Android, I’m using this method inside the connector helper class (the class in charge to make connection to the server using JDBC):

public List<User> getUsers(int ID){

        java.sql.ResultSet result = null;
        List<User> users = new ArrayList<User>();
        User user = new User();
        try {
             connection = this.getConnection();
             if (connection != null) {

                    //QUERY
                    String statement = "SELECT * Users WHERE groupID = " 
                                    + ID;
                    Statement select = connection.createStatement();

                    //Calls Query
                    result = select.executeQuery(statement);
                    while (result.next()){
                        user.setUserID(result.getInt("UserID"));
                        user.setFullName(result.getString("FullName"));
                        System.out.println(result.getString("FullName"));

                        //Adds to the list
                        users.add(user);

                    }


                    result.close();
                    result = null;
                    closeConnection();
             } 
             else {
                    System.out.println("Error: No active Connection");
             }
         }  catch (Exception e) {
             e.printStackTrace();
            }

        return users;
    }

The data is retrieved well from the server according to the System.out.println I’m using in every iteration of the while, the problem is that the list is always filled with repeated information about the last user I retrieve, to clarify:

If I got users A, B and C, when I read user A, list has this structure:[A], when I read user B, list is:[B,B], when I read user C: [C,C,C], etc. So basically all the objects in the list are being overwritten by the last one read.

I’ve been struggling with this for hours, hope someone can spot the problem because I can’t, thanks in advance for the help.

  • 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-06-03T01:32:01+00:00Added an answer on June 3, 2026 at 1:32 am

    You instantiate a single User object before your loop, and then modify the same User object at each iteration. So you end up with N times the same User object added to your list. You must recreate a new User at each iteration:

    while (result.next()){
        User user = new User();
        user.setUserID(result.getInt("UserID"));
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I need to clean up various Word 'smart' characters in user input, including but
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.