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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:00:35+00:00 2026-06-13T05:00:35+00:00

To add different objects from the database to an ArrayList this is the code

  • 0

To add different objects from the database to an ArrayList this is the code that I use:

try {
    Statement stat = con.createStatement();
    ResultSet rs = stat.executeQuery("SELECT DISTINCT Id, Username, FirstName, LastName FROM USER WHERE Username LIKE '%"+name+"%' or FirstName LIKE '%"+name+"%' or LastName LIKE '%"+name+"%'");
    while (rs.next()){
        finded.setUsername(rs.getString("Username"));
        finded.setFirstName(rs.getString("FirstName"));
        finded.setLastName(rs.getString("LastName"));
        finded.setId(rs.getInt("Id"));
        findList.add(finded);
        Log.d("this is the content of the List:",((Integer) findList.get(j).getId()).toString());
        Log.d("this is the content of the List 0:",((Integer) findList.get(0).getId()).toString());

    }
    con.close();

And this is what the Log.d gives me each time.

10-18 08:31:39.407: D/this is the content of the List:(427): 3
10-18 08:31:39.407: D/this is the content of the List 0:(427): 3
10-18 08:31:39.427: D/this is the content of the List:(427): 15
10-18 08:31:39.427: D/this is the content of the List 0:(427): 15
10-18 08:31:39.437: D/this is the content of the List:(427): 13
10-18 08:31:39.437: D/this is the content of the List 0:(427): 13
10-18 08:31:39.447: D/this is the content of the List:(427): 50
10-18 08:31:39.447: D/this is the content of the List 0:(427): 50
10-18 08:31:39.460: D/this is the content of the List:(427): 34
10-18 08:31:39.460: D/this is the content of the List 0:(427): 34
10-18 08:31:39.467: D/this is the content of the List:(427): 49
10-18 08:31:39.467: D/this is the content of the List 0:(427): 49
10-18 08:31:39.479: D/this is the content of the List:(427): 53
10-18 08:31:39.479: D/this is the content of the List 0:(427): 53

As you can see it copies the same object in all the position of the ArrayList. I tried also to do it with findList.add(index, finded) (index is an int who is incrementing) But I get the same result.

  • 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-13T05:00:35+00:00Added an answer on June 13, 2026 at 5:00 am

    You need to create a new object for findedinside the loop

    while (rs.next()){
        finded = new Finded();   // <<-- here
        finded.setUsername(rs.getString("Username"));
    

    otherwise you just add the same object over and over again

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a different question concerning: add a connection to database not working, asp.net
When I create a view different from index action (for example the action video/add)
I am retrieving number of values from database, that need to be added to
Every time I fetch objects from the database I get all objects. Now, I
Referencing the code below, I have a list of Error objects that has been
I'm working on an iPhone app that gets a number of objects from a
My understanding of Hibernate is that as objects are loaded from the DB they
I am having a problem inserting and removing objects to/from the database using Linq
I need to add a JNDI datasource from a legacy database to my Grails
How can I add different markers to google map v3? Here is example for

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.