From a6bd282b1c0b0adc74715be91565fab1f62e208d Mon Sep 17 00:00:00 2001 From: s-sakiyama <26-m-s-stella-tecnica@it-college.jp> Date: Wed, 30 Sep 2026 12:39:56 +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 | 35 ++++++++++-- src/com/rhizome/example/main/Main.java | 56 ++++++++++++++++++++ 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/src/com/rhizome/example/dao/EmployeeDAO.java b/src/com/rhizome/example/dao/EmployeeDAO.java index 867c635..0111ff5 100644 --- a/src/com/rhizome/example/dao/EmployeeDAO.java +++ b/src/com/rhizome/example/dao/EmployeeDAO.java @@ -12,7 +12,6 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.Scanner; import com.rhizome.example.dto.EmployeeDTO; import com.rhizome.example.util.DbUtil; @@ -75,7 +74,8 @@ public class EmployeeDAO { /** * 社員テーブルから社員IDに合致する情報を取得する - * + * @param id + * 検索する社員ID * @return 検索結果 * @throws SQLException * @throws ClassNotFoundException @@ -84,7 +84,6 @@ public class EmployeeDAO { public List selectById​(int id) throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException { List rtnList = new ArrayList<>(); - Scanner scanner = new Scanner(System.in); StringBuilder sql = new StringBuilder(); sql.append(" SELECT "); sql.append(" " + "*"); @@ -114,4 +113,34 @@ public class EmployeeDAO { } return rtnList; } + + /** + * 社員テーブルの社員IDに合致する情報を削除する + * + * @param id + * 削除ID + * @throws ClassNotFoundException + * @throws SQLException + * @throws NumberFormatException + */ + public void empDelete​(int id) + throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException { + StringBuilder sql = new StringBuilder(); + sql.append(" DELETE "); + sql.append(" FROM "); + sql.append(" " + TBL_NAME); + sql.append(" WHERE "); + sql.append(" " + ID_EMPLOYEE); + sql.append(" = "); + sql.append(" " + id); + + try { + this.stmt = con.prepareStatement(sql.toString()); + stmt.executeUpdate(); + System.out.println("削除が完了しました"); + } finally { + DbUtil.closeStatement(this.stmt); + } + + } } diff --git a/src/com/rhizome/example/main/Main.java b/src/com/rhizome/example/main/Main.java index ecbb5bf..83086c3 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; @@ -70,6 +71,39 @@ public class Main { } } + /** + * 社員テーブルの社員IDに合致する情報を削除する + * + * @throws ClassNotFoundException + * @throws SQLException + */ + public static void empDelete​() + throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException { + List rtnList = new ArrayList<>(); + Connection con = DbUtil.getConnection(); + EmployeeDAO empDao = new EmployeeDAO(con); + int id = Integer.parseInt(inputCheck("ID", true)); + rtnList = empDao.selectById​(id); + if (rtnList.size() == 0) { + return; + } + + System.out.println("指定されたIDの情報を削除します? Y/y\r\n" + + "(Y/y以外の入力であればキャンセル)"); + + String str = inputCheck(false); + + if (!str.equalsIgnoreCase("y")) { + System.out.println("キャンセルしました"); + return; + } + try { + empDao.empDelete​(id); + } finally { + DbUtil.closeConnection(empDao.con); + } + } + /** * 社員テーブルから情報を検索する * @@ -150,4 +184,26 @@ public class Main { } return str; } + + public static String inputCheck(boolean flg) { + String str = null; + while (true) { + str = sc.nextLine(); + if (str.trim().length() == 0) { + + continue; + } + if (flg) { + if (str.trim().length() > 0) { + try { + Integer.parseInt(str); + } catch (NumberFormatException e) { + continue; + } + } + } + break; + } + return str; + } }