Java 无法查看数据库mysql的内容。使用JSP/JDBC
我在MySQL中有一个表,其中有Java 无法查看数据库mysql的内容。使用JSP/JDBC,java,mysql,jsp,jdbc,Java,Mysql,Jsp,Jdbc,我在MySQL中有一个表,其中有申请人id、职业id、姓、名和入学年份列。我需要得到数据库的内容。 我写方法: 那是我的班级: public class Applicant extends Entity { private long professionId; private String lastName; private String firstName; private int entranceYear; public Applicant() {
申请人id、职业id、姓、名和入学年份列。我需要得到数据库的内容。
我写方法:
那是我的班级:
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;
}
所有内容都是相同的,但我写的数据不同
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循环中,您只使用一个申请人。您不断更改数据,以便只看到最后的数据。您不断将同一个已分配的申请人添加到列表中
每行需要一个新的申请人
执行此操作的简单方法是移动行applicator=new applicator()代码>到while循环内的第一行
5 John Smit 2 2008
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