10/29 追加課題 8-3まで完了

This commit is contained in:
2026-10-29 15:56:31 +09:00
parent c7f723ffe2
commit 9b993a436c
5 changed files with 606 additions and 78 deletions

View File

@@ -0,0 +1,195 @@
/*
* EmployeeDAO.java
* 作成 IT-College 2020
*------------------------------------------------------------
* Copyright(c) Rhizome Inc. All Rights Reserved.
*/
package com.rhizome.example.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.rhizome.example.dto.DepartmentDTO;
import com.rhizome.example.util.DbUtil;
public class DepartmentDAO {
private static final String TBL_NAME = "department";
private static final String ID_DEPARTMENT = "id_department";
private static final String NM_DEPARTMENT = "nm_department";
/** DBコネクション */
public Connection con;
/** DBステートメント */
PreparedStatement stmt;
/** 検索結果 */
ResultSet rs;
/**
* インスタンス生成時DBコネクション
*
* @throws ClassNotFoundException
* @throws SQLException
*/
public DepartmentDAO(Connection con) {
this.con = con;
}
/**
* 社員テーブルの情報を全件取得する
*
* @return 検索結果
* @throws SQLException
* @throws ClassNotFoundException
*/
public List<DepartmentDTO> selectAll() throws SQLException, ClassNotFoundException {
List<DepartmentDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder();
sql.append(" SELECT ");
sql.append(" " + "*");
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
try {
this.stmt = con.prepareStatement(sql.toString());
rs = stmt.executeQuery();
while (rs.next()) {
DepartmentDTO dto = new DepartmentDTO();
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
rtnList.add(dto);
}
} finally {
DbUtil.closeStatement(this.stmt);
}
return rtnList;
}
//部署ID検索
public List<DepartmentDTO> selectById(int id) throws SQLException {
//戻り値用リスト
List<DepartmentDTO> rtnList = new ArrayList<>();
//SQL作成
StringBuilder sql = new StringBuilder();
sql.append(" SELECT * ");
sql.append(" FROM department ");
sql.append(" WHERE id_department = ? ");
try {
//PreparedStatement生成
stmt = con.prepareStatement(sql.toString());
//部署IDを設定
stmt.setInt(1, id);
//SQL実行
rs = stmt.executeQuery();
//検索結果をDTOへ格納
while (rs.next()) {
//DTO生成
DepartmentDTO dto = new DepartmentDTO();
//検索結果をDTOへセット
dto.setIdDepartment( rs.getInt(ID_DEPARTMENT));
dto.setNmDepartment( rs.getString(NM_DEPARTMENT));
//リストへ追加
rtnList.add(dto);
}
} finally {
DbUtil.closeStatement(stmt);
}
//検索結果を返却
return rtnList;
}
//部署情報削除
public void deptDelete(int id) throws SQLException {
//SQL作成
StringBuilder sql = new StringBuilder();
sql.append(" DELETE ");
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" " + ID_DEPARTMENT + " = ?");
try {
//PreparedStatement生成
stmt = con.prepareStatement(sql.toString());
//値設定
stmt.setInt(1, id);
//SQL実行
stmt.executeUpdate();
} finally {
DbUtil.closeStatement(stmt);
}
}
//部署情報登録
public void deptInsert(
String departmentName)
throws SQLException {
//SQL作成
StringBuilder sql = new StringBuilder();
sql.append(" INSERT INTO department ");
sql.append(" ( ");
sql.append(" nm_department ");
sql.append(" ) ");
sql.append(" VALUES ");
sql.append(" (?) ");
try {
//PreparedStatement生成
stmt = con.prepareStatement(sql.toString());
//値設定
stmt.setString(1, departmentName);
//SQL実行
stmt.executeUpdate();
} finally {
DbUtil.closeStatement(stmt);
}
}
//部署情報更新
public void deptUpdate(
int id,
String departmentName)
throws SQLException {
//SQL作成
StringBuilder sql = new StringBuilder();
sql.append(" UPDATE department ");
sql.append(" SET ");
sql.append(" nm_department = ? ");
sql.append(" WHERE id_department = ? ");
try {
//PreparedStatement生成
stmt = con.prepareStatement(sql.toString());
//値設定
stmt.setString(1, departmentName);
stmt.setInt(2, id);
//SQL実行
stmt.executeUpdate();
} finally {
//Statement切断
DbUtil.closeStatement(stmt);
}
}
}

View File

