6 Commits

Author SHA1 Message Date
97f3924b48 Stept 6 (UPDATE) 2026-09-30 16:04:33 +09:00
9e7d1e06ae Stept 5 INSERT 2026-09-30 14:51:54 +09:00
a5533aeb77 Strpt 4 (DELETE) Done 2026-09-30 13:39:33 +09:00
0e5ccfe379 Stept 3 2026-09-30 11:31:08 +09:00
329c222f56 Stept 2 2026-09-29 16:53:19 +09:00
56130f34b2 Stept 1 2026-09-29 16:33:39 +09:00
4 changed files with 240 additions and 75 deletions

View File

@@ -53,7 +53,12 @@ public class EmployeeDAO {
List<EmployeeDTO> rtnList = new ArrayList<>(); List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" SELECT "); sql.append(" SELECT ");
sql.append(" " + "*"); sql.append(" " + ID_EMPLOYEE + ",");
sql.append(" " + NM_EMPLOYEE + ",");
sql.append(" " + KN_EMPLOYEE + ",");
sql.append(" " + MAIL_ADDRESS + ",");
sql.append(" " + PASSWORD + ",");
sql.append(" " + ID_DEPARTMENT);
sql.append(" FROM "); sql.append(" FROM ");
sql.append(" " + TBL_NAME); sql.append(" " + TBL_NAME);
try { try {
@@ -62,12 +67,14 @@ public class EmployeeDAO {
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
EmployeeDTO dto = new EmployeeDTO(); EmployeeDTO dto = new EmployeeDTO();
dto.setID_EMPLOYEE(rs.getInt("ID_EMPLOYEE")); dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
dto.setNM_EMPLOYEE(rs.getString("NM_EMPLOYEE")); dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
dto.setKN_EMPLOYEE(rs.getString("KN_EMPLOYEE")); dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
dto.setMAIL_ADDRESS(rs.getString("MAIL_ADDRESS")); dto.setMailAddress(rs.getString(MAIL_ADDRESS));
dto.setPASSWORD(rs.getString("PASSWORD")); dto.setPassword(rs.getString(PASSWORD));
dto.setID_DEPARTMENT(rs.getInt("ID_DEPARTMENT")); dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
// dto ကို List ထဲသို့ ထည့်ရပါမည်
rtnList.add(dto); rtnList.add(dto);
} }
} finally { } finally {
@@ -76,42 +83,117 @@ public class EmployeeDAO {
return rtnList; return rtnList;
} }
//社員テーブルから指定された社員IDに一致するデータを取得し、DTOに設定の上、リスト形式で戻 //use selectById make the method
public List<EmployeeDTO> selectById(int id_employee) throws SQLException { public List<EmployeeDTO> selectById(int empId) throws SQLException, ClassNotFoundException {
List<EmployeeDTO> list = new ArrayList<>(); //calling to DTO and new inout array
String sql = "SELECT * FROM employee WHERE ID_EMPLOYEE = ?"; List<EmployeeDTO> rtnList = new ArrayList<>();
try { //(StringBuilder) ကို သုံးခြင်းသည် Code များကို ဖတ်ရလွယ်ကူစေရန်၊ ပြင်ဆင်ရလွယ်ကူစေရန်
StringBuilder sql = new StringBuilder();
//append မသုံးရင်စာ‌ကြောင်းပိုရှဉ္လို့ append နဲံရေး
sql.append(" SELECT ");
sql.append(" " + ID_EMPLOYEE + ",");
sql.append(" " + NM_EMPLOYEE + ",");
sql.append(" " + KN_EMPLOYEE + ",");
sql.append(" " + MAIL_ADDRESS + ",");
sql.append(" " + PASSWORD + ",");
sql.append(" " + ID_DEPARTMENT);
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" " + ID_EMPLOYEE + " = ? ");
try {
this.stmt = con.prepareStatement(sql.toString()); this.stmt = con.prepareStatement(sql.toString());
this.stmt.setInt(1, id_employee); this.stmt.setInt(1, empId);
this.rs = this.stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
EmployeeDTO dto = new EmployeeDTO(); EmployeeDTO dto = new EmployeeDTO();
dto.setID_EMPLOYEE(rs.getInt("ID_EMPLOYEE")); dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
dto.setNM_EMPLOYEE(rs.getString("NM_EMPLOYEE")); dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
dto.setKN_EMPLOYEE(rs.getString("KN_EMPLOYEE")); dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
dto.setMAIL_ADDRESS(rs.getString("MAIL_ADDRESS")); dto.setMailAddress(rs.getString(MAIL_ADDRESS));
dto.setPASSWORD(rs.getString("PASSWORD")); dto.setPassword(rs.getString(PASSWORD));
dto.setID_DEPARTMENT(rs.getInt("ID_DEPARTMENT")); dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
list.add(dto);
rtnList.add(dto);
} }
} finally { } finally {
DbUtil.closeStatement(this.stmt); DbUtil.closeStatement(this.stmt);
} }
return list; return rtnList;
} }
//Delete method
// 社員テーブルから指定された社員IDに一致するデータを削除する public void empDelete(int empId) throws ClassNotFoundException, SQLException {
public void empDelete(int id_employee) throws SQLException { StringBuilder sql = new StringBuilder();
String sql = "DELETE FROM employee WHERE ID_EMPLOYEE = ?"; sql.append(" DELETE FROM ");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" " + ID_EMPLOYEE + " = ? ");
//for check
try { try {
this.stmt = con.prepareStatement(sql); this.stmt = con.prepareStatement(sql.toString());
this.stmt.setInt(1, id_employee); this.stmt.setInt(1, empId);
this.stmt.executeUpdate(); this.stmt.executeUpdate();
} finally { } finally {
DbUtil.closeStatement(this.stmt); DbUtil.closeStatement(this.stmt);
} }
}
public void empInsert (String nmemp, String knname,String mail, String pw, int deptId) throws ClassNotFoundException, SQLException{
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO");
sql.append(" " + TBL_NAME);
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 (?, ?, ?, ?, ?) ");
try {
this.stmt = con.prepareStatement(sql.toString());
this.stmt.setString(1, nmemp);
this.stmt.setString(2, knname);
this.stmt.setString(3, mail);
this.stmt.setString(4, pw);
this.stmt.setInt(5, deptId);
this.stmt.executeUpdate();
} finally {
DbUtil.closeStatement(this.stmt);
}
}
public void empUpdate (String nmemp, String knname,String mail, String pw, int deptId,int empId) throws ClassNotFoundException, SQLException{
StringBuilder sql = new StringBuilder();
sql.append("UPDATE ");
sql.append(" " + TBL_NAME);
sql.append(" SET ");
sql.append(" " + NM_EMPLOYEE + " = ?, ");
sql.append(" " + KN_EMPLOYEE + " = ?, ");
sql.append(" " + MAIL_ADDRESS + " = ?, ");
sql.append(" " + PASSWORD + " = ?, ");
sql.append(" " + ID_DEPARTMENT + " = ? ");
sql.append(" WHERE ");
sql.append(" " + ID_EMPLOYEE + " = ? ");
try {
this.stmt = con.prepareStatement(sql.toString());
this.stmt.setString(1, nmemp);
this.stmt.setString(2, knname);
this.stmt.setString(3, mail);
this.stmt.setString(4, pw);
this.stmt.setInt(5, deptId);
this.stmt.setInt(6, empId);
this.stmt.executeUpdate();
} finally {
DbUtil.closeStatement(this.stmt);
}
} }
} }

