Reputation: 171
I have a table in MySQL with the columns applicant_id, profession_id, last_name, first_name and entrance_year
. I need get the contents of the database.
I write method:
That is my class:
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;
}
}
That is my method, that show contents of the database:
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;
}
And JSP applicant:
<%@ 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>
When I enter the database instance to the values 5, I get a list of five of the same values! Look:
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
All contents is identical, but i write different data.
5 John Smit 2 2008
It is the last contents, that i put into database.
When, i open mysql database, i can see:
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
How to resolve this problem, please tell me.
Thanks.
Upvotes: 1
Views: 49
Reputation: 32681
In the while loop you are using just one Applicant. You keep changing the data so you will only see the last data. You keep adding the same allocated Applicant to the List.
You need a new Applicant for each row,
The easy way to do this is move the line Applicant applicant = new Applicant();
to the first line inside the while loop
Upvotes: 1