我尝试在MVC模式下创建一个Web应用程序来显示来自DB的数据
JSP:
<c:forEach var="pro" items="${list}">
<tr>
<td><c:out value="${pro.reqno}"></c:out></td>
<td><c:out value="${pro.leave_Type}"></c:out></td>
<td><c:out value="${pro.no_of_days}"></c:out></td>
<td><c:out value="${pro.status}"></c:out></td>
<td><c:out value="${pro.balanceUnits}"></c:out></td>
<td><c:out value="${pro.totalUnits}"></c:out></td>
</tr>
</c:forEach>
的Servlet
ArrayList temp = summary.getSummary(objUsr);
request.setAttribute("list", temp);
RequestDispatcher rd = request.getRequestDispatcher("JSP/login-success.jsp");
DAO课程:
public ArrayList<LeaveSummary> getSummary(LeaveSummary objUsr) throws SQLException {
PreparedStatement stmt = null;
ArrayList<LeaveSummary> rowArray = new ArrayList<LeaveSummary>();
ResultSet rs = null;
try {
LeaveSummary getValuesFromDb = new LeaveSummary();
Connection con = ConnectionProvider.getCon();
stmt = con.prepareStatement("SELECT * FROM create_request where username=?");
stmt.setString(1, objUsr.getUser_name());
rs = stmt.executeQuery();
while (rs.next()) {
getValuesFromDb.setReqno(rs.getInt(1));
getValuesFromDb.setUser_name(rs.getString(2));
getValuesFromDb.setNo_of_days(rs.getInt(4));
getValuesFromDb.setLeave_Type(rs.getString(3));
getValuesFromDb.setStatus(rs.getString(6));
getValuesFromDb.setTotalUnits(rs.getInt(7));
getValuesFromDb.setBalanceUnits(rs.getInt(8));
rowArray.add(getValuesFromDb);
}
}
catch(Exception e)
{
e.printStackTrace();
}
数据库表:
1 san Casual 1 sicks pending 10 9
2 san Others 2 Functional pending 10 8
但它仅打印第二行(即)最近添加的行,如何从表中打印两行
答案 0 :(得分:4)
创建新实例
LeaveSummary getValuesFromDb = new LeaveSummary();
在你的while循环中。您没有为数据库中的每个记录创建一个新的LeaveSummary实例。因此,每次循环结果集时都会重写相同的对象。这就是为什么单独的最后一条记录存在于列表中。
答案 1 :(得分:2)
问题是你要修改同一个对象并将其添加到列表中。在循环中创建新对象。
LeaveSummary getValuesFromDb = new LeaveSummary();
Connection con = ConnectionProvider.getCon();
stmt = con.prepareStatement("SELECT * FROM create_request where username=?");
stmt.setString(1, objUsr.getUser_name());
rs = stmt.executeQuery();
while (rs.next()) {
//Add this line
getValuesFromDb = new LeaveSummary();
getValuesFromDb.setReqno(rs.getInt(1));
getValuesFromDb.setUser_name(rs.getString(2));
getValuesFromDb.setNo_of_days(rs.getInt(4));
getValuesFromDb.setLeave_Type(rs.getString(3));
getValuesFromDb.setStatus(rs.getString(6));
getValuesFromDb.setTotalUnits(rs.getInt(7));
getValuesFromDb.setBalanceUnits(rs.getInt(8));
rowArray.add(getValuesFromDb);
}
答案 2 :(得分:1)
试试此代码
LeaveSummary getValuesFromDb = new LeaveSummary();
Connection con = ConnectionProvider.getCon();
stmt = con.prepareStatement("SELECT * FROM create_request where username=?");
stmt.setString(1, objUsr.getUser_name());
rs = stmt.executeQuery();
while (rs.next()) {
getValuesFromDb = new LeaveSummary();
getValuesFromDb.setReqno(rs.getInt(1));
getValuesFromDb.setUser_name(rs.getString(2));
getValuesFromDb.setNo_of_days(rs.getInt(4));
getValuesFromDb.setLeave_Type(rs.getString(3));
getValuesFromDb.setStatus(rs.getString(6));
getValuesFromDb.setTotalUnits(rs.getInt(7));
getValuesFromDb.setBalanceUnits(rs.getInt(8));
rowArray.add(getValuesFromDb);
}