社員管理システムを作成しています。データベースから取得してきたデータをweb上に表示したいのですが、「部署名」という項目のデータが表示されない原因が分かりません。
ちなみにデータベース上で「社員ID」「社員名」「性別」「住所」「生年月日」「権限」はemployeeテーブル、「部署ID」「部署名」はdepartmentテーブルにあり、「部署ID」で内部結合しています。
SQL
1-- ユーザーの作成・権限付与 2CREATE USER book_user IDENTIFIED BY systemsss; 3GRANT ALL PRIVILEGES TO book_user; 4 5 6-- ジャンル情報テーブルの作成 7CREATE TABLE genre ( 8 genre_id NUMBER(2) PRIMARY KEY, 9 genre_name VARCHAR2(30 CHAR) NOT NULL 10); 11 12-- 書籍情報テーブルの作成 13CREATE TABLE book ( 14 book_id NUMBER(5) PRIMARY KEY, 15 book_name VARCHAR2(60 CHAR) NOT NULL, 16 author VARCHAR2(30 CHAR) NOT NULL, 17 publication_date DATE NOT NULL, 18 stock NUMBER(4) NOT NULL, 19 genre_id NUMBER(2) NOT NULL REFERENCES genre(genre_id) 20); 21 22-- ユーザ情報テーブルの作成 23CREATE TABLE book_user ( 24 book_user_id NUMBER(5) PRIMARY KEY, 25 book_user_name VARCHAR2(30 CHAR) NOT NULL, 26 password VARCHAR2(16 CHAR) NOT NULL 27); 28 29 30-- ジャンル情報テーブルへのレコード登録 31INSERT INTO genre VALUES(1, '文学'); 32INSERT INTO genre VALUES(2, '経済'); 33INSERT INTO genre VALUES(3, '技術'); 34 35-- 書籍情報テーブルへのレコード登録 36INSERT INTO book VALUES(1, 'よくわかる経済', '山田健介', '2010/11/3', 10, 2); 37INSERT INTO book VALUES(2, '日本文学傑作選', '大原洋子', '1998/3/5', 3, 1); 38INSERT INTO book VALUES(3, '日本経済史2016', '宮本良太', '2016/7/20', 23, 2); 39 40-- ユーザ情報テーブルへのレコード登録 41INSERT INTO book_user VALUES(1, '鈴木太郎', 1111); 42INSERT INTO book_user VALUES(2, '渡辺花子', 2222); 43 44 45COMMIT; 46
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/** 16 * Servlet implementation class SelectAll 17 */ 18@WebServlet("/SelectAll") 19public class SelectAll extends HttpServlet { 20 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 List<Emp> empList =EmpDao.findAll(); 22 request.setAttribute("empList", empList); 23 request.getRequestDispatcher("/html/list/list.jsp").forward(request, response); 24 } 25} 26
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 List<Emp> findAll() { 2 3 Connection con = null; 4 PreparedStatement ps = null; 5 6 List<Emp> empList = new ArrayList<Emp>(); 7 8 try { 9 con = DBManager.getConnection(); 10 ps = con.prepareStatement("SELECT * FROM employee e INNER JOIN department d ON e.dept_id = d.dept_id"); 11 ResultSet rs = ps.executeQuery(); 12 13 while (rs.next()) { 14 15 Emp emp = new Emp(); 16 emp.setEmpId(rs.getInt("emp_id")); 17 emp.setEmpName(rs.getString("emp_name")); 18 emp.setGender(rs.getInt("gender")); 19 emp.setAddress(rs.getString("address")); 20 emp.setBirthday(rs.getDate("birthday")); 21 emp.setAuthority(rs.getInt("authority")); 22 23 Dept dept = new Dept(); 24 dept.setDeptName(rs.getString("dept_name")); 25 empList.add(emp); 26 } 27 } catch (SQLException e) { 28 e.printStackTrace(); 29 } finally { 30 DBManager.close(ps, con); 31 } 32 return empList; 33 }
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
プレビュー