无法看到数据库mysql的内容。使用JSP / JDBC

时间:2015-09-14 20:58:25

标签: java mysql jsp jdbc

我在MySQL中有一个带有applicant_id, profession_id, last_name, first_name and entrance_year列的表。我需要获取数据库的内容。 我写方法: 那是我的班级:

public class Applicant extends Entity {

    private long professionId;
    private String lastName;
    private String firstName;
    private int entranceYear;

    public Applicant() {
        this.id = -1;
    }

    public Applicant(long professionId, String lastName, String firstName, int entranceYear) {
        this.professionId = professionId;
        this.lastName = lastName;
        this.firstName = firstName;
        this.entranceYear = entranceYear;
    }

    public long getProfessionId() {
        return professionId;
    }

    public void setProfessionId(long professionId) {
        this.professionId = professionId;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public int getEntranceYear() {
        return entranceYear;
    }

    public void setEntranceYear(int entranceYear) {
        this.entranceYear = entranceYear;
    }

}

这是我的方法,显示数据库的内容:

public List<Applicant> getApplicants() throws Exception {
        Statement statement = null;
        List <Applicant> applicants = new ArrayList<>();

        try {
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM applicant");
            Applicant applicant = new Applicant();
            while (resultSet.next()) {
                applicant.setId(resultSet.getInt("applicant_id"));
                applicant.setFirstName(resultSet.getString("first_name"));
                applicant.setLastName(resultSet.getString("last_name"));
                applicant.setProfessionId(resultSet.getInt("profession_id"));
                applicant.setEntranceYear(resultSet.getInt("entrance_year"));
                applicants.add(applicant);

            }

        } catch (SQLException e) {
            throw new Exception(e);
        }

        return applicants;
    }

和JSP申请者:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
  <title></title>
</head>
<body>
<h1>Applicants</h1>
<c:choose>
  <c:when test="${applicants.size() == 0}">
    <p><c:out value="No applicants yet"></c:out></p>
  </c:when>
  <c:otherwise>
    <table>
      <tr>
        <th>ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Profession ID</th>
        <th>Entrance Year</th>
        <th>Actions</th>
      </tr>
      <c:forEach items="${applicants}" var="applicant">
        <tr>
          <td>
            <c:out value="${applicant.getId()}"/>
          </td>
          <td>
            <c:out value="${applicant.getFirstName()}"/>
          </td>
          <td>
            <c:out value="${applicant.getLastName()}"/>
          </td>
          <td>
            <c:out value="${applicant.getProfessionId()}"/>
          </td>
          <td>
            <c:out value="${applicant.getEntranceYear()}"/>
          </td>
          <td>
            <a href="controller?command=deleteApplicant&id=${applicant.getId()}">Delete</a>
            <a href="controller?command=editApplicant&id=${applicant.getId()}">Edit</a>
          </td>
        </tr>
      </c:forEach>
    </table>
  </c:otherwise>
</c:choose>
<a href="controller?command=addApplicant">Add new applicant</a>
</body>
</html>

当我将数据库实例输入值5时,我会得到五个相同值的列表!看:

5   John    Smit    2   2008    
5   John    Smit    2   2008    
5   John    Smit    2   2008    
5   John    Smit    2   2008    
5   John    Smit    2   2008    

所有内容都相同,但我写的是不同的数据。

    5   John    Smit    2   2008

这是我放入数据库的最后一个内容。

当我打开mysql数据库时,我可以看到:

1   Duke    Nukem   1   2007    
2   The     Rock    2   2006    
3   Rey     Junior  3   2009    
4   Randy   Orton   1   2012    
5   John    Smit    2   2008    

如何解决这个问题,请告诉我。

感谢。

1 个答案:

答案 0 :(得分:1)

在while循环中,您只使用一个申请人。您不断更改数据,因此您只能看到最后的数据。您继续将相同的已分配申请人添加到列表中。

每行需要一个新的申请人

执行此操作的简单方法是将行submit移至while循环内的第一行