From bae6f62ca2d259d7606861bc3e6c9702e75d4936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=B7=9D=E3=80=80=E8=A3=95=E6=A8=B9?= <26-y-ishikawa-stellatecnica@it-college.co.jp> Date: Thu, 29 Oct 2026 15:56:21 +0900 Subject: [PATCH] =?UTF-8?q?20261029=205=E6=AE=B5=E9=9A=8E=E7=9B=AE?= =?UTF-8?q?=E3=81=BE=E3=81=A7=E5=AE=8C=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/rhizome/example/dao/EmployeeDAO.java | 57 +++++++++++++++++- src/com/rhizome/example/main/Main.java | 63 +++++++++++++++++++- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/src/com/rhizome/example/dao/EmployeeDAO.java b/src/com/rhizome/example/dao/EmployeeDAO.java index 8c92b9c..8fd0a98 100644 --- a/src/com/rhizome/example/dao/EmployeeDAO.java +++ b/src/com/rhizome/example/dao/EmployeeDAO.java @@ -77,6 +77,7 @@ public class EmployeeDAO { return rtnList; } + //指定されたIDの社員データを取得する public List selectById(int id_employee)throws SQLException, ClassNotFoundException{ List rtnList = new ArrayList<>(); StringBuilder sql = new StringBuilder(); @@ -85,7 +86,7 @@ public class EmployeeDAO { sql.append(" FROM "); sql.append(" " + TBL_NAME); sql.append(" WHERE "); - sql.append(" ID_DEPARTMENT = ?"); + sql.append(" ID_EMPLOYEE = ?"); try { this.stmt = con.prepareStatement(sql.toString()); @@ -107,4 +108,58 @@ public class EmployeeDAO { } return rtnList; } + + //指定された社員のデータを削除する + public void empDelete(int id_employee)throws SQLException, ClassNotFoundException{ + //リスト作成 + List rtnList = new ArrayList<>(); + //SQL文の作成 + StringBuilder sql = new StringBuilder(); + sql.append(" DELETE "); + sql.append(" "); + sql.append(" FROM "); + sql.append(" " + TBL_NAME); + sql.append(" WHERE "); + sql.append(" ID_EMPLOYEE = ?"); + + try { + //SQL文の組み立て結果を保存 + this.stmt = con.prepareStatement(sql.toString()); + //?に代入 + this.stmt.setInt(1,id_employee ); + //削除命令 + stmt.executeUpdate(); + }finally { + DbUtil.closeStatement(this.stmt); + } +} + + public void empInsert(String name, String kana, String email, + String password, Integer idDep)throws SQLException, ClassNotFoundException{ + List rtnList = new ArrayList<>(); + StringBuilder sql = new StringBuilder(); + sql.append(" INSERT "); + sql.append(" " + " INTO "); + sql.append(" " + TBL_NAME); + sql.append(" ( "); + sql.append(" " + NM_EMPLOYEE + ", " + KN_EMPLOYEE + ", " + + MAIL_ADDRESS + ", " + PASSWORD + ", " + ID_DEPARTMENT); + sql.append(" ) "); + + sql.append(" VALUES "); + sql.append(" ( ?, ?, ?, ?, ?) "); + + try { + this.stmt = con.prepareStatement(sql.toString()); + this.stmt.setString(1,name ); + this.stmt.setString(2,kana ); + this.stmt.setString(3,email ); + this.stmt.setString(4,password ); + this.stmt.setInt(5,idDep ); + stmt.executeUpdate(); + }finally { + DbUtil.closeStatement(this.stmt); + } + } + } diff --git a/src/com/rhizome/example/main/Main.java b/src/com/rhizome/example/main/Main.java index e6464da..03b668b 100644 --- a/src/com/rhizome/example/main/Main.java +++ b/src/com/rhizome/example/main/Main.java @@ -54,11 +54,12 @@ public class Main { empSelect(); break; case "2": - inputCheck("id_employee"); + empInsert(); break; case "3": break; case "4": + empDelete(); break; case "9": return; @@ -88,13 +89,22 @@ public class Main { case "1": empList = empDao.selectAll(); break; + case "2": + String strId =inputCheck("ID", true); + int ID = Integer.parseInt(strId); + empList = empDao.selectById(ID); + break; default: return; } + if(empList.isEmpty()) { + System.out.println("入力された値で情報がみつかりませんでした"); + }else{ for (EmployeeDTO emp : empList) { System.out.println(emp.getId() + "\t" + emp.getName() + "\t" + emp.getKana() + "\t" + emp.getEmail() + "\t" + emp.getPass() + "\t" + emp.getIdDep()); } + } } finally { DbUtil.closeConnection(empDao.con); } @@ -149,4 +159,55 @@ public class Main { } return str; } + + public static void empDelete() throws ClassNotFoundException, SQLException { + //リスト作成 + List empList = new ArrayList<>(); + //DB接続 + Connection con = DbUtil.getConnection(); + //DAOインスタンス生成 + EmployeeDAO empDao = new EmployeeDAO(con); + try { + //入力された文字列をdelIdに保存 + String delId =inputCheck("ID", true); + //入力された文字列を整数に変換して保存 + int ID = Integer.parseInt(delId); + //入力されたIDに一致するデータを保存 + empList = empDao.selectById(ID); + //リストが空=一致なし + if(empList.isEmpty()) { + System.out.println("入力された値で情報がみつかりませんでした"); + }else { + System.out.println("指定されたIDの情報を削除します? Y/y"); + //削除の同意受付 + String agr = sc.nextLine(); + //同意が得られたら + if("Y".equals(agr) || "y".equals(agr)) { + //削除メソッド実行し削除 + empDao.empDelete(ID); + System.out.println("削除が完了しました"); + } + } + }finally { + DbUtil.closeConnection(empDao.con); + } + } + + public static void empInsert() throws ClassNotFoundException, SQLException { + Connection con = DbUtil.getConnection(); + EmployeeDAO empDao = new EmployeeDAO(con); + try { + String name = inputCheck("社員名",false); + String kana = inputCheck("フリガナ", false); + String email = inputCheck("メールアドレス", false); + String password = inputCheck("パスワード", false); + String depId = inputCheck("部署ID", true); + int dID = Integer.parseInt(depId); + + empDao.empInsert(name,kana,email,password,dID); + System.out.println("登録が完了しました"); + }finally { + DbUtil.closeConnection(empDao.con); + } + } }