社員管理システムを作成してます。左側の部署名検索から検索した結果、下のようなエラーとなります。
SQL文に示すように、dept_idで二つのテーブルを内部結合しています。
SQL
1-- ユーザーの作成・権限付与 2CREATE USER servlet_training_user IDENTIFIED BY systemsss; 3GRANT ALL PRIVILEGES TO servlet_training_user; 4 5-- 部署テーブルの作成 6CREATE TABLE department ( 7 dept_id NUMBER(2) PRIMARY KEY, 8 dept_name VARCHAR2(15 CHAR) NOT NULL 9); 10 11-- 社員テーブルの作成 12CREATE TABLE employee ( 13 emp_id NUMBER(5) PRIMARY KEY, 14 emp_pass VARCHAR2(16 CHAR) NOT NULL, 15 emp_name VARCHAR2(30 CHAR) NOT NULL, 16 gender NUMBER(1) NOT NULL, 17 address VARCHAR(60 CHAR) NOT NULL, 18 birthday DATE NOT NULL, 19 authority NUMBER(1) NOT NULL, 20 dept_id NUMBER(2) NOT NULL REFERENCES department(dept_id) 21); 22 23-- シーケンスの作成 24CREATE SEQUENCE seq_emp NOCACHE;; 25 26 27-- 部署テーブルへのレコード登録 28INSERT INTO department VALUES(1, '営業部'); 29INSERT INTO department VALUES(2, '経理部'); 30INSERT INTO department VALUES(3, '総務部'); 31 32-- 社員テーブルへのレコード登録 33INSERT INTO employee VALUES(seq_emp.nextval,'1111','鈴木太郎',1,'東京都','1986/10/12',1,1); 34INSERT INTO employee VALUES(seq_emp.nextval,'2222','田中二郎',1,'千葉県','1979/07/02',2,2); 35INSERT INTO employee VALUES(seq_emp.nextval,'3333','渡辺花子',2,'大阪府','1988/04/23',2,2); 36 37COMMIT; 38
servlet
1package jp.co.sss.crud.servlet; 2 3import java.io.IOException; 4import java.util.List; 5 6import javax.servlet.ServletException; 7import javax.servlet.annotation.WebServlet; 8import javax.servlet.http.HttpServlet; 9import javax.servlet.http.HttpServletRequest; 10import javax.servlet.http.HttpServletResponse; 11 12import jp.co.sss.crud.bean.Emp; 13import jp.co.sss.crud.db.EmpDao; 14 15@WebServlet("/SelectDeptName") 16public class SelectDeptName extends HttpServlet { 17 protected void doPost(HttpServletRequest request, HttpServletResponse response) 18 throws ServletException, IOException { 19 String empName = (String)request.getParameter("deptName"); 20 List<Emp> empList = EmpDao.findByDeptName(deptName); 21 request.setAttribute("empList", empList); 22 request.getRequestDispatcher("/html/list/list.jsp").forward(request, response); 23 } 24 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 25 this.doPost(request, response); 26 } 27} 28
JavaBeans
1package jp.co.sss.crud.bean; 2 3import java.util.Date; 4 5public class Emp { 6 private int empId; 7 private String empPass; 8 private String empName; 9 private int gender; 10 private String address; 11 private Date birthday; 12 private int authority; 13 private Dept dept; 14 15 public int getEmpId() { 16 return empId; 17 } 18 public void setEmpId(int empId) { 19 this.empId = empId; 20 } 21 public String getEmpPass() { 22 return empPass; 23 } 24 public void setEmpPass(String empPass) { 25 this.empPass = empPass; 26 } 27 public String getEmpName() { 28 return empName; 29 } 30 public void setEmpName(String empName) { 31 this.empName = empName; 32 } 33 public int getGender() { 34 return gender; 35 } 36 public void setGender(int gender) { 37 this.gender = gender; 38 } 39 public String getAddress() { 40 return address; 41 } 42 public void setAddress(String address) { 43 this.address = address; 44 } 45 public Date getBirthday() { 46 return birthday; 47 } 48 public void setBirthday(Date birthday) { 49 this.birthday = birthday; 50 } 51 public int getAuthority() { 52 return authority; 53 } 54 public void setAuthority(int authority) { 55 this.authority = authority; 56 } 57 public Dept getDept() { 58 return dept; 59 } 60 public void setDept(Dept dept) { 61 this.dept = dept; 62 } 63 64}
EmpDao
1public static Emp findByDeptName(String deptName) { 2 Connection con = null; 3 PreparedStatement ps = null; 4 Emp emp = new Emp(); 5 try { 6 con = DBManager.getConnection(); 7 ps = con.prepareStatement( 8 "SELECT * FROM employee e INNER JOIN department d ON e.dept_id = d.dept_id WHERE dept_name = ?"); 9 ps.setString(1, deptName); 10 ResultSet rs = ps.executeQuery(); 11 12 if (rs.next()) { 13 emp.setEmpId(rs.getInt("emp_id")); 14 emp.setEmpName(rs.getString("emp_name")); 15 emp.setGender(rs.getInt("gender")); 16 emp.setAddress(rs.getString("address")); 17 emp.setBirthday(rs.getDate("birthday")); 18 emp.setAuthority(rs.getInt("authority")); 19 20 Dept dept = new Dept(); 21 dept.setDeptName(rs.getString("dept_name")); 22 emp.setDept(dept); 23 } 24 } catch (SQLException e) { 25 e.printStackTrace(); 26 } finally { 27 DBManager.close(ps, con); 28 } 29 return emp; 30 }
jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4<!DOCTYPE html> 5<html> 6<head> 7<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8<link rel="stylesheet" href="<%=request.getContextPath()%>/css/layout.css"/> 9<link rel="stylesheet" href="<%=request.getContextPath()%>/css/style.css"/> 10<title>社員管理システム</title> 11</head> 12<body class="list"> 13 <header> 14 <div class="content"> 15 <div class="title">社員管理システム</div> 16 <div class="user_info">ようこそ、<a href="../update/update_input.jsp">田中二郎</a>さん<span class="pipeline">|</span><a href="<%=request.getContextPath()%>/html/index.jsp">ログアウト</a> 17 </div> 18 </div> 19 </header> 20 21 <div class="container content"> 22 <aside class="search"> 23 <div class="title">社員名検索</div> 24 <div class="form"> 25 <form action="<%=request.getContextPath() %>/SelectName" method="post"> 26 <input type="text" name="empName" /> 27 <input type="submit" value="検索" /> 28 </form> 29 </div> 30 <div class="title">部署名検索</div> 31 <div class="form"> 32 <form action="<%=request.getContextPath() %>/SelectDeptName" method="post"> 33 <select name="deptId"> 34 <option value="1" selected="selected">営業部</option> 35 <option value="2">経理部</option> 36 <option value="3">総務部</option> 37 </select> 38 <input type="submit" value="検索" /> 39 </form> 40 </div> 41 </aside> 42 43 <article class="main"> 44 <h3 class="page_title">社員一覧画面</h3> 45 <div class="regist_link"> 46 <a href="../regist/regist_input.jsp">新規社員登録</a> 47 </div> 48 <table class="emp_list_table"> 49 <tr> 50 <th class="empId">社員ID</th> 51 <th class="empName">社員名</th> 52 <th class="gender">性別</th> 53 <th class="address">住所</th> 54 <th class="birthday">生年月日</th> 55 <th class="authority">権限</th> 56 <th class="deptName">部署名</th> 57 <th class="button" colspan="2">操作</th> 58 </tr> 59 <c:forEach var="emp" items="${empList}"> 60 <tr> 61 <td>${emp.empId}</td> 62 <td>${emp.empName}</td> 63 <td>${emp.gender}</td> 64 <td>${emp.address}</td> 65 <td>${emp.birthday}</td> 66 <td>${emp.authority}</td> 67 <td>${emp.dept.deptName}</td> 68 <td class="button"> 69 <form action="<%=request.getContextPath() %>/html/update/update_input.jsp" method="post"> 70 <input type="hidden" name="${emp.empId}"/> 71 <input type="submit" value="変更" /> 72 </form> 73 </td> 74 <td class="button"> 75 <form action="<%=request.getContextPath() %>/DeleteCheck" method="post"> 76 <input type="hidden" name="${emp.empId}"/> 77 <input type="submit" value="削除" class="delete" /> 78 </form> 79 </td> 80 </tr> 81 </c:forEach> 82 </table> 83 </article> 84 </div> 85 86 <footer> 87 <small>Copyright(C) 2017 System Shared co., ltd, ALL Rights Reserved</small> 88 <!-- small要素は免責・著作権・ライセンス要件などを表す際に使用します。 --> 89 </footer> 90</body> 91</html>
回答1件
あなたの回答
tips
プレビュー