我有两张桌子: 第一个表,有两列专业(profession_id,profession_name)。 第二个表,申请人有五列(applicant_id,profession_id,last_name,first_name,entrance_year)。 表中的profession_id表和表中的profession_id是MySQL中的相关字段。 我需要,而不是profession_id,在表申请人中获得可用职业的下拉列表。 我的桌子:
CREATE TABLE PROFESSION (
PROFESSION_ID INT NOT NULL AUTO_INCREMENT,
PROFESSION_NAME VARCHAR(50) NOT NULL,
PRIMARY KEY (PROFESSION_ID)
);
CREATE TABLE APPLICANT (
APPLICANT_ID INT NOT NULL AUTO_INCREMENT,
PROFESSION_ID INT NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
FIRST_NAME VARCHAR(30) NOT NULL,
ENTRANCE_YEAR INT NOT NULL,
PRIMARY KEY (APPLICANT_ID),
FOREIGN KEY (PROFESSION_ID) REFERENCES PROFESSION (PROFESSION_ID)
);
表申请人的方法:
public Applicant getApplicant(long applicantId) throws Exception {
PreparedStatement preparedStatement = null;
Applicant applicant = null;
try {
preparedStatement = connection.prepareStatement("SELECT * FROM applicant WHERE applicant_id=?");
preparedStatement.setInt(1, (int) applicantId);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
applicant = new Applicant();
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"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
}
return applicant;
}
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");
ResultSet resultSet = statement.executeQuery("select a.applicant_id, a.first_name, a.last_name, a.entrance_year, p.profession_name from applicant a join profession p on a.profession_id = p.profession_id");
Applicant applicant = null;
while (resultSet.next()) {
applicant = new Applicant();
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.setProfessionName(resultSet.getString("profession_name"));
applicant.setEntranceYear(resultSet.getInt("entrance_year"));
applicants.add(applicant);
}
} catch (SQLException e) {
throw new Exception(e);
}
return applicants;
}
public void saveApplicant(Applicant applicant) throws Exception {
PreparedStatement preparedStatement = null;
try {
if (applicant.getId() == -1) {
preparedStatement = connection.prepareStatement("INSERT INTO applicant (first_name, last_name, profession_id, entrance_year) VALUES (?,?,?,?)");
preparedStatement.setString(1, applicant.getFirstName());
preparedStatement.setString(2, applicant.getLastName());
preparedStatement.setInt(3, (int)applicant.getProfessionId());
preparedStatement.setInt(4, applicant.getEntranceYear());
} else {
preparedStatement = connection.prepareStatement("UPDATE applicant SET first_name=?, last_name=?, profession_id=?, entrance_year=? WHERE applicant_id=?");
preparedStatement.setString(1, applicant.getFirstName());
preparedStatement.setString(2, applicant.getLastName());
preparedStatement.setInt(3, (int) applicant.getProfessionId());
preparedStatement.setInt(4, applicant.getEntranceYear());
preparedStatement.setInt(5, (int) applicant.getId());
}
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new Exception(e);
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
}
}
在方法List getApplicants()中,我实现了INNER JOIN,并且在结果表中,我有而不是applicant_id,专业名称。看:
ID First Name Last Name Profession ID Entrance Year Actions
1 The Rock Java 2007 Delete Edit
2 CM Punk JS 2008 Delete Edit
3 Duke Nukem Java 2007 Delete Edit
Add new applicant
之前,它的方法显示ID,而不是专业名称
ID First Name Last Name Profession ID Entrance Year Actions
1 The Rock 1 2007 Delete Edit
2 CM Punk 2 2008 Delete Edit
3 Duke Nukem 1 2007 Delete Edit
Add new applicant
是的,我有一份专业清单,而不是职业ID。但是,我需要在方法saveApplicant中更改,以获取下拉列表。
我的类saveApplicant:
public class SaveApplicantCommand implements ICommand {
private ApplicantDBProvider provider = ApplicantDBProvider.INSTANCE;
@Override
public String execute(HttpServletRequest request, HttpServletResponse resp) {
Applicant applicant = new Applicant();
applicant.setFirstName(request.getParameter("first_name"));
applicant.setLastName(request.getParameter("last_name"));
applicant.setProfessionId(Long.parseLong(request.getParameter("profession_id")));
applicant.setEntranceYear(Integer.parseInt(request.getParameter("entrance_year")));
if (request.getParameter("applicant_id") != null) {
applicant.setId(Long.parseLong(request.getParameter("applicant_id")));
}
try {
provider.saveApplicant(applicant);
} catch (Exception e) {
request.setAttribute("error", e);
return "pages/error.jsp";
}
return "controller?command=applicants";
}
}
我的jsp。 edit_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>Add applicant</h1>
<form method="post" action="controller?command=saveApplicant">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Profession ID</th>
<th>Entrance Year</th>
</tr>
</table>
<c:choose>
<c:when test="${applicant ne null}">
<input type="text" name="first_name" value="${applicant.getFirstName()}"/>
<input type="text" name="last_name" value="${applicant.getLastName()}"/>
<input type="text" name="profession_id" value="${applicant.getProfessionId()}"/>
<input type="text" name="entrance_year" value="${applicant.getEntranceYear()}"/>
<input type="hidden" name="applicant_id" value="${applicant.getId()}"/>
</c:when>
<c:otherwise>
<input type="text" name="first_name" value=""/>
<input type="text" name="last_name" value=""/>
<input type="text" name="profession_id" value=""/>
<input type="text" name="entrance_year" value=""/>
</c:otherwise>
</c:choose>
<input type=submit value=submit>
</form>
</body>
</html>
我听说,用标签实现的下拉列表。但是,如何做到这一点,我不知道。我需要,从第二张表中获取专业清单。在方法getApplicants()中,我实现了它。但是,在方法getApplicants()中,我使用INNER。在方法saveApplicant()中,我使用了INSERT和UPDATE。以及如何从其他表中下载专业列表,我不知道。请帮忙。谢谢。抱歉,您需要阅读大量代码。