From 6493baccef14b456f43d144e6e094bf0ea5503ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=93=A6=E3=80=80=E6=98=B4=E4=B8=96?= <26-k-kawara-rootship@it-college.co.jp> Date: Fri, 30 Oct 2026 16:48:29 +0900 Subject: [PATCH] =?UTF-8?q?=E5=BF=85=E9=A0=88=E6=AE=B5=E9=9A=8E=E5=AE=9F?= =?UTF-8?q?=E8=A3=85=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 | 92 +++++++++++--------- src/com/rhizome/example/main/Main.java | 38 ++++---- 2 files changed, 69 insertions(+), 61 deletions(-) diff --git a/src/com/rhizome/example/dao/EmployeeDAO.java b/src/com/rhizome/example/dao/EmployeeDAO.java index 21b8ec2..d36e380 100644 --- a/src/com/rhizome/example/dao/EmployeeDAO.java +++ b/src/com/rhizome/example/dao/EmployeeDAO.java @@ -78,7 +78,8 @@ public class EmployeeDAO { return rtnList; } - public java.util.List selectById​(int id) throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException { + public java.util.List selectById​(int id) + throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException { List rtnList = new ArrayList<>(); StringBuilder sql = new StringBuilder(); sql.append(" SELECT * FROM "); @@ -128,52 +129,57 @@ public class EmployeeDAO { DbUtil.closeStatement​(this.stmt); } } - - public void empInsert​(java.lang.String name, java.lang.String kana, java.lang.String mail, java.lang.String pass, int depId) throws java.lang.NumberFormatException, java.sql.SQLException, java.lang.ClassNotFoundException{ + + public void empInsert​(java.lang.String name, java.lang.String kana, java.lang.String mail, java.lang.String pass, + int depId) throws java.lang.NumberFormatException, java.sql.SQLException, java.lang.ClassNotFoundException { //List rtnList = new ArrayList<>(); StringBuilder sql = new StringBuilder(); sql.append(" INSERT INTO " + TBL_NAME); - sql.append(" (nm_employee, kn_employee, mail_address, password, id_department) "); - 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, mail); - this.stmt.setString(4, pass); - this.stmt.setInt(5, depId); - - this.stmt.executeUpdate(); - - } finally { - DbUtil.closeStatement​(this.stmt); - } - } - - public void empUpdate​(int id, java.lang.String name, java.lang.String kana, java.lang.String mail, java.lang.String pass, int depId) throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException{ - StringBuilder sql = new StringBuilder(); - sql.append(" UPDATE " + TBL_NAME + " SET "); - sql.append(" (id_employee=?,nm_employee=?, kn_employee=?, mail_address=?, password=?, id_department=?) "); - sql.append(" WHERE id_employee = ? "); - + sql.append(" (nm_employee, kn_employee, mail_address, password, id_department) "); + sql.append(" VALUES "); + sql.append(" (?, ?, ?, ?, ?) "); + try { - this.stmt = con.prepareStatement(sql.toString()); - - this.stmt.setInt(1, id); - this.stmt.setString(2, name); - this.stmt.setString(3, kana); - this.stmt.setString(4, mail); - this.stmt.setString(5, pass); - this.stmt.setInt(6, depId); - - this.stmt.executeUpdate(); - - } finally { - DbUtil.closeStatement​(this.stmt); - } + this.stmt = con.prepareStatement(sql.toString()); + + this.stmt.setString(1, name); + this.stmt.setString(2, kana); + this.stmt.setString(3, mail); + this.stmt.setString(4, pass); + this.stmt.setInt(5, depId); + + this.stmt.executeUpdate(); + + } finally { + DbUtil.closeStatement​(this.stmt); + } + } + + public void empUpdate​(int id, java.lang.String name, java.lang.String kana, java.lang.String mail, + java.lang.String pass, int depId) + throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException { + StringBuilder sql = new StringBuilder(); + sql.append(" UPDATE "); + sql.append(TBL_NAME); + sql.append(" SET "); + sql.append(" nm_employee = ?, kn_employee = ?, mail_address = ?, password = ?, id_department = ? "); + sql.append(" WHERE id_employee = ? "); + + try { + this.stmt = con.prepareStatement(sql.toString()); + + this.stmt.setString(1, name); + this.stmt.setString(2, kana); + this.stmt.setString(3, mail); + this.stmt.setString(4, pass); + this.stmt.setInt(5, depId); + this.stmt.setInt(6, id); + + this.stmt.executeUpdate(); + + } finally { + DbUtil.closeStatement​(this.stmt); + } } } \ No newline at end of file diff --git a/src/com/rhizome/example/main/Main.java b/src/com/rhizome/example/main/Main.java index 1d2809e..31b7a4e 100644 --- a/src/com/rhizome/example/main/Main.java +++ b/src/com/rhizome/example/main/Main.java @@ -110,22 +110,20 @@ public class Main { ); } - + } finally { DbUtil.closeConnection​(empDao.con); } } public static void empInsert() - throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException { + throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException { //List empList = new ArrayList<>(); Connection con = DbUtil.getConnection(); EmployeeDAO empDao = new EmployeeDAO(con); try { - - - + String name = inputCheck("社員名", false); String kana = inputCheck("フリガナ", false); String mail = inputCheck("メールアドレス", false); @@ -143,35 +141,39 @@ public class Main { } } - - public static void empUpdate() throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException{ + + public static void empUpdate() + throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException { Connection con = DbUtil.getConnection(); EmployeeDAO empDao = new EmployeeDAO(con); - + try { - - - String idstr = inputCheck("ID",true); + + String idstr = inputCheck("ID", true); + int id = Integer.parseInt(idstr); + + List checkList = empDao.selectById​(id); + if (checkList.size() == 0) { + System.out.println("入力された値で情報がみつかりませんでした"); + return; + } + String name = inputCheck("社員名", false); String kana = inputCheck("フリガナ", false); String mail = inputCheck("メールアドレス", false); String pass = inputCheck("パスワード", false); String depIdstr = inputCheck("部署ID", true); - - int id = Integer.parseInt(idstr); - + int depId = Integer.parseInt(depIdstr); - System.out.println("更新が完了しました"); + empDao.empUpdate​(id, name, kana, mail, pass, depId); - empDao.empUpdate​(id,name, kana, mail, pass, depId); + System.out.println("更新が完了しました"); } finally { DbUtil.closeConnection​(empDao.con); } - - } public static void empDelete() throws ClassNotFoundException, SQLException {