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
如何解决这个问题,请告诉我。
谢谢。