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

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

问题描述

我有以下的Java类

 包com.picvik.model;进口java.util.Date;公共类ViewAlbum {私人整数ALBUMID;
私人字符串ALBUMNAME;
私人字符串描述;
私人字符串位置;
私人日期日期;
私人整数UID;公共整数getAlbumid(){
    返回ALBUMID;
}
公共无效setAlbumid(整数ALBUMID){
    this.albumid = ALBUMID;
}
公共字符串getAlbumname(){
    返回ALBUMNAME;
}
公共无效setAlbumname(字符串ALBUMNAME){
    this.albumname = ALBUMNAME;
}
公共字符串getDescription(){
    返回描述;
}
公共无效setDescription(字符串描述){
    this.description =描述;
}
公共字符串的getLocation(){
    返回的位置;
}
公共无效的setLocation(字符串位置){
    this.location =位置;
}
公开日期GETDATE(){
    归期;
}
公共无效的setDate(日期为准){
    this.date =日期;
}
公共整数的getuid(){
    返回UID;
}
公共无效的setuid(整数UID){
    this.uid = UID;
}}

我从数据库检索数据并将其添加到我的数组列表,像这样

 公开的ArrayList getAllAlbums(整数UID){
    ViewAlbum专辑=新ViewAlbum();
    ArrayList的< ViewAlbum> allAlbums =新的ArrayList< ViewAlbum>();
    尝试{
        字符串的QString =SELECT ALBUMID,ALBUMNAME,说明,位置,+
                到目前为止,从UID WHERE picvik_picture_album+
                UID ='+ UID +;        的System.out.println(QString的);
        连接= com.picvik.util.MySqlConnection.getInstance()的getConnection()。
        PTMT =连接prepareStatement(QString的)。
        的resultSet = ptmt.executeQuery();
        而(resultSet.next()){
            //System.out.println(resultSet.getString(\"albumname));
            album.setAlbumid(resultSet.getInt(ALBUMID));
            album.setAlbumname(resultSet.getString(ALBUMNAME));
            album.setDescription(resultSet.getString(说明));
            album.setLocation(resultSet.getString(位置));
            album.setDate(resultSet.getDate(DATE));
            album.setUid(resultSet.getInt(UID));
            allAlbums.add(册页);
        }        resultSet.close();
        ptmt.close();
        connection.close()时;
    }赶上(例外五){
        e.printStackTrace();
    }
    返回allAlbums;
}

但是,当我试图打印存储在数组列表中的值。它总是给我的最后一个插入的记录。

 < D​​IV CLASS =行>
                < D​​IV CLASS =span10>
                    < S:迭代器值=allAlbums>
                        < S:属性值=ALBUMNAME/>
                    < / S:迭代器>
                < / DIV>
            < / DIV>


解决方案

在这里,

  ViewAlbum专辑=新ViewAlbum();
// ...而(resultSet.next()){
    album.setAlbumid(resultSet.getInt(ALBUMID));
    // ...
    allAlbums.add(册页);
}

您打算重用的所有记录非常相同的专辑实例。该实例的数据在环获得覆盖每次。该名单不包含实例的副本,但它包含的参考应用于单个实例的副本。要知道,Java是面向对象的。

您应该创建一个新的专辑每条记录实例。移动实例化的循环中。

  // ...而(resultSet.next()){
    ViewAlbum专辑=新ViewAlbum();
    album.setAlbumid(resultSet.getInt(ALBUMID));
    // ...
    allAlbums.add(册页);
}

参见:


无关的具体问题,您应该在最后块被关闭JDBC资源,或者在<$ C $打开它们C>尝试()尝试-与资源的语句,否则他们将仍然执行查询或处理结果集时漏走在异常情况下。你也应该移动JDBC资源的声明的方法块内,否则你会碰到threadsafety问题为好。最后但并非最不重要的,你应该使用 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:

这篇关于ArrayList的在java中印最后一个插入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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