简体   繁体   English

java中的ArrayList打印最后插入的值?

[英]ArrayList in java printing last inserted value?

I have following java class我有以下java类

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,这里,

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.您正在为所有记录重复使用相同的album实例。 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.您知道,Java 是面向对象的。

You should be creating a new album instance per record.您应该为每条记录创建一个新album实例。 Move the instantiation to inside the loop.将实例化移动到循环内部。

// ...

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

See also:也可以看看:


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.具体问题无关,你应该在finally块中关闭JDBC资源,或者在try() try-with-resources语句中打开它们,否则在执行查询或处理过程中出现异常时它们仍然会泄漏结果集。 You should also move the declarations of JDBC resources to inside the method block, otherwise you'll run into threadsafety issues as well.您还应该将 JDBC 资源的声明移动到方法块内部,否则您也会遇到线程安全问题。 Last but not least, you should use the setter methods of PreparedStatement to set user-controlled variables in a SQL string.最后但并非最不重要的一点是,您应该使用PreparedStatement的 setter 方法在 SQL 字符串中设置用户控制的变量。 If they were strings, you'd have a SQL injection attack hole.如果它们是字符串,你就会有一个 SQL 注入攻击漏洞。

See also:也可以看看:

You have only one instance of ViewAlbum and you are playing(setting the values) only with that single instance throughout the loop.您只有一个 ViewAlbum 实例,并且您在整个循环中仅使用该单个实例进行播放(设置值)。 So after completition of loop you have only one object inserted into ArrayList for N(Size of Resultset) no of times.因此,在循环完成后,您只有一个对象插入到 ArrayList 中 N(结果集的大小)次数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM