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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:31:31+00:00 2026-06-16T16:31:31+00:00

I have following java class package com.picvik.model; import java.util.Date; public class ViewAlbum { private

  • 0

I have following java class

package com.picvik.model;

import java.util.Date;

public class ViewAlbum {

private Integer albumid;
private String albumname;
private String description;
private String location;
private Date date;
private Integer uid;

public Integer getAlbumid() {
    return albumid;
}
public void setAlbumid(Integer albumid) {
    this.albumid = albumid;
}
public String getAlbumname() {
    return albumname;
}
public void setAlbumname(String albumname) {
    this.albumname = albumname;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public String getLocation() {
    return location;
}
public void setLocation(String location) {
    this.location = location;
}
public Date getDate() {
    return date;
}
public void setDate(Date date) {
    this.date = date;
}
public Integer getUid() {
    return uid;
}
public void setUid(Integer uid) {
    this.uid = uid;
}

}

I am retrieving data from db and adding it to my array list like this

public ArrayList getAllAlbums(Integer uid) {
    ViewAlbum album = new  ViewAlbum();
    ArrayList<ViewAlbum>allAlbums = new ArrayList<ViewAlbum>();
    try {
        String qstring = "SELECT albumid, albumname, description, location," +
                " date, uid FROM picvik_picture_album WHERE " +
                "uid = '" + uid + "';";

        System.out.println(qstring);
        connection = com.picvik.util.MySqlConnection.getInstance().getConnection();
        ptmt = connection.prepareStatement(qstring);
        resultSet = ptmt.executeQuery();
        while(resultSet.next()) {
            //System.out.println(resultSet.getString("albumname"));
            album.setAlbumid(resultSet.getInt("albumid"));
            album.setAlbumname(resultSet.getString("albumname"));
            album.setDescription(resultSet.getString("description"));
            album.setLocation(resultSet.getString("location"));
            album.setDate(resultSet.getDate("date"));
            album.setUid(resultSet.getInt("uid"));
            allAlbums.add(album);
        }

        resultSet.close();
        ptmt.close();
        connection.close();


    } catch (Exception e) {
        e.printStackTrace();
    }   
    return allAlbums;
}

But when I am trying to print the values stored in array list. Its always giving me the last inserted record.

<div class="row">
                <div class="span10">
                    <s:iterator value="allAlbums">
                        <s:property value="albumname"/>
                    </s:iterator>   
                </div>
            </div>
  • 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-16T16:31:33+00:00Added an answer on June 16, 2026 at 4:31 pm

    Here,

    ViewAlbum album = new ViewAlbum();
    // ...
    
    while (resultSet.next()) {
        album.setAlbumid(resultSet.getInt("albumid"));
        // ...
        allAlbums.add(album);
    }
    

    you’re reusing the very same album instance for all records. The instance’s data get overridden everytime in the loop. The list does not contain copies of the instance, but it contains copies of the reference to the single instance. You know, Java is Object Oriented.

    You should be creating a new album instance per record. Move the instantiation to inside the loop.

    // ...
    
    while (resultSet.next()) {
        ViewAlbum album = new ViewAlbum();
        album.setAlbumid(resultSet.getInt("albumid"));
        // ...
        allAlbums.add(album);
    }
    

    See also:

    • How can I pass an Integer class correctly by reference?

    Unrelated to the concrete problem, you should be closing JDBC resources in the finally block, or be opening them in the try() try-with-resources statement, otherwise they will still leak away in case of an exception during executing the query or processing the result set. You should also move the declarations of JDBC resources to inside the method block, otherwise you’ll run into threadsafety issues as well. Last but not least, you should use the setter methods of PreparedStatement to set user-controlled variables in a SQL string. If they were strings, you’d have a SQL injection attack hole.

    See also:

    • Java Iterator backed by a ResultSet
    • JDBC MySql connection pooling practices to avoid exhausted connection pool
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple Java code: package testj; import java.util.*; public class Query<T>
i have the following action class: package com.pendulum.web; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest;
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
i have the following piece of code : package com.example.task; import java.util.List; import android.os.Bundle;
I have the following class in Java package com.artifex.mupdf.data; public class FzTextSpan { FzRect
I've got following java class. package com.org.data.dbresource; import org.springframework.orm.ibatis.SqlMapClientTemplate; public class DBConnectionManager { private
Say I have the following simple java bean: class MyBean { private Date startDate;
I have written the following Java source file ( Hello.java ): package com; public
I have done the following modifications in my code: ConnectionPool.java: package com.dao; import java.sql.*;
I have the following Java class: package web; import javax.servlet.*; import javax.servlet.http.*; import java.io.*;

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.