怎么我写的list页面只显示数据库里的第一条数据?求大神帮我看看
DAO里面的代码是publicList<User>list(){Connectionconn=null;PreparedStatementps=null;ResultSe...
DAO里面的代码是public List<User> list() {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
List<User> users=new ArrayList<User>();
try {
conn=DatebaseConnection.getConnection();
String sql="select * from usertest";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()){
User user=new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setNickname(rs.getString("nickname"));
user.setPassword(rs.getString("password"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DatebaseConnection.close(ps);
DatebaseConnection.close(rs);
DatebaseConnection.close(conn);
}
return users;
}
list.jsp的代码是
<html>
<head>
<%
IUserDAO userDAO=DAOFactory.getUserDAO();
List<User> users=userDAO.list();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
</head>
<body>
<jsp:include page="inc.jsp"></jsp:include>
<table align="center" width="500" border="1">
<tr>
<td>用户ID</td><td>用户名称</td><td>用户昵称</td><td>操作</td>
</tr>
<%
for(User u:users){
%>
<tr>
<td><%=u.getId()%></td>
<td><%=u.getUsername() %></td>
<td><%=u.getNickname() %></td>
<td><a href="">删除</a> <a href="">更新</a></td>
</tr>
<%
}
%>
</table> 展开
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
List<User> users=new ArrayList<User>();
try {
conn=DatebaseConnection.getConnection();
String sql="select * from usertest";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()){
User user=new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setNickname(rs.getString("nickname"));
user.setPassword(rs.getString("password"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DatebaseConnection.close(ps);
DatebaseConnection.close(rs);
DatebaseConnection.close(conn);
}
return users;
}
list.jsp的代码是
<html>
<head>
<%
IUserDAO userDAO=DAOFactory.getUserDAO();
List<User> users=userDAO.list();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
</head>
<body>
<jsp:include page="inc.jsp"></jsp:include>
<table align="center" width="500" border="1">
<tr>
<td>用户ID</td><td>用户名称</td><td>用户昵称</td><td>操作</td>
</tr>
<%
for(User u:users){
%>
<tr>
<td><%=u.getId()%></td>
<td><%=u.getUsername() %></td>
<td><%=u.getNickname() %></td>
<td><a href="">删除</a> <a href="">更新</a></td>
</tr>
<%
}
%>
</table> 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询