vulturistic vulturistic - 2 years ago 99
Java Question

Fetching all the array elements using

i'm having a problem fetching data from database. i'm trying to fetch the list of array. my problem is i can only get the last element of the array. maybe because it overwrite the previous one. here's my code.

String sql;
sql = "select * from NM_PROPERTIES_LOADER";
ResultSet rs = stmt.executeQuery(sql);
String prop_name = null;
String prop_value = null;
PropertyData reader = new PropertyData();

prop_name = rs.getString("prop_name");
prop_value = rs.getString("prop_value");
..//other setter..

System.out.println(prop_name + " " + prop_value);

In my sysout I can retrieve all the datas. but on my page only the last element is retrieved. Here's my dao class.

public class PropertyData {

private int id;
private String propName;
private String propValue;
private String engineName;
private String desc;
private Date createdAt;
private String createdBy;
private Date updatedAt;
private String updatedBy;

public int getId() {
return id;
public void setId(int id) { = id;
public String getPropName() {
return propName;
public void setPropName(String propName) {
this.propName = propName;
public String getPropValue() {
return propValue;
public void setPropValue(String propValue) {
this.propValue = propValue;
public String getEngineName() {
return engineName;
public void setEngineName(String engineName) {
this.engineName = engineName;
public String getDesc() {
return desc;
public void setDesc(String desc) {
this.desc = desc;
public Date getCreatedAt() {
return createdAt;
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
public String getCreatedBy() {
return createdBy;
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
public Date getUpdatedAt() {
return updatedAt;
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
public String getUpdatedBy() {
return updatedBy;
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;


Answer Source

In every iteration of the loop, you overwrite the content of reader. You want to create reader inside the loop, not outside, and also do prop.add(reader) inside the loop, not outside.

Like this:

 String sql = "select * from NM_PROPERTIES_LOADER";
 ResultSet rs = stmt.executeQuery(sql);

 while( {
     PropertyData reader = new PropertyData();
     String name = rs.getString("prop_name");
     String value = rs.getString("prop_value");

     // other setter calls..  

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download