Compare commits
6 Commits
s-sakiyama
...
h-hein
| Author | SHA1 | Date | |
|---|---|---|---|
| 97f3924b48 | |||
| 9e7d1e06ae | |||
| a5533aeb77 | |||
| 0e5ccfe379 | |||
| 329c222f56 | |||
| 56130f34b2 |
@@ -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 {
|
||||||
@@ -61,9 +66,15 @@ public class EmployeeDAO {
|
|||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
EmployeeDTO dto = new EmployeeDTO(rs.getInt(ID_EMPLOYEE), rs.getString(NM_EMPLOYEE),
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
rs.getString(KN_EMPLOYEE), rs.getString(MAIL_ADDRESS), rs.getString(PASSWORD),
|
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
rs.getInt(ID_DEPARTMENT));
|
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 ကို List ထဲသို့ ထည့်ရပါမည်
|
||||||
rtnList.add(dto);
|
rtnList.add(dto);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@@ -72,156 +83,114 @@ public class EmployeeDAO {
|
|||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//use selectById make the method
|
||||||
* 社員テーブルから社員IDに合致する情報を取得する
|
public List<EmployeeDTO> selectById(int empId) throws SQLException, ClassNotFoundException {
|
||||||
* @param id
|
//calling to DTO and new inout array
|
||||||
* 検索する社員ID
|
|
||||||
* @return 検索結果
|
|
||||||
* @throws SQLException
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws NumberFormatException
|
|
||||||
*/
|
|
||||||
public List<EmployeeDTO> selectById(int id)
|
|
||||||
throws SQLException, ClassNotFoundException, NumberFormatException {
|
|
||||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
//(StringBuilder) ကို သုံးခြင်းသည် Code များကို ဖတ်ရလွယ်ကူစေရန်၊ ပြင်ဆင်ရလွယ်ကူစေရန်
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
|
//append မသုံးရင်စာကြောင်းပိုရှဉ္လို့ append နဲံရေး
|
||||||
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);
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE ");
|
||||||
sql.append(" " + ID_EMPLOYEE);
|
sql.append(" " + ID_EMPLOYEE + " = ? ");
|
||||||
sql.append(" = ");
|
|
||||||
sql.append(" " + id);
|
|
||||||
try {
|
|
||||||
|
|
||||||
|
try {
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
this.stmt.setInt(1, empId);
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
while (rs.next()) {
|
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));
|
||||||
|
|
||||||
EmployeeDTO dto = new EmployeeDTO(rs.getInt(ID_EMPLOYEE), rs.getString(NM_EMPLOYEE),
|
|
||||||
rs.getString(KN_EMPLOYEE), rs.getString(MAIL_ADDRESS), rs.getString(PASSWORD),
|
|
||||||
rs.getInt(ID_DEPARTMENT));
|
|
||||||
rtnList.add(dto);
|
rtnList.add(dto);
|
||||||
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
}
|
}
|
||||||
if (rtnList.size() == 0) {
|
|
||||||
System.out.println("入力された値で情報がみつかりませんでした");
|
|
||||||
}
|
|
||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
//Delete method
|
||||||
/**
|
public void empDelete(int empId) throws ClassNotFoundException, SQLException {
|
||||||
* 社員テーブルの社員IDに合致する情報を削除する
|
|
||||||
*
|
|
||||||
* @param id
|
|
||||||
* 削除ID
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
* @throws NumberFormatException
|
|
||||||
*/
|
|
||||||
public void empDelete(int id)
|
|
||||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" DELETE ");
|
sql.append(" DELETE FROM ");
|
||||||
sql.append(" FROM ");
|
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" " + TBL_NAME);
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE ");
|
||||||
sql.append(" " + ID_EMPLOYEE);
|
sql.append(" " + ID_EMPLOYEE + " = ? ");
|
||||||
sql.append(" = ");
|
//for check
|
||||||
sql.append(" " + id);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
stmt.executeUpdate();
|
this.stmt.setInt(1, empId);
|
||||||
System.out.println("削除が完了しました");
|
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{
|
||||||
/**
|
|
||||||
* 社員テーブルに指定された情報を新規登録する
|
|
||||||
*
|
|
||||||
* @param name - 社員名
|
|
||||||
* @param kana - 社員名カナ
|
|
||||||
* @param mail - メールアドレス
|
|
||||||
* @param pass - パスワード
|
|
||||||
* @param depId - 部署ID
|
|
||||||
* @throws NumberFormatException
|
|
||||||
* @throws SQLException
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
*/
|
|
||||||
public void empInsert(String name, String kana, String mail, String pass, int depId)
|
|
||||||
throws NumberFormatException, SQLException, ClassNotFoundException {
|
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" INSERT ");
|
sql.append("INSERT INTO");
|
||||||
sql.append(" INTO ");
|
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" " + TBL_NAME);
|
||||||
sql.append(" ( " + NM_EMPLOYEE);
|
sql.append(" ( ");
|
||||||
sql.append(" , " + KN_EMPLOYEE);
|
|
||||||
sql.append(" , " + MAIL_ADDRESS);
|
sql.append(" " + NM_EMPLOYEE + ",");
|
||||||
sql.append(" , " + PASSWORD);
|
sql.append(" " + KN_EMPLOYEE + ",");
|
||||||
sql.append(" , " + ID_DEPARTMENT + " ) ");
|
sql.append(" " + MAIL_ADDRESS + ",");
|
||||||
sql.append(" VALUES ");
|
sql.append(" " + PASSWORD + ",");
|
||||||
sql.append(" ( '" + name);
|
sql.append(" " + ID_DEPARTMENT);
|
||||||
sql.append("' , '" + kana);
|
sql.append(" ) ");
|
||||||
sql.append("' , '" + mail);
|
sql.append(" VALUES (?, ?, ?, ?, ?) ");
|
||||||
sql.append("' , '" + pass);
|
|
||||||
sql.append("' , " + depId + " ) ");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
stmt.executeUpdate();
|
this.stmt.setString(1, nmemp);
|
||||||
System.out.println("登録が完了しました");
|
this.stmt.setString(2, knname);
|
||||||
|
this.stmt.setString(3, mail);
|
||||||
|
this.stmt.setString(4, pw);
|
||||||
|
this.stmt.setInt(5, deptId);
|
||||||
|
|
||||||
|
this.stmt.executeUpdate();
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public void empUpdate (String nmemp, String knname,String mail, String pw, int deptId,int empId) throws ClassNotFoundException, SQLException{
|
||||||
/**
|
|
||||||
* 社員テーブルの社員IDに合致する情報を更新する
|
|
||||||
*
|
|
||||||
* @param id - 社員ID
|
|
||||||
* @param name - 社員名
|
|
||||||
* @param kana - 社員名カナ
|
|
||||||
* @param mail - メールアドレス
|
|
||||||
* @param pass - パスワード
|
|
||||||
* @param depId - 部署ID
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
* @throws NumberFormatException
|
|
||||||
|
|
||||||
*/
|
|
||||||
public void empUpdate(int id, String name, String kana, String mail, String pass, int depId)
|
|
||||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" UPDATE ");
|
sql.append("UPDATE ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" " + TBL_NAME);
|
||||||
sql.append(" SET ");
|
sql.append(" SET ");
|
||||||
sql.append(" " + NM_EMPLOYEE);
|
|
||||||
sql.append(" = '" + name + " ' ");
|
sql.append(" " + NM_EMPLOYEE + " = ?, ");
|
||||||
sql.append(" , " + KN_EMPLOYEE);
|
sql.append(" " + KN_EMPLOYEE + " = ?, ");
|
||||||
sql.append(" = '" + kana + " ' ");
|
sql.append(" " + MAIL_ADDRESS + " = ?, ");
|
||||||
sql.append(" , " + MAIL_ADDRESS);
|
sql.append(" " + PASSWORD + " = ?, ");
|
||||||
sql.append(" = '" + mail + " ' ");
|
sql.append(" " + ID_DEPARTMENT + " = ? ");
|
||||||
sql.append(" , " + PASSWORD);
|
|
||||||
sql.append(" = '" + pass + " ' ");
|
|
||||||
sql.append(" , " + ID_DEPARTMENT);
|
|
||||||
sql.append(" = " + depId);
|
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE ");
|
||||||
sql.append(" " + ID_EMPLOYEE);
|
sql.append(" " + ID_EMPLOYEE + " = ? ");
|
||||||
sql.append(" = ");
|
|
||||||
sql.append(" " + id);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
stmt.executeUpdate();
|
this.stmt.setString(1, nmemp);
|
||||||
System.out.println("更新が完了しました");
|
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 {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,79 +0,0 @@
|
|||||||
package com.rhizome.example.dto;
|
|
||||||
|
|
||||||
public class EmployeeDTO {
|
|
||||||
private int ID_EMPLOYEE;
|
|
||||||
private String NM_EMPLOYEE;
|
|
||||||
private String KN_EMPLOYEE;
|
|
||||||
private String MAIL_ADDRESS;
|
|
||||||
private String PASSWORD;
|
|
||||||
private int ID_DEPARTMENT;
|
|
||||||
|
|
||||||
public EmployeeDTO() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public EmployeeDTO(int ID_EMPLOYEE, String NM_EMPLOYEE, String KN_EMPLOYEE, String MAIL_ADDRESS, String PASSWORD,
|
|
||||||
int ID_DEPARTMENT) {
|
|
||||||
setID_EMPLOYEE(ID_EMPLOYEE);
|
|
||||||
setNM_EMPLOYEE(NM_EMPLOYEE);
|
|
||||||
setKN_EMPLOYEE(KN_EMPLOYEE);
|
|
||||||
setMAIL_ADDRESS(MAIL_ADDRESS);
|
|
||||||
setPASSWORD(PASSWORD);
|
|
||||||
setID_DEPARTMENT(ID_DEPARTMENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getID_EMPLOYEE() {
|
|
||||||
return ID_EMPLOYEE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setID_EMPLOYEE(int iD_EMPLOYEE) {
|
|
||||||
ID_EMPLOYEE = iD_EMPLOYEE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getNM_EMPLOYEE() {
|
|
||||||
return NM_EMPLOYEE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNM_EMPLOYEE(String nM_EMPLOYEE) {
|
|
||||||
NM_EMPLOYEE = nM_EMPLOYEE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getKN_EMPLOYEE() {
|
|
||||||
return KN_EMPLOYEE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setKN_EMPLOYEE(String kN_EMPLOYEE) {
|
|
||||||
KN_EMPLOYEE = kN_EMPLOYEE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMAIL_ADDRESS() {
|
|
||||||
return MAIL_ADDRESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMAIL_ADDRESS(String mAIL_ADDRESS) {
|
|
||||||
MAIL_ADDRESS = mAIL_ADDRESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPASSWORD() {
|
|
||||||
return PASSWORD;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPASSWORD(String pASSWORD) {
|
|
||||||
PASSWORD = pASSWORD;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getID_DEPARTMENT() {
|
|
||||||
return ID_DEPARTMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setID_DEPARTMENT(int iD_DEPARTMENT) {
|
|
||||||
ID_DEPARTMENT = iD_DEPARTMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void showInfo() {
|
|
||||||
System.out.println(ID_EMPLOYEE + "\t" + NM_EMPLOYEE +
|
|
||||||
"\t" + KN_EMPLOYEE + "\t" + MAIL_ADDRESS +
|
|
||||||
"\t" + PASSWORD + "\t" + ID_DEPARTMENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -54,13 +54,13 @@ public class Main {
|
|||||||
empSelect();
|
empSelect();
|
||||||
break;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
empInsert();
|
empInsert();
|
||||||
break;
|
break;
|
||||||
case "3":
|
case "3":
|
||||||
empUpdate();
|
empUpdate();
|
||||||
break;
|
break;
|
||||||
case "4":
|
case "4":
|
||||||
empDelete();
|
empDelete();
|
||||||
break;
|
break;
|
||||||
case "9":
|
case "9":
|
||||||
return;
|
return;
|
||||||
@@ -73,88 +73,62 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private void empUpdate() throws ClassNotFoundException, SQLException{
|
||||||
* 社員テーブルの社員IDに合致する情報を更新する
|
// TODO 自動生成されたメソッド・スタブ
|
||||||
*
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
* @throws NumberFormatException
|
|
||||||
*/
|
|
||||||
public static void empUpdate()
|
|
||||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
|
||||||
Connection con = DbUtil.getConnection();
|
Connection con = DbUtil.getConnection();
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
int id = Integer.parseInt(inputCheck("ID", true));
|
|
||||||
|
|
||||||
if (empDao.selectById(id).size() == 0) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// အချက်အလက် အသစ်များ တောင်းခံခြင်း
|
||||||
String name = inputCheck("社員名", false);
|
String name = inputCheck("社員名", false);
|
||||||
String kana = inputCheck("フリガナ", false);
|
String kana = inputCheck("フリガナ", false);
|
||||||
String mail = inputCheck("メールアドレス", false);
|
String mail = inputCheck("メールアドレス", false);
|
||||||
String pass = inputCheck("パスワード", false);
|
String pass = inputCheck("パスワード", false);
|
||||||
int depId = Integer.parseInt(inputCheck("部署ID", true));
|
|
||||||
|
|
||||||
try {
|
String strDeptId = inputCheck("部署ID", true);
|
||||||
empDao.empUpdate(id, name, kana, mail, pass, depId);
|
int deptId = Integer.parseInt(strDeptId);
|
||||||
|
|
||||||
|
// DB ထဲသို့ ပြင်ဆင်ချက် ထည့်သွင်းခြင်း
|
||||||
|
empDao.empUpdate(name, kana, mail, pass, deptId,id);
|
||||||
|
System.out.println("更新が完了しました");
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection(empDao.con);
|
DbUtil.closeConnection(empDao.con);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 社員テーブルに指定された情報を新規登録する
|
|
||||||
* @throws NumberFormatException
|
public static void empInsert() throws ClassNotFoundException, SQLException {
|
||||||
* @throws SQLException
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
*/
|
|
||||||
public static void empInsert()
|
|
||||||
throws NumberFormatException, SQLException, ClassNotFoundException {
|
|
||||||
Connection con = DbUtil.getConnection();
|
Connection con = DbUtil.getConnection();
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// အချက်အလက်များ အသီးသီး တောင်းခံခြင်း (inputCheck ကို သုံးပါ)
|
||||||
String name = inputCheck("社員名", false);
|
String name = inputCheck("社員名", false);
|
||||||
String kana = inputCheck("フリガナ", false);
|
String kana = inputCheck("フリガナ", false);
|
||||||
String mail = inputCheck("メールアドレス", false);
|
String mail = inputCheck("メールアドレス", false);
|
||||||
String pass = inputCheck("パスワード", false);
|
String pass = inputCheck("パスワード", false);
|
||||||
int depId = Integer.parseInt(inputCheck("部署ID", true));
|
|
||||||
|
|
||||||
try {
|
// 部署ID အတွက် Numerical Check ဖြစ်သည့် true ထည့်ပါမည်
|
||||||
empDao.empInsert(name, kana, mail, pass, depId);
|
String strDeptId = inputCheck("部署ID", true);
|
||||||
} finally {
|
int deptId = Integer.parseInt(strDeptId);
|
||||||
DbUtil.closeConnection(empDao.con);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
// DB ထဲသို့ ထည့်သွင်းခြင်း
|
||||||
|
empDao.empInsert(name, kana, mail, pass, deptId);
|
||||||
|
System.out.println("登録が完了しました");
|
||||||
|
|
||||||
/**
|
|
||||||
* 社員テーブルの社員IDに合致する情報を削除する
|
|
||||||
*
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
*/
|
|
||||||
public static void empDelete()
|
|
||||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
|
||||||
List<EmployeeDTO> 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 {
|
} finally {
|
||||||
DbUtil.closeConnection(empDao.con);
|
DbUtil.closeConnection(empDao.con);
|
||||||
}
|
}
|
||||||
@@ -167,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);
|
||||||
@@ -175,22 +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":
|
||||||
empList = empDao.selectById(Integer.parseInt(inputCheck("ID", true)));
|
// inputCheck 方法ကို သုံးပြီး ID ရိုက်ခိုင်းပါ (数値 check အတွက် true ထည့်ပါ)
|
||||||
break;
|
String strId = inputCheck("ID", true);
|
||||||
|
int id = Integer.parseInt(strId);
|
||||||
|
empList = empDao.selectById(id);
|
||||||
|
;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ရှာဖွေတွေ့ရှိခြင်း မရှိပါက
|
||||||
|
if (empList.isEmpty()) {
|
||||||
|
System.out.println("入力された値で情報がみつかりませんでした");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (EmployeeDTO emp : empList) {
|
for (EmployeeDTO emp : empList) {
|
||||||
emp.showInfo();
|
//for (String emp : empList) {
|
||||||
|
//System.out.println(emp);
|
||||||
|
System.out.println(emp.getIdEmployee() + "\t" +
|
||||||
|
emp.getNmEmployee() + "\t" +
|
||||||
|
emp.getKnEmployee() + "\t" +
|
||||||
|
emp.getMailAddress() + "\t" +
|
||||||
|
emp.getPassword() + "\t" +
|
||||||
|
emp.getIdDepartment());
|
||||||
}
|
}
|
||||||
} 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* フラグを判定し、テーブル名を取得する
|
* フラグを判定し、テーブル名を取得する
|
||||||
*
|
*
|
||||||
@@ -241,25 +264,6 @@ public class Main {
|
|||||||
return str;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import java.sql.Connection;
|
|||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class DbUtil {
|
public class DbUtil {
|
||||||
// 定数定義(共通)
|
// 定数定義(共通)
|
||||||
@@ -43,16 +42,18 @@ public class DbUtil {
|
|||||||
* 例外 :ClassNotFoundException, SQLException
|
* 例外 :ClassNotFoundException, SQLException
|
||||||
* 処理 :JDBCドライバを読み込み後、DB接続する。
|
* 処理 :JDBCドライバを読み込み後、DB接続する。
|
||||||
* 生成されたConnectionを返却する
|
* 生成されたConnectionを返却する
|
||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してくださ
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
||||||
if (con != null && con.isValid(1)) {
|
//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;
|
return con;
|
||||||
}
|
}
|
||||||
Class.forName(DB_DRIVER);
|
|
||||||
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
@@ -63,10 +64,12 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
|
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
||||||
|
if (stmt != null) {
|
||||||
|
stmt.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static void closeStatement(PreparedStatement ps) throws SQLException {
|
|
||||||
if (Objects.nonNull(ps))
|
|
||||||
ps.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -79,8 +82,10 @@ public class DbUtil {
|
|||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
public static void closeConnection(Connection con) throws SQLException {
|
public static void closeConnection(Connection con) throws SQLException {
|
||||||
if (Objects.nonNull(con))
|
if(con != null) {
|
||||||
con.close();
|
con.close();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user