From ae6a693aebc92fc905ceb4d52796a00b9ebe08bd Mon Sep 17 00:00:00 2001 From: m-shimizu <26-m-e-stella-tecnica@it-college.jp> Date: Wed, 30 Sep 2026 16:19:18 +0900 Subject: [PATCH] =?UTF-8?q?4=E6=AE=B5=E9=9A=8E=E7=9B=AE=EF=BC=88DAO?= =?UTF-8?q?=E3=81=AE=E3=83=87=E3=83=BC=E3=82=BF=E5=89=8A=E9=99=A4=E3=83=A1?= =?UTF-8?q?=E3=82=BD=E3=83=83=E3=83=89=E3=82=92=E4=BD=9C=E6=88=90=E2=86=92?= =?UTF-8?q?=E5=89=8A=E9=99=A4=E5=87=A6=E7=90=86=E5=AE=8C=E6=88=90=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/rhizome/example/dao/EmployeeDAO.java | 16 +++++++++++ src/com/rhizome/example/main/Main.java | 29 ++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/com/rhizome/example/dao/EmployeeDAO.java b/src/com/rhizome/example/dao/EmployeeDAO.java index fd28229..980b482 100644 --- a/src/com/rhizome/example/dao/EmployeeDAO.java +++ b/src/com/rhizome/example/dao/EmployeeDAO.java @@ -107,4 +107,20 @@ public class EmployeeDAO { return rtnList; } + //社員データ削除 + public void empDelete(int id) throws SQLException { + StringBuilder sql = new StringBuilder(); + sql.append(" DELETE "); + sql.append(" FROM "); + sql.append(TBL_NAME); + sql.append(" WHERE ID_EMPLOYEE = ? "); + try { + this.stmt = con.prepareStatement(sql.toString()); + this.stmt.setInt(1, id); + this.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 707b000..b8b2042 100644 --- a/src/com/rhizome/example/main/Main.java +++ b/src/com/rhizome/example/main/Main.java @@ -58,6 +58,7 @@ public class Main { case "3": break; case "4": + empDelete(); break; case "9": return; @@ -113,6 +114,34 @@ public class Main { } } + //社員テーブルから情報を削除する + public static void empDelete() + throws ClassNotFoundException, SQLException { + Connection con = DbUtil.getConnection(); + EmployeeDAO empDao = new EmployeeDAO(con); + String id = inputCheck("ID", true); + + try { + List empList = empDao.selectById(Integer.parseInt(id)); + + if (empList.isEmpty()) { + System.out.println("入力されて値で情報がみつかりませんでした"); + } else { + System.out.println("指定されたIDの情報を削除しますか? Y/y"); + System.out.println("Y/y以外の入力であればキャンセル"); + + String confirm = sc.nextLine(); + + if (confirm.equalsIgnoreCase("y")) { + empDao.empDelete(Integer.parseInt(id)); + System.out.println("削除が完了しました"); + } + } + } finally { + DbUtil.closeConnection(con); + } + } + /** * フラグを判定し、テーブル名を取得する *