View File

@@ -54,8 +54,10 @@ public class Main {
empSelect(); empSelect();
break; break;
case "2": case "2":
empInsert();
break; break;
case "3": case "3":
empUpdate();
break; break;
case "4": case "4":
empDelete(); empDelete();
@@ -71,6 +73,67 @@ public class Main {
} }
} }
private void empUpdate() throws ClassNotFoundException, SQLException{
// TODO 自動生成されたメソッド・スタブ
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
// ID ရိုက်ထည့်ခိုင်းခြင်း
String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId);
// ID ရှိမရှိ စစ်ဆေးခြင်း (selectById ကို သုံးပါ)
List<EmployeeDTO> empList = empDao.selectById(id);
if (empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
// အချက်အလက် အသစ်များ တောင်းခံခြင်း
String name = inputCheck("社員名", false);
String kana = inputCheck("フリガナ", false);
String mail = inputCheck("メールアドレス", false);
String pass = inputCheck("パスワード", false);
String strDeptId = inputCheck("部署ID", true);
int deptId = Integer.parseInt(strDeptId);
// DB ထဲသို့ ပြင်ဆင်ချက် ထည့်သွင်းခြင်း
empDao.empUpdate(name, kana, mail, pass, deptId,id);
System.out.println("更新が完了しました");
} finally {
DbUtil.closeConnection(empDao.con);
}
}
public static void empInsert() throws ClassNotFoundException, SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
// အချက်အလက်များ အသီးသီး တောင်းခံခြင်း (inputCheck ကို သုံးပါ)
String name = inputCheck("社員名", false);
String kana = inputCheck("フリガナ", false);
String mail = inputCheck("メールアドレス", false);
String pass = inputCheck("パスワード", false);
// 部署ID အတွက် Numerical Check ဖြစ်သည့် true ထည့်ပါမည်
String strDeptId = inputCheck("部署ID", true);
int deptId = Integer.parseInt(strDeptId);
// DB ထဲသို့ ထည့်သွင်းခြင်း
empDao.empInsert(name, kana, mail, pass, deptId);
System.out.println("登録が完了しました");
} finally {
DbUtil.closeConnection(empDao.con);
}
}
/** /**
* 社員テーブルから情報を検索する * 社員テーブルから情報を検索する
* *
@@ -78,6 +141,7 @@ public class Main {
* @throws SQLException * @throws SQLException
*/ */
public static void empSelect() throws ClassNotFoundException, SQLException { public static void empSelect() throws ClassNotFoundException, SQLException {
//List<String> empList = new ArrayList<>();
List<EmployeeDTO> empList = new ArrayList<>(); List<EmployeeDTO> empList = new ArrayList<>();
Connection con = DbUtil.getConnection(); Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con); EmployeeDAO empDao = new EmployeeDAO(con);
@@ -86,61 +150,70 @@ public class Main {
try { try {
switch (select) { switch (select) {
case "1": case "1":
//empList = empDao.selectAll();
empList = empDao.selectAll(); empList = empDao.selectAll();
break; break;
case "2": case "2":
String inputId = inputCheck("検索する社員ID", true); // inputCheck 方法ကို သုံးပြီး ID ရိုက်ခိုင်းပါ (数値 check အတွက် true ထည့်ပါ)
int input = Integer.parseInt(inputId); String strId = inputCheck("ID", true);
empList = empDao.selectById(input); int id = Integer.parseInt(strId);
break; empList = empDao.selectById(id);
;
default: default:
return; return;
} }
for (EmployeeDTO emp : empList) {
System.out.println(emp);
}
} finally {
DbUtil.closeConnection(empDao.con);
}
}
public static void empDelete() throws ClassNotFoundException, SQLException { // ရှာဖွေတွေ့ရှိခြင်း မရှိပါက
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
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()) { if (empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした"); System.out.println("入力された値で情報がみつかりませんでした");
return; return;
} }
System.out.println("指定されたIDの情報を削除します? Y/y"); for (EmployeeDTO emp : empList) {
System.out.println("(Y/y以外の入力であればキャンセル)"); //for (String emp : empList) {
String confilm = sc.nextLine(); //System.out.println(emp);
// 検索結果が存在した場合 System.out.println(emp.getIdEmployee() + "\t" +
if ("Y".equalsIgnoreCase(confilm)) { emp.getNmEmployee() + "\t" +
empDao.empDelete(inputId); emp.getKnEmployee() + "\t" +
System.out.println("社員ID" + input + "のデータを削除しました"); emp.getMailAddress() + "\t" +
} else { emp.getPassword() + "\t" +
System.out.println("削除をキャンセルしました"); emp.getIdDepartment());
} }
} catch (NumberFormatException e) {
System.out.println("入力された値で情報がみつかりませんでした");
} finally { } finally {
DbUtil.closeConnection(empDao.con); DbUtil.closeConnection(empDao.con);
} }
} }
public static void empDelete() throws ClassNotFoundException, SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
// ID ရိုက်ထည့်ခိုင်းခြင်း (inputCheck ကို သုံးပါ)
String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId);
//System.out.println("IDを入力してください");
// ID ရှိမရှိ စစ်ဆေးခြင်း (selectById ကို သုံးပါ)
List<EmployeeDTO> empList = empDao.selectById(id);
if (empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
// ဖျက်ရန် အတည်ပြုချက် (Confirmation) တောင်းခြင်း
System.out.println("指定されたIDの情報を削除します? Y/y");
String confirm = sc.nextLine();
if ("Y".equalsIgnoreCase(confirm)) {
empDao.empDelete(id);
System.out.println("削除が完了しました");
}
}finally {
DbUtil.closeConnection(empDao.con);
}
}
/** /**
* フラグを判定し、テーブル名を取得する * フラグを判定し、テーブル名を取得する
* *
@@ -190,4 +263,7 @@ public class Main {
} }
return str; return str;
} }
} }

View File

@@ -42,14 +42,19 @@ public class DbUtil {
* 例外 :ClassNotFoundException, SQLException * 例外 :ClassNotFoundException, SQLException
* 処理 :JDBCドライバを読み込み後、DB接続する。 * 処理 :JDBCドライバを読み込み後、DB接続する。
* 生成されたConnectionを返却する * 生成されたConnectionを返却する
* ※Connection生成には上記の定数を活用してください * ※Connection生成には上記の定数を活用してくださ
*/ */
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName(DB_DRIVER);
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS); public static Connection getConnection() throws ClassNotFoundException, SQLException {
//connection is null or not null and create new
if (con == null || con.isClosed()) {
Class.forName(DB_DRIVER);
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
}
return con;
} }
/* /*
* TODO 以下の仕様でメソッドを作成してください。 * TODO 以下の仕様でメソッドを作成してください。
* *
@@ -62,6 +67,7 @@ public class DbUtil {
public static void closeStatement(PreparedStatement stmt) throws SQLException { public static void closeStatement(PreparedStatement stmt) throws SQLException {
if (stmt != null) { if (stmt != null) {
stmt.close(); stmt.close();
} }
} }
@@ -76,8 +82,9 @@ public class DbUtil {
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/ */
public static void closeConnection(Connection con) throws SQLException { public static void closeConnection(Connection con) throws SQLException {
if (con != null) { if(con != null) {
con.close(); con.close();
} }
} }