public List<Category> getCategoryList() throws SQLException {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {conn = DbUtil.getConnection();String sql = "select * from category";ps = conn.prepareStatement(sql);rs = ps.executeQuery(sql);List<Category> categoryList = new ArrayList<Category>();Category c = new Category();while (rs.next()) {c.setcId(rs.getInt("cId"));System.out.println("ci Dao===="+rs.getInt("cId"));c.setName(rs.getString("name"));c.setDescribes(rs.getString("describes"));c.setContentTime(rs.getString("contentTime"));categoryList.add(c);}System.out.println("categoryList.get(0).getCid()=="+categoryList.get(0).getcId());//输出3System.out.println("categoryList.get(1).getCid()=="+categoryList.get(1).getcId());//输出3 和上面的一样 ???????return categoryList;} finally {DbUtil.close(rs, ps);}}
2 回答
慕婉清6462132
TA贡献1804条经验 获得超2个赞
把Category c = new Category();写到循环里面去就好了!
while (rs.next()) {
Category c = new Category();
c.setcId(rs.getInt("cId"));
System.out.println("ci Dao===="+rs.getInt("cId"));
c.setName(rs.getString("name"));
c.setDescribes(rs.getString("describes"));
c.setContentTime(rs.getString("contentTime"));
categoryList.add(c);
}
白板的微信
TA贡献1883条经验 获得超3个赞
Category c = new Category();
这一句放到while循环里面去
否则只创建了一个对象
所有的操作都是对这一个对象进行的
所以只有最后一次循环生效了
必须在每次循环的时候new一个对象才可以
- 2 回答
- 0 关注
- 71 浏览
添加回答
举报
0/150
提交
取消