4 Commits

Author SHA1 Message Date
0062b9fce4 4段階目終了 2026-09-30 14:32:50 +09:00
c65c3b1db8 3段階目終了 2026-09-30 13:02:23 +09:00
e5fef82677 2段階目終了 2026-09-29 16:53:16 +09:00
4f60de10c4 1段階目終了 2026-09-29 16:15:11 +09:00
5 changed files with 66 additions and 138 deletions

View File

@@ -62,13 +62,12 @@ public class EmployeeDAO {
rs = stmt.executeQuery();
while (rs.next()) {
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));
dto.setID_EMPLOYEE(rs.getInt("ID_EMPLOYEE"));
dto.setNM_EMPLOYEE(rs.getString("NM_EMPLOYEE"));
dto.setKN_EMPLOYEE(rs.getString("KN_EMPLOYEE"));
dto.setMAIL_ADDRESS(rs.getString("MAIL_ADDRESS"));
dto.setPASSWORD(rs.getString("PASSWORD"));
dto.setID_DEPARTMENT(rs.getInt("ID_DEPARTMENT"));
rtnList.add(dto);
}
} finally {
@@ -77,50 +76,42 @@ public class EmployeeDAO {
return rtnList;
}
public List<EmployeeDTO> selectById(int id) throws SQLException, ClassNotFoundException {
List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder();
sql.append(" SELECT ");
sql.append(" * ");
sql.append(" FROM ");
sql.append(TBL_NAME);
sql.append(" WHERE ID_EMPLOYEE = ? ");
//社員テーブルから指定された社員IDに一致するデータを取得し、DTOに設定の上、リスト形式で戻
public List<EmployeeDTO> selectById(int id_employee) throws SQLException {
List<EmployeeDTO> list = new ArrayList<>();
String sql = "SELECT * FROM employee WHERE ID_EMPLOYEE = ?";
try {
this.stmt = con.prepareStatement(sql.toString());
this.stmt.setInt(1, id);
rs = this.stmt.executeQuery();
this.stmt.setInt(1, id_employee);
this.rs = this.stmt.executeQuery();
while (rs.next()) {
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);
dto.setID_EMPLOYEE(rs.getInt("ID_EMPLOYEE"));
dto.setNM_EMPLOYEE(rs.getString("NM_EMPLOYEE"));
dto.setKN_EMPLOYEE(rs.getString("KN_EMPLOYEE"));
dto.setMAIL_ADDRESS(rs.getString("MAIL_ADDRESS"));
dto.setPASSWORD(rs.getString("PASSWORD"));
dto.setID_DEPARTMENT(rs.getInt("ID_DEPARTMENT"));
list.add(dto);
}
} finally {
DbUtil.closeStatement(this.stmt);
}
return rtnList;
return list;
}
//社員データ削除
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 = ? ");
// 社員テーブルから指定された社員IDに一致するデータを削除する
public void empDelete(int id_employee) throws SQLException {
String sql = "DELETE FROM employee WHERE ID_EMPLOYEE = ?";
try {
this.stmt = con.prepareStatement(sql.toString());
this.stmt.setInt(1, id);
this.stmt = con.prepareStatement(sql);
this.stmt.setInt(1, id_employee);
this.stmt.executeUpdate();
} finally {
DbUtil.closeStatement(this.stmt);
}
}
}

View File

@@ -1,59 +0,0 @@
package com.rhizome.example.dto;
public class EmployeeDTO {
private int idEmployee;
private String nmEmployee;
private String knEmployee;
private String mailAddress;
private String password;
private int idDepartment;
public int getIdEmployee() {
return idEmployee;
}
public void setIdEmployee(int 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 int getIdDepartment() {
return idDepartment;
}
public void setIdDepartment(int idDepartment) {
this.idDepartment = idDepartment;
}
}

View File

@@ -89,56 +89,55 @@ public class Main {
empList = empDao.selectAll();
break;
case "2":
String id = inputCheck("ID", true);
empList = empDao.selectById(Integer.parseInt(id));
String inputId = inputCheck("検索する社員ID", true);
int input = Integer.parseInt(inputId);
empList = empDao.selectById(input);
break;
default:
return;
}
if (empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
} else {
for (EmployeeDTO emp : empList) {
System.out.println(
emp.getIdEmployee() + " "
+ emp.getNmEmployee() + " "
+ emp.getKnEmployee() + " "
+ emp.getMailAddress() + " "
+ emp.getPassword() + " "
+ emp.getIdDepartment());
}
for (EmployeeDTO emp : empList) {
System.out.println(emp);
}
} finally {
DbUtil.closeConnection(empDao.con);
}
}
//社員テーブルから情報を削除する
public static void empDelete()
throws ClassNotFoundException, SQLException {
public static void empDelete() throws ClassNotFoundException, SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
String id = inputCheck("ID", true);
try {
List<EmployeeDTO> 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("削除が完了しました");
}
System.out.println("IDを入力してください");
String input = sc.nextLine();
// 空文字が入力された場合
if (input.strip().isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
int inputId = Integer.parseInt(input.strip());
List<EmployeeDTO> empList = empDao.selectById(inputId);
// 検索結果が存在しないIDの場合
if (empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
System.out.println("指定されたIDの情報を削除します? Y/y");
System.out.println("(Y/y以外の入力であればキャンセル)");
String confilm = sc.nextLine();
// 検索結果が存在した場合
if ("Y".equalsIgnoreCase(confilm)) {
empDao.empDelete(inputId);
System.out.println("社員ID" + input + "のデータを削除しました");
} else {
System.out.println("削除をキャンセルしました");
}
} catch (NumberFormatException e) {
System.out.println("入力された値で情報がみつかりませんでした");
} finally {
DbUtil.closeConnection(con);
DbUtil.closeConnection(empDao.con);
}
}

View File

@@ -44,12 +44,10 @@ public class DbUtil {
* 生成されたConnectionを返却する
* ※Connection生成には上記の定数を活用してください
*/
public static Connection getConnection()
throws ClassNotFoundException, SQLException {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName(DB_DRIVER);
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
return con;
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
}
/*
@@ -61,11 +59,11 @@ public class DbUtil {
* 例外 :SQLException
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
*/
public static void closeStatement(PreparedStatement stmt)
throws SQLException {
public static void closeStatement(PreparedStatement stmt) throws SQLException {
if (stmt != null) {
stmt.close();
}
}
/*
@@ -77,8 +75,7 @@ public class DbUtil {
* 例外 :SQLException
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/
public static void closeConnection(Connection con)
throws SQLException {
public static void closeConnection(Connection con) throws SQLException {
if (con != null) {
con.close();
}