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>
Here,
you’re reusing the very same
albuminstance 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
albuminstance per record. Move the instantiation to inside the loop.See also:
Unrelated to the concrete problem, you should be closing JDBC resources in the
finallyblock, or be opening them in thetry()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 ofPreparedStatementto set user-controlled variables in a SQL string. If they were strings, you’d have a SQL injection attack hole.See also: