解析Class类型的Arraylist

时间:2017-11-02 20:03:40

标签: java arraylist

我使用arraylist来保存数据库中的结果集数据。我的结果集包含四行。我使用对象类型的arraylist。我能够将数据保存到arraylist。 但是当我尝试从arraylist打印数据时,它正在打印保存的最后一个对象的数据。但是当我尝试打印arraylist时,它正在打印四个不同的对象。请帮我打印arraylist数据。

我的Servlet:

List<FMB_BOOKS> books = FMB_BOOKS.getBooks();                        
System.out.println("List from Books: " + books);
PrintWriter out = response.getWriter();         
Iterator<FMB_BOOKS> itr = books.iterator();
FMB_BOOKS results = new FMB_BOOKS();                      
out.println(books.toString());
for (FMB_BOOKS book : books) 
{
    System.out.println("Book ID from Books: " + book.getBookid());
    out.println(book.getBookid());
    out.println(book.getBookname());
    out.println(book.getAuthorname());
    out.println(book.getCategory());
    out.println(book.getLanguage());
    out.println(book.getYear());
    out.println(book.getPublisher());
    out.println(book.getCaption());
    out.println(book.getImage());               
}

我的Java类:

while (rs.next())
{
    FMB_BOOKS Obj_Books = new FMB_BOOKS();              
    System.out.println("Inside While Result Set");              
    FMB_BOOKS.setBookid(rs.getString("BOOK_ID"));
    FMB_BOOKS.setBookname(rs.getString("BOOK_NAME"));
    FMB_BOOKS.setAuthorname(rs.getString("AUTHOR_NAME"));
    FMB_BOOKS.setCaption(rs.getString("CAPTION"));
    FMB_BOOKS.setCategory(rs.getString("CATEGORY"));
    FMB_BOOKS.setYear(rs.getString("PUBLISHED_YEAR"));
    FMB_BOOKS.setPublisher(rs.getString("PUBLISHER"));
    FMB_BOOKS.setLanguage(rs.getString("LANGUAGE"));
    FMB_BOOKS.setImage(rs.getString("IMAGE"));        
    books.add(Obj_Books);

}

0 个答案:

没有答案