java - 看不到数据库mysql的内容。使用 JSP/JDBC

标签 java mysql jsp jdbc

我在 MySQL 中有一个表,其中包含 applicant_id、profession_id、last_name、first_name 和 entry_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    

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

谢谢。

最佳答案

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

你需要为每一行一个新的申请人,

执行此操作的简单方法是将 Applicant applicant = new Applicant(); 行移动到 while 循环内的第一行

关于java - 看不到数据库mysql的内容。使用 JSP/JDBC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32573832/

相关文章:

java - 无法让 Glassfish 使用版本 7 编译 JSP

java - 如何迭代并获取动态传递的类的值

java - 使用 Spring 在 Java 中发送文本 REST 消息

java - 在java中,如果不在main()中,我什么时候可以访问这个实例?

mysql - 我怎样才能避免看门狗的distinct()?

jsp - 有助于 JSP 中字符串国际化的工具

java - xor如何在两个数组中给出不同的数字‽

c# - 从 MySQL 数据库中减去数量而不会变成负数

php - 向Mysql数据库中插入数据

jsp - response.sendRedirect 不起作用