日期:2014-05-17 浏览次数:20997 次
public ArrayList<User> select(String sql){ ArrayList<User> list = new ArrayList<User>(); Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while(rs.next()){ //将rs结果集中的每一条记录都转化成User对象,再放进list中 User user = new User(); user.setTel(rs.getString("tel")); user.setUsername(rs.getString("username")); user.setAddr(rs.getString("addr")); user.setMail(rs.getString("mail")); user.setType(rs.getInt("type")); user.setYf(rs.getDouble("yf")); user.setNum(rs.getInt("num")); user.setYb(rs.getInt("yb")); user.setFp(rs.getInt("fp")); user.setPrice(rs.getDouble("price")); user.setNum(rs.getInt("num")); user.setId(rs.getString("id")); user.setFk(rs.getInt("fk")); list.add(user); } } catch (SQLException e) { System.out.println("Sql异常1"); } finally{ if(rs!=null){ try { rs.close(); } catch (SQLException e) { System.out.println("关闭rs时异常"); } } if(stmt!=null){ try { stmt.close(); } catch (SQLException e) { System.out.println("关闭stmt时异常"); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { System.out.println("关闭conn时异常"); } } } return list; }
<% UsersDAO db = new UsersDAO(); String sql="select *from ZTC"; ArrayList<User> list = db.select(sql); if(list.isEmpty()==false) { for(int i=0;i<list.size();i++) { User user = list.get(i); %> <tr> <td><%=user.getId()%></td> <td><%=user.getUsername()%></td> <td><%=user.getTel()%></td> <td><%=user.getAddr()%></td> <td><%=user.getMail()%></td> <td><%=user.getType()%></td> <td><%=user.getNum()%></td> <td><%=user.getYb()%></td> <td><%=user.getYf()%></td> <td><%=user.getFp()%></td> <td><%=user.getPrice()%></td> <td><%=user.getFk()%></td> <td>