只通过arraylist获得最后一次循环

时间:2015-11-03 23:35:17

标签: java arraylist

好的,如果我打印出循环内的值,我将得到db的所有结果,但是当我尝试循环思考Driver类中的数组列表时,我只得到数组的最后一个值。
/ ************************** studentdata班********************* ********* /

 public ArrayList<Student> AllStudentdata() throws SQLException {
    Student student = null;
    ArrayList<Student> Studentlist = new ArrayList<Student>();
    String Sql = "CALL `studentsdb`.`SelectStudentsGarde`()";
    try {
        stat = con.createStatement();
        result = stat.executeQuery(Sql);
        while (result.next()) {
            student = new Student();
            student.setFristName(result.getString(1));
            student.setLastName(result.getString(2));       
            Studentlist.add(student);
        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return Studentlist;
}

/ **************************司机班****************** ************ /

public static void main(String[] args) {
    // TODO Auto-generated method stub

    ArrayList<Student> Studentlist=new ArrayList<Student>();

    Studentdata studata = new Studentdata();

    try {
        Studentlist=studata.AllStudentdata();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        System.out.println(e.getMessage());
    }

    System.out.println(Studentlist.size());


    for(int i = 0;i<Studentlist.size();i++)
    {




        System.out.println(Studentlist.get(i));
    }

}

1 个答案:

答案 0 :(得分:3)

在黑暗中拍摄,我没有调试这个,但你继续在你的循环中重置学生参考。试试这个:

thingObj