diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..8ddd4c7 --- /dev/null +++ b/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..2e9776e --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + em-jdbc + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.wst.jsdt.core.jsNature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/.jsdtscope b/.settings/.jsdtscope new file mode 100644 index 0000000..cca691f --- /dev/null +++ b/.settings/.jsdtscope @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.container b/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000..49c8cd4 --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.JRE_CONTAINER \ No newline at end of file diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.name b/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000..11006e2 --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Global \ No newline at end of file diff --git a/src/com/rhizome/example/dao/EmployeeDAO.java b/src/com/rhizome/example/dao/EmployeeDAO.java index ef63c49..28cef51 100644 --- a/src/com/rhizome/example/dao/EmployeeDAO.java +++ b/src/com/rhizome/example/dao/EmployeeDAO.java @@ -13,6 +13,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import com.rhizome.example.dto.EmployeeDTO; import com.rhizome.example.util.DbUtil; public class EmployeeDAO { @@ -48,8 +49,8 @@ public class EmployeeDAO { * @throws SQLException * @throws ClassNotFoundException */ - public List selectAll() throws SQLException, ClassNotFoundException { - List rtnList = new ArrayList<>(); + public List selectAll() throws SQLException, ClassNotFoundException { + List rtnList = new ArrayList<>(); StringBuilder sql = new StringBuilder(); sql.append(" SELECT "); sql.append(" " + "*"); @@ -60,7 +61,15 @@ public class EmployeeDAO { this.stmt = con.prepareStatement(sql.toString()); rs = stmt.executeQuery(); while (rs.next()) { - rtnList.add(rs.getString(NM_EMPLOYEE)); + EmployeeDTO dto = new EmployeeDTO(); + + dto.setIdEmployee(rs.getInt(ID_EMPLOYEE)); + dto.setNmEmployee(rs.getString(NM_EMPLOYEE)); + dto.setKnEmployee(rs.getString(KN_EMPLOYEE)); + dto.setMailAddress(rs.getString(MAIL_ADDRESS)); + dto.setPassword(rs.getString(PASSWORD)); + dto.setIdDepartment(rs.getInt(ID_DEPARTMENT)); + rtnList.add(dto); } } finally { DbUtil.closeStatement(this.stmt); @@ -68,4 +77,111 @@ public class EmployeeDAO { return rtnList; } + //社員ID検索 + public List selectById(int id) throws SQLException { + //戻り値用リスト + List rtnList = new ArrayList<>(); + //SQL作成 + StringBuilder sql = new StringBuilder(); + sql.append(" SELECT * "); + sql.append(" FROM employee "); + sql.append(" WHERE id_employee = ? "); + + try { + //PreparedStatement生成 + stmt = con.prepareStatement(sql.toString()); + //社員IDを設定 + stmt.setInt(1, id); + //SQL実行 + rs = stmt.executeQuery(); + //検索結果をDTOへ格納 + while (rs.next()) { + //DTO生成 + EmployeeDTO dto = new EmployeeDTO(); + //検索結果をDTOへセット + dto.setIdEmployee(rs.getInt(ID_EMPLOYEE)); + dto.setNmEmployee(rs.getString(NM_EMPLOYEE)); + dto.setKnEmployee(rs.getString(KN_EMPLOYEE)); + dto.setMailAddress(rs.getString(MAIL_ADDRESS)); + dto.setPassword(rs.getString(PASSWORD)); + dto.setIdDepartment(rs.getInt(ID_DEPARTMENT)); + + //リストへ追加 + rtnList.add(dto); + } + + } finally { + + DbUtil.closeStatement(stmt); + } + //検索結果を返却 + return rtnList; + } + + //社員情報削除 + public void empDelete(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_EMPLOYEE + " = ?"); + + try { + //PreparedStatement生成 + stmt = con.prepareStatement(sql.toString()); + //値設定 + stmt.setInt(1, id); + //SQL実行 + stmt.executeUpdate(); + + } finally { + + DbUtil.closeStatement(stmt); + } + } + + //社員情報登録 + public void empInsert( + String name, + String kana, + String mail, + String password, + int departmentId) + throws SQLException { + //SQL作成 + StringBuilder sql = new StringBuilder(); + + sql.append(" INSERT INTO employee "); + sql.append(" ( "); + sql.append(" nm_employee, "); + sql.append(" kn_employee, "); + sql.append(" mail_address, "); + sql.append(" password, "); + sql.append(" id_department "); + sql.append(" ) "); + sql.append(" VALUES "); + sql.append(" (?, ?, ?, ?, ?) "); + + try { + + //PreparedStatement生成 + stmt = con.prepareStatement(sql.toString()); + //値設定 + stmt.setString(1, name); + stmt.setString(2, kana); + stmt.setString(3, mail); + stmt.setString(4, password); + stmt.setInt(5, departmentId); + + //SQL実行 + stmt.executeUpdate(); + + } finally { + + DbUtil.closeStatement(stmt); + } + } + } diff --git a/src/com/rhizome/example/dto/EmployeeDTO.java b/src/com/rhizome/example/dto/EmployeeDTO.java new file mode 100644 index 0000000..f879e39 --- /dev/null +++ b/src/com/rhizome/example/dto/EmployeeDTO.java @@ -0,0 +1,50 @@ +package com.rhizome.example.dto; + +public class EmployeeDTO { + + private Integer idEmployee; + private String nmEmployee; + private String knEmployee; + private String mailAddress; + private String password; + private Integer idDepartment; + + + public Integer getIdEmployee() { + return idEmployee; + } + public void setIdEmployee(Integer idEmployee) { + this.idEmployee = idEmployee; + } + public String getNmEmployee() { + return nmEmployee; + } + public void setNmEmployee(String nmEmployee) { + this.nmEmployee = nmEmployee; + } + public String getKnEmployee() { + return knEmployee; + } + public void setKnEmployee(String knEmployee) { + this.knEmployee = knEmployee; + } + public String getMailAddress() { + return mailAddress; + } + public void setMailAddress(String mailAddress) { + this.mailAddress = mailAddress; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + public Integer getIdDepartment() { + return idDepartment; + } + public void setIdDepartment(Integer idDepartment) { + this.idDepartment = idDepartment; + } + +} \ 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 14fb220..e7c2741 100644 --- a/src/com/rhizome/example/main/Main.java +++ b/src/com/rhizome/example/main/Main.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Scanner; import com.rhizome.example.dao.EmployeeDAO; +import com.rhizome.example.dto.EmployeeDTO; import com.rhizome.example.util.DbUtil; /** @@ -53,10 +54,12 @@ public class Main { empSelect(); break; case "2": + empInsert(); break; case "3": break; case "4": + empDelete(); break; case "9": return; @@ -76,7 +79,7 @@ public class Main { * @throws SQLException */ public static void empSelect() throws ClassNotFoundException, SQLException { - List empList = new ArrayList<>(); + List empList = new ArrayList<>(); Connection con = DbUtil.getConnection(); EmployeeDAO empDao = new EmployeeDAO(con); System.out.println("1:全検索 2:条件検索 それ以外:戻る"); @@ -86,11 +89,33 @@ public class Main { case "1": empList = empDao.selectAll(); break; + + case "2": + + String id = inputCheck("ID", true); + empList = empDao.selectById( + Integer.parseInt(id)); + break; + default: return; } - for (String emp : empList) { - System.out.println(emp); + + if (empList.size() == 0) { + System.out.println("入力された値で情報がみつかりませんでした"); + return; + } + + for (EmployeeDTO emp : empList) { + + System.out.println( + emp.getIdEmployee() + "\t" + + emp.getNmEmployee() + "\t" + + emp.getKnEmployee() + "\t" + + emp.getMailAddress() + "\t" + + emp.getPassword() + "\t" + + emp.getIdDepartment() + ); } } finally { DbUtil.closeConnection(empDao.con); @@ -146,4 +171,79 @@ public class Main { } return str; } + //社員情報を削除する + public static void empDelete() throws ClassNotFoundException, SQLException { + //DB接続 + Connection con = DbUtil.getConnection(); + EmployeeDAO empDao = new EmployeeDAO(con); + + try { + //削除対象ID入力 + String strId = inputCheck("ID", true); + int id = Integer.parseInt(strId); + + //入力されたID確認 + List empList = empDao.selectById(id); + //データが存在しない場合 + if (empList.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)) { + //社員情報削除 + empDao.empDelete(id); + + System.out.println("削除が完了しました"); + } + + } finally { + + DbUtil.closeConnection(empDao.con); + } + } + + //社員情報登録 + public static void empInsert() throws ClassNotFoundException, SQLException { + + //DB接続 + Connection con = DbUtil.getConnection(); + EmployeeDAO empDao = new EmployeeDAO(con); + + 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)); + + System.out.println("登録が完了しました"); + + } finally { + + DbUtil.closeConnection(empDao.con); + } + } + } diff --git a/src/com/rhizome/example/util/DbUtil.java b/src/com/rhizome/example/util/DbUtil.java index d2bb82e..8d7ff37 100644 --- a/src/com/rhizome/example/util/DbUtil.java +++ b/src/com/rhizome/example/util/DbUtil.java @@ -1,6 +1,9 @@ package com.rhizome.example.util; import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; public class DbUtil { // 定数定義(共通) @@ -42,6 +45,18 @@ public class DbUtil { * ※Connection生成には上記の定数を活用してください */ + public static Connection getConnection() + throws ClassNotFoundException, SQLException { + + Class.forName(DB_DRIVER); + + con = DriverManager.getConnection( + DB_URL, + DB_USER, + DB_PASS); + + return con; + } /* * TODO 以下の仕様でメソッドを作成してください。 @@ -53,6 +68,14 @@ public class DbUtil { * 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする */ + public static void closeStatement(PreparedStatement stmt) + throws SQLException { + + if (stmt != null) { + stmt.close(); + } + } + /* * TODO 以下の仕様でメソッドを作成してください。 @@ -64,5 +87,11 @@ public class DbUtil { * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する */ + public static void closeConnection(Connection con) + throws SQLException { + if (con != null) { + con.close(); + } + } }