求高手解释下面怎么会这样

Ricesky 发布于 2017/10/22 18:20
阅读 77
收藏 0

在数据库查询的list集合循环取出放到封装的bean中实体类 ,再将该bean放到新的list集合的时候,新list集合最后一个添加的bean会覆盖前面添加的bean 总是出现覆盖了  之前是能够正常显示数据库的信息的  请问知道怎么决解吗?

求大神们 给点建议 怎么改进  集合里面的先放进的数据不会被之后的覆盖掉。

代码如下 :

 List<ProfileDemo> profileDemos=new  ArrayList<ProfileDemo>();
 @Override
 public List<ProfileDemo> findAll() {
  ProfileDemo profileDemo=new ProfileDemo();
  String sql="select * from profile";
  rs=super.find(sql, null);
  try {
   while(rs.next()){
    
    profileDemo.setId(rs.getInt(1));
    profileDemo.setUserName(rs.getString(2));
    profileDemo.setBirthday(rs.getString(3));
    profileDemo.setGender(rs.getString(4));
    profileDemo.setCareer(rs.getString(5));
    profileDemo.setAddress(rs.getString(6));
    profileDemo.setMobile(rs.getString(7));
    profileDemos.add(profileDemo);
   }

 

 

 用system语句在控制台上验证:

List<ProfileDemo> profileDemos=new  ArrayList<ProfileDemo>();
 @Override
 public List<ProfileDemo> findAll() {
  ProfileDemo profileDemo=new ProfileDemo();
  String sql="select * from profile";
  rs=super.find(sql, null);
  try {
   while(rs.next()){
    
    profileDemo.setId(rs.getInt(1));
    profileDemo.setUserName(rs.getString(2));
    
    profileDemo.setBirthday(rs.getString(3));
    profileDemo.setGender(rs.getString(4));
    profileDemo.setCareer(rs.getString(5));
    profileDemo.setAddress(rs.getString(6));
    profileDemo.setMobile(rs.getString(7));
    System.out.println("DAO"+profileDemos.toString());
    
    
    System.out.println("验证执行代码"+profileDemo);
    
    profileDemos.add(profileDemo);
    System.out.println("添加到集合之后"+profileDemos);

控制台输出结果:

信息: Server startup in 3453 ms
====Base==根据id查询语句====
=====base===rs=oracle.jdbc.driver.OracleResultSetImpl@6c03833
验证执行代码ProfileDemo [id=1, userName=wangyang aaaa, birthday=2012-01-12, gender=man, career=mannager, address=长江南路180号, mobile=18818206477]
添加到集合之后[ProfileDemo [id=1, userName=wangyang aaaa, birthday=2012-01-12, gender=man, career=mannager, address=长江南路180号, mobile=18818206477]]
DAO[ProfileDemo [id=2, userName=jane, birthday=2008-05-02, gender=wom, career=cleaner, address=梅陇路180号, mobile=18818205787]]
验证执行代码ProfileDemo [id=2, userName=jane, birthday=2008-05-02, gender=wom, career=cleaner, address=梅陇路180号, mobile=18818205787]
添加到集合之后[ProfileDemo [id=2, userName=jane, birthday=2008-05-02, gender=wom, career=cleaner, address=梅陇路180号, mobile=18818205787], ProfileDemo [id=2, userName=jane, birthday=2008-05-02, gender=wom, career=cleaner, address=梅陇路180号, mobile=18818205787]]
DAO[ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014], ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014]]
验证执行代码ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014]
添加到集合之后[ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014], ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014], ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014]]
service[ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014], ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014], ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014]]
[ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014], ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014], ProfileDemo [id=3, userName=Curry, birthday=1992-01-23, gender=wom, career=analyst, address=长江南路180号, mobile=18818206014]]

 

 

 

 

加载中
0
卡卡嘎嘎嘎
Ricesky
Ricesky
谢谢 !
0
C
CallMain
建议你去学学java基础
Ricesky
Ricesky
我现在就是在学基础 ! 谢谢!
C
CallMain
创建对象放while里
0
原来qq名字可以起得这么长
原来qq名字可以起得这么长
怪不得1楼叫你学基础…list集合这样用…
Ricesky
Ricesky
嘿嘿 犯了错才能更加清楚的知道原理,了解自己的缺失啊! 太顺了也不一定见得好!脑力有限记不住太多犯错那也是经验啊!
0
Kit_lee
Kit_lee

一开始就学应用,不管基础,不是不行。但不要光踩坑,还要弄明白为什么掉坑里。建议百度一下java的对象和引用,不要光解决问题,不求甚解。

Ricesky
Ricesky
谢谢 你的建议 !
返回顶部
顶部