私は、JSPとサーブレットで社員を登録するシステムを作っています。
社員登録確認画面まではいくのですが、社員完了画面にいく際に、500エラーが発生してしまいます。
コンソールで原因を調べてみると、EmployeeDAOのps.setInt(7, employee.getDeptId());
というコードでエラーが出ていました。
実現したいことは、社員完了画面までいって社員登録ができていることです。
JSP(登録入力)です。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>社員管理システム</title> </head> <body> <h3>社員登録入力画面</h3> <div class="update"> <p class="error">${errMessage}</p> <form method="post" action="/servlet_crud//Check" method="post"> <div class="form"> <div class="label">パスワード:</div> <div class="input"> <input type="password" name="empPass" /> </div> </div> <div class="form"> <div class="label">社員名:</div> <div class="input"> <input type="text" name="empName" /> </div> </div> <div class="form"> <div class="label">性別:</div> <div class="input"> <input type="radio" name="gender" value="1" />男性 <input type="radio" name="gender" value="2" />女性 </div> </div> <div class="form"> <div class="label">住所:</div> <div class="input"> <input type="text" name="address" /> </div> </div> <div class="form"> <div class="label">生年月日:</div> <div class="input"> <input type="text" name="birthday"/>(YYYY/MM/DD) </div> </div> <div class="form"> <div class="label">権限:</div> <div class="input"> <input type="radio" name="authority" value="1" />一般 <input type="radio" name="authority" value="2" />管理者 </div> </div> <div class="form"> <div class="label">部署名:</div> <div class="input"> <select name="deptId"> <option value="1">営業部</option> <option value="2">経理部</option> <option value="3">総務部</option> </select> </div> </div> <div class="form"> <div class="label"></div> <div class="input"> <input type="submit" value="登録" /> </div> </div> </form> <form action="../../select/list.jsp"> <div class="form"> <div class="label"></div> <div class="input"> <input type="submit" value="戻る" /> </div> </div> </form> </div> </div> </body> </html>
servlet(Check)です。
package jp.co.sss.servlet.regist; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import jp.co.sss.crud.bean.Department; import jp.co.sss.crud.bean.Employee; import jp.co.sss.crud.db.DepartmentDAO; @WebServlet("/Check") public class Check extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String empPass = request.getParameter("empPass"); String empName = request.getParameter("empName"); String gender = request.getParameter("gender"); String address = request.getParameter("address"); String birthday = request.getParameter("birthday"); String authority = request.getParameter("authority"); String deptIdstr = "1"; int deptId = Integer.parseInt(deptIdstr); Department department = null; try { department = DepartmentDAO.findByDepartment(deptId); } catch (SQLException e) { throw new ServletException(e); } Employee employee = new Employee(); employee.setEmpPass(empPass); employee.setEmpName(empName); employee.setGender(gender); employee.setAddress(address); employee.setBirthday(birthday); employee.setAuthority(authority); employee.setDepartment(department); request.setAttribute("employee", employee); request.getRequestDispatcher("/jsp/sample01/regist_check.jsp").forward(request, response); } } /** * 社員を登録 * * @return 社員登録 * @throws SQLException * データベース操作失敗時に送出 */ public static void insert(Employee employee) { Connection con = null; PreparedStatement ps = null; try { con = DBManager.getConnection(); ps = con.prepareStatement("INSERT INTO EMPLOYEE(EMP_PASS,EMP_NAME," + "GENDER,ADDRESS,BIRTHDAY,AUTHORITY,DEPT_ID)VALUES(?, ?, ?, ?, ?, ?, ?)"); ps.setString(1, employee.getEmpPass()); ps.setString(2, employee.getEmpName()); ps.setString(3, employee.getGender()); ps.setString(4, employee.getAddress()); ps.setString(5, employee.getBirthday()); ps.setString(6, employee.getAuthority()); ps.setInt(7, employee.getDeptId()); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { DBManager.close(ps, con); } }
DepartmentDAO
package jp.co.sss.crud.db; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import jp.co.sss.crud.bean.Department; public class DepartmentDAO { /** * 部署情報を部署IDで検索 * * @param genreId * 部署ID * @return 部署情報 * @throws SQLException * データベース操作失敗時に送出 */ public static Department findByDepartment(int deptId) throws SQLException { Department department = null; Connection connection = DBManager.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM DEPARTMENT WHERE DEPT_ID = ?"); preparedStatement.setInt(1, deptId); ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { department = new Department(); department.setDeptId(resultSet.getInt("dept_id")); department.setDeptName(resultSet.getString("dept_name")); } DBManager.close(preparedStatement, connection); return department; } }
Departmentのbean
package jp.co.sss.crud.bean; /** * 部署情報Bean * * @author system_shared */ public class Department { /** 部署ID */ private int deptId; /** 部署名 */ private String deptName; /** * 部署IDを取得 * * @return 部門ID */ public int getDeptId() { return deptId; } /** * 部門IDを保存 * * @param deptId * 部門ID */ public void setDeptId(int deptId) { this.deptId = deptId; } /** * 部門を取得 * * @return 部門名 */ public String getDeptName() { return deptName; } /** * 部門名を保存 * * @param deptName * 部門名 */ public void setDeptName(String deptName) { this.deptName = deptName; } }
HTTPステータス500-というエラーが起き、説明に、The server encountered an internal error that prevented it from fulfilling this request.というのが表示されています。
ps.setInt(7, employee.getDeptId());のコードを消すと社員完了画面まではいくのですが、社員登録はされませんでした。
intの宣言に間違いがないか確認したり、デバックで調べたりしたのですが、解決できなかったので、アドバイス等頂けると大変助かります。
ソースコードが多く、大変ご迷惑だと思いますが、宜しくお願い致します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/29 06:24
2019/07/29 06:27
2019/07/29 06:28
2019/07/29 06:29
2019/07/29 06:30
2019/07/29 06:32
2019/07/29 06:36
2019/07/29 06:38