2015-09-14 54 views
1

我有一個MySQL表中的列applicant_id, profession_id, last_name, first_name and entrance_year。我需要獲取數據庫的內容。 我寫的方法: 這就是我的課:不能看到數據庫mysql的內容。使用JSP/JDBC

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

在while循環中,您只使用一個申請人。您不斷更改數據,以便您只能看到最後的數據。您不斷將相同的已分配申請人添加到列表中。

你需要爲每行一個新的申請人,

最簡單的方式做,這是招行Applicant applicant = new Applicant();第一行while循環中

相關問題