以下是两个方法,假如数据库里有三条记录,为什么第一个查出的结果是正常的,而第二个查出的结果是三个重复的结果呢??
public ArrayList getDBISBN(){
ArrayList arr = new ArrayList();
try {
rs = st.executeQuery("select isbn from bookinfo");
while (rs.next()){
arr.add(rs.getString(1));
}
rs.close();
st.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return arr;
}
public ArrayList getBooksinfo(){
ArrayList arr = new ArrayList();
BooksActionForm baf = new BooksActionForm();
try {
rs = st.executeQuery("select * from bookinfo");
while (rs.next()){
baf.setTitle(rs.getString(1));
baf.setAuthor(rs.getString(2));
baf.setPublisher(rs.getString(3));
baf.setPages(rs.getInt(4));
baf.setPrice(rs.getFloat(5));
baf.setIsbn(rs.getString(6));
arr.add(baf);
}
rs.close();
st.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return arr;
}
public ArrayList getDBISBN(){
ArrayList arr = new ArrayList();
try {
rs = st.executeQuery("select isbn from bookinfo");
while (rs.next()){
arr.add(rs.getString(1));
}
rs.close();
st.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return arr;
}
public ArrayList getBooksinfo(){
ArrayList arr = new ArrayList();
BooksActionForm baf = new BooksActionForm();
try {
rs = st.executeQuery("select * from bookinfo");
while (rs.next()){
baf.setTitle(rs.getString(1));
baf.setAuthor(rs.getString(2));
baf.setPublisher(rs.getString(3));
baf.setPages(rs.getInt(4));
baf.setPrice(rs.getFloat(5));
baf.setIsbn(rs.getString(6));
arr.add(baf);
}
rs.close();
st.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return arr;
}
ArrayList arr = new ArrayList();
BooksActionForm baf = null;
try {
rs = st.executeQuery("select * from bookinfo");
while (rs.next()){
baf = new BooksActionForm();
baf.setTitle(rs.getString(1));
baf.setAuthor(rs.getString(2));
baf.setPublisher(rs.getString(3));
baf.setPages(rs.getInt(4));
baf.setPrice(rs.getFloat(5));
baf.setIsbn(rs.getString(6));
arr.add(baf);
}
rs.close();
st.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return arr;
}
lz的getBooksinfo方法中,就创建了一个BooksActionForm 对象,while循环中每次都是对同个对象进行的操作,所以结果是rs最后一条记录中的结果。