gpt4 book ai didi

java - 不要使用 jdbc 和 jsp 在 mysql 中插入数据

转载 作者:行者123 更新时间:2023-11-29 03:29:16 25 4
gpt4 key购买 nike

我在 MySQL 中有一个表,其中包含列 applicant_idprofession_idlast_namefirst_name入学年份

这是我用来插入或更新记录的代码:

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.

我的 Controller 类是:

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();
}
}
}

抱歉代码量太大,但我认为需要它来显示整个问题。

感谢您的关注。

最佳答案

你的代码有问题。你的jsp没有包含一个名为“applicant_id”的字段。但是你在类SaveApplicantCommand中将参数名称称为“applicant_id”。它可能是“application_id”,对吧?

  if (request.getParameter("applicant_id") != null) {
applicant.setId(Long.parseLong(request.getParameter("applicant_id")));
}

然后把上面的代码改成

if (request.getParameter("application_id") != null) {
applicant.setId(Long.parseLong(request.getParameter("application_id")));
}

关于java - 不要使用 jdbc 和 jsp 在 mysql 中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32565837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com