日期:2014-05-20 浏览次数:20639 次
public List<User> list(){ List<User> users=new ArrayList<User>(); Connection conn=this.getConnection(); ResultSet rs=null; String sql="select id,username,password,age,address from user"; PreparedStatement ps=this.prepare(conn, sql); User user=new User(); try{ rs=ps.executeQuery(); while(rs.next()){ user.setId(rs.getInt(1)); user.setUsername(rs.getString(2)); user.setPassword(rs.getString(3)); user.setAddress(rs.getString(4)); users.add(user); } }catch(Exception e){ e.printStackTrace(); } return users; }
public static void main(String[] args) { Main main=new Main(); List<User> allUser=main.list(); Iterator<User> iterator=allUser.iterator(); while(iterator.hasNext()){ User user=iterator.next(); System.out.println(user.getUsername()); } }
while(rs.next()){ user.setId(rs.getInt(1)); user.setUsername(rs.getString(2)); user.setPassword(rs.getString(3)); user.setAddress(rs.getString(4)); users.add(user); }