java中的ArrayList打印最后插入的值? [英] ArrayList in java printing last inserted value?

查看:20
本文介绍了java中的ArrayList打印最后插入的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下java类

package com.picvik.model;导入 java.util.Date;公共类 ViewAlbum {私有整数专辑;私人字符串专辑名称;私人字符串描述;私人字符串位置;私人日期;私人整数 uid;公共整数 getAlbumid() {返回白蛋白;}公共无效setAlbumid(整数专辑){this.albumid = 白蛋白;}公共字符串 getAlbumname() {返回专辑名称;}公共无效setAlbumname(字符串专辑名){this.albumname = 专辑名;}公共字符串 getDescription() {退货说明;}public void setDescription(字符串描述){this.description = 描述;}公共字符串 getLocation() {返回地点;}公共无效设置位置(字符串位置){this.location = 位置;}公共日期 getDate() {归期;}公共无效设置日期(日期日期){this.date = 日期;}公共整数 getUid() {返回uid;}公共无效 setUid(整数 uid){this.uid = uid;}}

我正在从数据库中检索数据并将其添加到我的数组列表中

public ArrayList getAllAlbums(Integer uid) {ViewAlbum专辑=新的ViewAlbum();ArrayListallAlbums = new ArrayList();尝试 {String qstring = "选择专辑名称、专辑名称、描述、位置," +日期,uid 来自 picvik_picture_album WHERE" +"uid = '" + uid + "';";System.out.println(qstring);连接 = com.picvik.util.MySqlConnection.getInstance().getConnection();ptmt = connection.prepareStatement(qstring);结果集 = ptmt.executeQuery();while(resultSet.next()) {//System.out.println(resultSet.getString("专辑名"));专辑.setAlbumid(resultSet.getInt("albumid"));专辑.setAlbumname(resultSet.getString("专辑名"));专辑.setDescription(resultSet.getString("description"));专辑.setLocation(resultSet.getString("location"));专辑.setDate(resultSet.getDate("date"));专辑.setUid(resultSet.getInt("uid"));allAlbums.add(专辑);}结果集.close();ptmt.close();connection.close();} 捕获(异常 e){e.printStackTrace();}返回所有专辑;}

但是当我尝试打印存储在数组列表中的值时.它总是给我最后插入的记录.

<div class="span10"><s:property value="专辑名"/></s:迭代器>

解决方案

这里,

ViewAlbum 专辑 = new ViewAlbum();//...而(resultSet.next()){专辑.setAlbumid(resultSet.getInt("albumid"));//...allAlbums.add(专辑);}

您正在为所有记录重用完全相同的 album 实例.实例的数据每次在循环中都会被覆盖.该列表不包含实例的副本,但包含对单个实例的引用 的副本.您知道,Java 是面向对象的.

您应该为每条记录创建一个新的 album 实例.将实例化移动到循环内部.

//...而(resultSet.next()){ViewAlbum专辑=新的ViewAlbum();专辑.setAlbumid(resultSet.getInt("albumid"));//...allAlbums.add(专辑);}

另见:

<小时>

与具体问题无关,您应该在 finally 块中关闭 JDBC 资源,或者在 try() 中打开它们try-with-resources 语句,否则在执行查询或处理结果集的过程中,如果出现异常,它们仍然会泄漏.您还应该将 JDBC 资源的声明移动到方法块内部,否则您也会遇到线程安全问题.最后但并非最不重要的一点是,您应该使用 PreparedStatement 的 setter 方法在 SQL 字符串中设置用户控制的变量.如果它们是字符串,就会有 SQL 注入攻击漏洞.

另见:

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,

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:


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中的ArrayList打印最后插入的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