【发布时间】:2015-12-10 12:22:57
【问题描述】:
我在 MySQL 中有一个表,其中包含 applicant_id、profession_id、last_name、first_name 和 entrance_year 列。
这是我用来插入或更新记录的代码:
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.setLong(3, (long) applicant.getProfessionId());
preparedStatement.setInt(4, (int) applicant.getEntranceYear());
} else {
preparedStatement = connection.prepareStatement("UPDATE applicant SET first_name=?, last_name=?, profession_id=?, entrace_year=? WHERE applicant_id=?");
preparedStatement.setString(1, applicant.getFirstName());
preparedStatement.setString(2, applicant.getLastName());
preparedStatement.setLong(3, (long) applicant.getProfessionId());
preparedStatement.setInt(4, (int) applicant.getEntranceYear());
preparedStatement.setInt(5, (int) applicant.getId());
}
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new Exception(e);
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
}
}
这是实现上述代码的类:
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:
<%@ 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 application</h1>
<form method="post" action="controller?command=saveApplication">
<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="${application ne null}">
<input type="text" name="first_name" value="${application.getFirstName()}"/>
<input type="text" name="last_name" value="${application.getLastName()}"/>
<input type="text" name="profession_id" value="${application.getProfessionId()}"/>
<input type="text" name="entrance_year" value="${application.getEntranceYear()}"/>
<input type="hidden" name="application_id" value="${application.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>
此表格允许我输入/编辑 4 个参数。但是,在提交表单后,会引发异常:
java.lang.NullPointerException
org.sourceit.main.Controller.processRequest(Controller.java:28)
org.sourceit.main.Controller.doPost(Controller.java:23)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.64 logs.
我的控制器类是:
public class Controller extends HttpServlet {
private Chooser chooser = Chooser.INSTANCE;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
}
private void processRequest(HttpServletRequest req, HttpServletResponse resp) {
try {
String page = chooser.chooseCommand(req.getParameter("command")).execute(req, resp);
req.getRequestDispatcher(page).forward(req, resp);
} catch (ServletException | IOException e) {
e.printStackTrace();
}
}
}
对代码量感到抱歉,但我认为需要展示整个问题。
感谢您的关注。
【问题讨论】:
-
您将
command参数作为表单中的 GET 方法发送,但表单方法是 POST。 -
这里
String page = chooser.chooseCommand(req.getParameter("command")).execute(req, resp);或这里req.getRequestDispatcher(page).forward(req, resp);有问题。是哪一个?
标签: java mysql jsp tomcat jdbc