猿问

对象参数无法更新问题

for (int i = 0; i <5 i++) {

Items item = new Items();

System.out.println(Integer.parseInt(s1[i]));

stmt.setInt(1, Integer.parseInt(s1[i]));

rs = stmt.executeQuery();

while (rs.next()) {

item.setId(rs.getInt("id"));

item.setName(rs.getString("name"));

item.setCity(rs.getString("city"));

item.setNumber(rs.getInt("number"));

item.setPrice(rs.getInt("price"));

item.setPicture(rs.getString("picture"));

list.add(item);

}


当我把Items对象放到for循环上时(如下),获得的list对象里五个item都是一样的。为什么会这样啊。。。

                                        Items item = new Items();

for (int i = 0; i <s1.length-1; i++) {

System.out.println(Integer.parseInt(s1[i]));

stmt.setInt(1, Integer.parseInt(s1[i]));

rs = stmt.executeQuery();

while (rs.next()) {

item.setId(rs.getInt("id"));

item.setName(rs.getString("name"));

item.setCity(rs.getString("city"));

item.setNumber(rs.getInt("number"));

item.setPrice(rs.getInt("price"));

item.setPicture(rs.getString("picture"));

list.add(item);

}


iMcLaren
浏览 1587回答 1
1回答

qq_原来这就是来自北方的汉子_03305421

for(){while (rs.next()) { item = new Items();//....list.add(item);}}
随时随地看视频慕课网APP

相关分类

Java
我要回答