@@ -24,6 +24,8 @@ public class EmployeeDAO {
private static final String MAIL_ADDRESS = "mail_address";
private static final String PASSWORD = "password";
private static final String ID_DEPARTMENT = "id_department";
private static final String NM_DEPARTMENT = "nm_department";
/** DBコネクション */
public Connection con;
@@ -53,9 +55,16 @@ public class EmployeeDAO {
List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder();
sql.append(" SELECT ");
sql.append(" " + "*");
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
sql.append(" employee.id_employee ");
sql.append(" ,employee.nm_employee ");
sql.append(" ,employee.kn_employee ");
sql.append(" ,employee.mail_address ");
sql.append(" ,employee.password ");
sql.append(" ,employee.id_department ");
sql.append(" ,department.nm_department ");
sql.append(" FROM employee ");
sql.append(" INNER JOIN department ");
sql.append(" ON employee.id_department = department.id_department ");
try {
this.stmt = con.prepareStatement(sql.toString());
@@ -69,6 +78,7 @@ public class EmployeeDAO {
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
dto.setPassword(rs.getString(PASSWORD));
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
rtnList.add(dto);
}
} finally {
@@ -83,9 +93,18 @@ public class EmployeeDAO {
List<EmployeeDTO> rtnList = new ArrayList<>();
//SQL作成
StringBuilder sql = new StringBuilder();
sql.append(" SELECT * ");
sql.append(" SELECT ");
sql.append(" employee.id_employee ");
sql.append(" ,employee.nm_employee ");
sql.append(" ,employee.kn_employee ");
sql.append(" ,employee.mail_address ");
sql.append(" ,employee.password ");
sql.append(" ,employee.id_department ");
sql.append(" ,department.nm_department ");
sql.append(" FROM employee ");
sql.append(" WHERE id_employee = ? ");
sql.append(" INNER JOIN department ");
sql.append(" ON employee.id_department = department.id_department ");
sql.append(" WHERE employee.id_employee = ? ");
try {
//PreparedStatement生成
@@ -105,6 +124,7 @@ public class EmployeeDAO {
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
dto.setPassword(rs.getString(PASSWORD));
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
//リストへ追加
rtnList.add(dto);

View File

@@ -0,0 +1,23 @@
package com.rhizome.example.dto;
public class DepartmentDTO {
private Integer idDepartment;
private String nmDepartment;
public Integer getIdDepartment() {
return idDepartment;
}
public void setIdDepartment(Integer idDepartment) {
this.idDepartment = idDepartment;
}
public String getNmDepartment() {
return nmDepartment;
}
public void setNmDepartment(String nmDepartment) {
this.nmDepartment = nmDepartment;
}
}

View File

@@ -8,6 +8,7 @@ public class EmployeeDTO {
private String mailAddress;
private String password;
private Integer idDepartment;
private String nmDepartment;
public Integer getIdEmployee() {
@@ -46,5 +47,13 @@ public class EmployeeDTO {
public void setIdDepartment(Integer idDepartment) {
this.idDepartment = idDepartment;
}
public String getNmDepartment() {
return nmDepartment;
}
public void setNmDepartment(String nmDepartment) {
this.nmDepartment = nmDepartment;
}
}

View File

@@ -6,7 +6,9 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.rhizome.example.dao.DepartmentDAO;
import com.rhizome.example.dao.EmployeeDAO;
import com.rhizome.example.dto.DepartmentDTO;
import com.rhizome.example.dto.EmployeeDTO;
import com.rhizome.example.util.DbUtil;
@@ -51,18 +53,51 @@ public class Main {
String ope = sc.nextLine();
switch (ope) {
case "1":
empSelect();
//社員テーブル
if (tblFlg == 0) {
empSelect();
//部署テーブル
} else {
deptSelect();
}
break;
case "2":
empInsert();
//社員テーブル
if (tblFlg == 0) {
empInsert();
//部署テーブル
} else {
deptInsert();
}
break;
case "3":
empUpdate();
//社員テーブル
if (tblFlg == 0) {
empUpdate();
//部署テーブル
} else {
deptUpdate();
}
break;
case "4":
empDelete();
//社員テーブル
if (tblFlg == 0) {
empDelete();
//部署テーブル
} else {
deptDelete();
}
break;
case "9":
if (tblFlg == 0) {
tblFlg = 1;
} else {
tblFlg = 0;
}
return;
case "0":
System.out.println("システムを終了します");
@@ -107,21 +142,79 @@ public class Main {
return;
}
//最大文字数取得
int maxName = 0;
int maxKana = 0;
int maxMail = 0;
int maxPassword = 0;
int maxDepartment = 0;
for (EmployeeDTO emp : empList) {
if (getDisplayLength(emp.getNmEmployee()) > maxName) {
maxName = getDisplayLength(emp.getNmEmployee());
}
if (getDisplayLength(emp.getKnEmployee()) > maxKana) {
maxKana = getDisplayLength(emp.getKnEmployee());
}
if (getDisplayLength(emp.getMailAddress()) > maxMail) {
maxMail = getDisplayLength(emp.getMailAddress());
}
if (getDisplayLength(emp.getPassword()) > maxPassword) {
maxPassword = getDisplayLength(emp.getPassword());
}
if (getDisplayLength(emp.getNmDepartment())
> maxDepartment) {
maxDepartment = getDisplayLength(emp.getNmDepartment());
}
}
for (EmployeeDTO emp : empList) {
System.out.println(
emp.getIdEmployee() + "\t"
+ emp.getNmEmployee() + "\t"
+ emp.getKnEmployee() + "\t"
+ emp.getMailAddress() + "\t"
+ emp.getPassword() + "\t"
+ emp.getIdDepartment()
);
adjust(String.valueOf(emp.getIdEmployee()),10)
+ adjust(emp.getNmEmployee(), maxName + 5)
+ adjust(emp.getKnEmployee(), maxKana + 5)
+ adjust(emp.getMailAddress(), maxMail + 5)
+ adjust(emp.getPassword(), maxPassword + 5)
+ adjust(emp.getNmDepartment(), maxDepartment + 5));
}
} finally {
DbUtil.closeConnection(empDao.con);
}
}
//表示幅取得
//半角=1
//全角=2
public static int getDisplayLength(String str) {
int length = 0;
for (char c : str.toCharArray()) {
//ASCIIコードで算出
if (c <= 127) {
length += 1;
} else {
length += 2;
}
}
return length;
}
//指定文字数まで空白を追加
public static String adjust(String str, int length) {
StringBuilder sb = new StringBuilder(str);
while (getDisplayLength(sb.toString()) < length) {
sb.append(" ");
}
return sb.toString();
}
/**
* フラグを判定し、テーブル名を取得する
@@ -172,6 +265,28 @@ public class Main {
}
return str;
}
//更新用入力
//Enterのみの場合は現在値を返す
public static String inputUpdate(
String titleName,
String currentValue) {
System.out.println(
titleName
+ "を入力してください"
+ "(現在値:" + currentValue + ")");
String str = sc.nextLine();
//Enterのみの場合は現在値を返却
if (str.trim().length() == 0) {
return currentValue;
}
return str;
}
//社員情報を削除する
public static void empDelete() throws ClassNotFoundException, SQLException {
//DB接続
@@ -211,88 +326,254 @@ public class Main {
DbUtil.closeConnection(empDao.con);
}
}
//社員情報登録
public static void empInsert() throws ClassNotFoundException, SQLException {
//DB接続
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
//DB接続
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
try {
//社員名入力
String name = inputCheck("社員名", false);
//フリガナ入力
String kana = inputCheck("フリガナ", false);
//メールアドレス入力
String mail = inputCheck("メールアドレス", false);
//パスワード入力
String password = inputCheck("パスワード", false);
//部署ID入力
String departmentId = inputCheck("部署ID", true);
//登録処理
empDao.empInsert(
name,
kana,
mail,
password,
Integer.parseInt(departmentId));
//社員名入力
String name = inputCheck("社員名", false);
//フリガナ入力
String kana = inputCheck("フリガナ", false);
//メールアドレス入力
String mail = inputCheck("メールアドレス", false);
//パスワード入力
String password = inputCheck("パスワード", false);
//部署ID入力
String departmentId = inputCheck("部署ID", true);
//登録処理
empDao.empInsert(
name,
kana,
mail,
password,
Integer.parseInt(departmentId));
System.out.println("登録が完了しました");
System.out.println("登録が完了しました");
} finally {
} finally {
DbUtil.closeConnection(empDao.con);
}
DbUtil.closeConnection(empDao.con);
}
}
//社員情報更新
public static void empUpdate() throws ClassNotFoundException, SQLException {
//DB接続
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
//DB接続
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
//更新対象ID入力
String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId);
try {
//更新対象ID入力
String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId);
//存在チェック
List<EmployeeDTO> empList = empDao.selectById(id);
//存在チェック
List<EmployeeDTO> empList = empDao.selectById(id);
if (empList.size() == 0) {
System.out.println("入力された値で情報がみつかりませんでした");
if (empList.size() == 0) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
return;
}
//社員名入力
String name = inputCheck("社員名", false);
//フリガナ入力
String kana = inputCheck("フリガナ", false);
//メールアドレス入力
String mail = inputCheck("メールアドレス", false);
//パスワード入力
String password = inputCheck("パスワード", false);
//部署ID入力
String departmentId = inputCheck("部署ID", true);
//更新処理
empDao.empUpdate(
id,
name,
kana,
mail,
password,
Integer.parseInt(departmentId));
EmployeeDTO emp = empList.get(0);
System.out.println("更新が完了しました");
//Enterのみの場合は現在値をそのまま使用
//社員名入力
String name = inputUpdate("社員名", emp.getNmEmployee());
//フリガナ入力
String kana = inputUpdate("フリガナ", emp.getKnEmployee());
//メールアドレス入力
String mail = inputUpdate("メールアドレス", emp.getMailAddress());
//パスワード入力
String password = inputUpdate("パスワード", emp.getPassword());
//部署ID入力
String departmentId = inputUpdate("部署ID", String.valueOf(emp.getIdDepartment()));
//更新処理
empDao.empUpdate(
id,
name,
kana,
mail,
password,
Integer.parseInt(departmentId));
} finally {
System.out.println("更新が完了しました");
DbUtil.closeConnection(empDao.con);
}
} finally {
DbUtil.closeConnection(empDao.con);
}
}
//テーブル切り替え
//ここから部署テーブル
//部署テーブルから情報を検索する
public static void deptSelect() throws ClassNotFoundException, SQLException {
//検索結果格納用リスト
List<DepartmentDTO> deptList = new ArrayList<>();
//DB接続
Connection con = DbUtil.getConnection();
//DAO生成
DepartmentDAO deptDao = new DepartmentDAO(con);
//検索方法選択
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
String select = sc.nextLine();
try {
switch (select) {
//全件検索
case "1":
deptList = deptDao.selectAll();
break;
//条件検索
case "2":
//部署ID入力
String id = inputCheck("ID", true);
//ID検索実行
deptList = deptDao.selectById(Integer.parseInt(id));
break;
//メニューへ戻る
default:
return;
}
//検索結果0件の場合
if (deptList.size() == 0) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
//検索結果表示
for (DepartmentDTO dept : deptList) {
System.out.println(
dept.getIdDepartment()
+ "\t"
+ dept.getNmDepartment());
}
} finally {
DbUtil.closeConnection(deptDao.con);
}
}
//部署情報登録
public static void deptInsert() throws ClassNotFoundException, SQLException {
//DB接続
Connection con = DbUtil.getConnection();
//DAO生成
DepartmentDAO deptDao =
new DepartmentDAO(con);
try {
//部署名入力
String departmentName = inputCheck("部署名", false);
//登録処理
deptDao.deptInsert(departmentName);
System.out.println("登録が完了しました");
} finally {
DbUtil.closeConnection(deptDao.con);
}
}
//部署情報更新
public static void deptUpdate() throws ClassNotFoundException, SQLException {
//DB接続
Connection con = DbUtil.getConnection();
//DAO生成
DepartmentDAO deptDao = new DepartmentDAO(con);
try {
//更新対象ID入力
String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId);
//存在チェック
List<DepartmentDTO> deptList = deptDao.selectById(id);
//データが存在しない場合
if (deptList.size() == 0) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
//部署名入力
String departmentName = inputCheck("部署名", false);
//更新処理
deptDao.deptUpdate(id, departmentName);
System.out.println("更新が完了しました");
} finally {
DbUtil.closeConnection(
deptDao.con);
}
}
//部署情報を削除する
public static void deptDelete() throws ClassNotFoundException, SQLException {
//DB接続
Connection con = DbUtil.getConnection();
//DAO生成
DepartmentDAO deptDao = new DepartmentDAO(con);
try {
//削除対象ID入力
String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId);
//入力されたIDの存在確認
List<DepartmentDTO> deptList = deptDao.selectById(id);
//データが存在しない場合
if (deptList.size() == 0) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
//削除確認メッセージ表示
System.out.println("指定されたIDの情報を削除しますか? Y/y");
System.out.println("(Y/y以外の入力であればキャンセル)");
String answer = sc.nextLine();
//Yまたはyが入力された場合のみ削除
if ("Y".equals(answer) || "y".equals(answer)) {
//部署情報削除
deptDao.deptDelete(id);
System.out.println("削除が完了しました");
}
} finally {
DbUtil.closeConnection(deptDao.con);
}
}
}