上面的代码是在一个DAO实现类中的 我的程序是在一个jsp页面中接收这个coll 
jsp的代码如下
Collection colle = (Collection) request.getAttribute("teaList");
Iterator it = colle.iterator(); while (it.hasNext()) {
TeacherVo tv = (TeacherVo) it.next();
String id = tv.getId();
String name = tv.getName();
String title = tv.getTitle();
String password = tv.getPassword();

解决方案 »

  1.   

    我认为问题出在将查询到的记录存放到集合类的部分就是dao实现类那 查询语句可能错误
    高手帮忙
      

  2.   

    你把代码改为如下试试:try{ psmt=connect.createStatement();
    rt=psmt.executeQuery(sql); 
    while(rt.next()){ tv.setId(rt.getString("id")); 
    tv.setName(rt.getString("name")); 
    tv.setTitle(rt.getString("title")); 
    tv.setPassword(rt.getString("password")); 
    coll.add(tv); 

      

  3.   

    public Collection showAllTeacher(TeacherVo tv){ 
    Collection coll =new ArrayList(); 
    sql ="select * from teacher"; 
    try{ psmt=jdbc.getConnection().prepareStatement(sql); 
    rt=psmt.executeQuery(); 
    while(rt.next()){ 
    TV tv = new TV();//==============================
    tv.setId(rt.getString("id")); 
    tv.setName(rt.getString("name")); 
    tv.setTitle(rt.getString("title")); 
    tv.setPassword(rt.getString("password")); 
    coll.add(tv); 


    就将你的TV实体类在循环内部创建
      

  4.   

    学习之。
    参考下:import java.util.*;class Teacher{
    private String id;
    private String name;

    public void setId(String id){
    this.id = id;
    }

    public void setName(String name){
    this.name = name;
    }

    public String getId(){
    return this.id;
    }

    public String getName(){
    return this.name;
    }
    }
    public class CsdnTest { public Collection<Teacher> getAll(Teacher[] t) {
    Collection<Teacher> col = new ArrayList<Teacher>();
    for(int i=0;i<t.length;i++){
    t[i] = new Teacher();
    t[i].setId(""+i);
    t[i].setName("Teacher's name");
    col.add(t[i]);
    }
    return col;
    } public static void main(String[] args) {
    CsdnTest test = new CsdnTest();
    Teacher[] teacher = new Teacher[12];
    Iterator<Teacher> it = test.getAll(teacher).iterator();
    while(it.hasNext()){
    Teacher t = it.next();
    System.out.println(t.getId() + "\t" + t.getName());
    }
    }
    }
      

  5.   

    new 的时候一定要在while循环里面new
      

  6.   

    public Collection showAllTeacher(TeacherVo tv){  
    Collection coll =new ArrayList();  
    sql ="select * from teacher";  
    try{  
    TV tv =null;//==================
    psmt=jdbc.getConnection().prepareStatement(sql);  
    rt=psmt.executeQuery();  
    while(rt.next()){  
    = new TV();//============================== 
    tv.setId(rt.getString("id"));  
    tv.setName(rt.getString("name"));  
    tv.setTitle(rt.getString("title"));  
    tv.setPassword(rt.getString("password"));  
    coll.add(tv);  
    }  
    }  在公司的前辈说 这样好点哦...
      

  7.   

    因为coll.add传入的是一个对象的引用地址!你用同一个TV不断循环!只是一个TV的值不断在改变!而coll中存储的都是同一个内存空间(也就是同一个tv对象)!所以只是一条记录!而且是最后一条记录!
      

  8.   

    public Collection showAllTeacher(TeacherVo tv){ 
    Collection coll =new ArrayList(); 
    sql ="select * from teacher"; 
    try{ psmt=jdbc.getConnection().prepareStatement(sql); 
    rt=psmt.executeQuery(); 
    while(rt.next()){ tv.setId(rt.getString("id")); 
    tv.setName(rt.getString("name")); 
    tv.setTitle(rt.getString("title")); 
    tv.setPassword(rt.getString("password")); 
    coll.add(tv); 


    最后一条记录是 
    004  王四 讲师 123; 加入数据库有四条记录,执行循环中所引用的变量tv 是同一个对象,所占有的的空间也是一样的,所以最后一次赋值后,会改变集合中前三条记录为最后一条记录,这个时候你需要 为每一个tv 分配一块不同的空间(new )就可以实现你想要得结果了。