Java ArrayList正在运行

Java ArrayList正在运行,java,database,arraylist,Java,Database,Arraylist,我遇到了一个奇怪的问题。我有一个数据库与6个不同的“出价”。我正在尝试使用以下代码提取投标: public ArrayList<ProjectBid> getProjectBids(String projectID) { ArrayList<ProjectBid> result = new ArrayList<ProjectBid>(); ProjectBid bid = new ProjectBid();

我遇到了一个奇怪的问题。我有一个数据库与6个不同的“出价”。我正在尝试使用以下代码提取投标:

public ArrayList<ProjectBid> getProjectBids(String projectID)
    {
        ArrayList<ProjectBid> result = new ArrayList<ProjectBid>();

        ProjectBid bid = new ProjectBid();

        try{

        st = con.createStatement();
        String query = "SELECT * FROM bids WHERE projectID=\""+projectID+"\"";
        System.out.println(query);
        ResultSet rs = st.executeQuery(query);


        while (rs.next())
          {
            bid.setProjectID(projectID);
            bid.setBidID(rs.getString("bidID"));
            bid.setBidderEmail(rs.getString("bidder_email"));
            bid.setBidAmount(rs.getInt("bid_amount"));
            bid.setBidMessage(rs.getString("bid_message"));
            bid.setTimestamp(rs.getString("timestamp"));

            result.add(bid);
          }
          st.close();

        }
        catch(Exception ex){
            System.out.println("Exception: "+ex);
        }

        return result;          
    }
public ArrayList getProjectBids(字符串projectID)
{
ArrayList结果=新建ArrayList();
ProjectBid=新的ProjectBid();
试一试{
st=con.createStatement();
String query=“从projectID=\”“+projectID+\”“”的投标中选择*;
System.out.println(查询);
结果集rs=st.executeQuery(查询);
while(rs.next())
{
bid.setProjectID(projectID);
bid.setbidd(rs.getString(“bidID”);
bid.setBidderEmail(rs.getString(“投标人电子邮件”);
投标金额(卢比整(“投标金额”);
bid.setBidMessage(rs.getString(“bid_message”);
bid.setTimestamp(rs.getString(“timestamp”);
结果。添加(投标);
}
圣克洛斯();
}
捕获(例外情况除外){
System.out.println(“异常:+ex”);
}
返回结果;
}
出于某种原因,我的ArrayList结果为我提供了6个相同的“bid”对象,而不是分别从数据库中添加每个“bids”。如果我在while循环中添加
“System.out.println(bid.getBidID);”
,代码将按预期在结果集中运行,并且正确打印每次运行的正确值。谁能告诉我我的代码有什么问题吗

谢谢

添加

ProjectBid bid = new ProjectBid();
while
循环中。到目前为止,您只需覆盖相同的
ProjectBid
对象。因此,
ArrayList
中的每个引用都指向循环中不断更改的相同对象值。因此,最后您将在
ArrayList
中拥有所有元素,其数据与从数据库检索到的最后一行相同

    while (rs.next())
      {
        ProjectBid bid = new ProjectBid();
        bid.setProjectID(projectID);
        bid.setBidID(rs.getString("bidID"));
        bid.setBidderEmail(rs.getString("bidder_email"));
        bid.setBidAmount(rs.getInt("bid_amount"));
        bid.setBidMessage(rs.getString("bid_message"));
        bid.setTimestamp(rs.getString("timestamp"));

        result.add(bid);
      }

您应该移动
ProjectBid=newprojectbid()在while循环中。使用当前代码,您正在更新相同的
bid
变量,该变量在
while
循环外部定义,因此它在循环完成后获得最后一条记录的值。

因为对bid的引用始终相同,它将用当前值覆盖出价,从而更改结果列表中的所有出价,为您提供最后添加的出价

在循环中实例化bid对象,这应该可以正常工作

while (rs.next())
  {
    ProjectBid bid = new ProjectBid();
    ...
    ...
    ...
    result.add(bid);
  }

然后,我将为数据库中的每一行创建一个新的“bid”对象。如果我有1000次出价,这将如何影响我的内存使用?覆盖同一个对象不应该同样好吗?@user3586514:不。首先,您将使用当前方法覆盖现有数据(这个答案是正确的);其次,每个新的
ProjectBid
对象的生命周期只持续到循环结束;每个人都有资格进行垃圾收集。不,不会。All
add()
method会添加对所传递对象的引用。正如我前面在循环末尾所说的,所有数组列表引用都将指向同一个对象。您需要单独的对象来保存不同的数据库行条目。@user3586514您可以在循环外定义变量,但必须在循环内使用
new
运算符重新分配它。@user3586514不应该这样做,您已经注意到它不起作用。是的,它会影响你的记忆。。。一点点别在乎这个。