diff --git a/src/com/rhizome/example/dao/DepartmentDAO.java b/src/com/rhizome/example/dao/DepartmentDAO.java new file mode 100644 index 0000000..f1643c4 --- /dev/null +++ b/src/com/rhizome/example/dao/DepartmentDAO.java @@ -0,0 +1,200 @@ +package com.rhizome.example.dao; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import com.rhizome.example.dto.DepartmentDTO; +import com.rhizome.example.util.DbUtil; + +public class DepartmentDAO { + private static final String TBL_NAME = "department"; + private static final String ID_DEPARTMENT = "id_department"; + private static final String NM_DEPARTMENT = "nm_department"; + + /** DBコネクション */ + public Connection con; + /** DBステートメント */ + PreparedStatement stmt; + /** 検索結果 */ + ResultSet rs; + + /** + * インスタンス生成時DBコネクション + * + * @throws ClassNotFoundException + * @throws SQLException + */ + public DepartmentDAO(Connection con) { + this.con = con; + } + + /** + * 部署テーブルの情報を全件取得する + * + * @return 検索結果 + * @throws SQLException + * @throws ClassNotFoundException + */ + public List selectAll() throws SQLException, ClassNotFoundException { + List rtnList = new ArrayList<>(); + StringBuilder sql = new StringBuilder(); + sql.append(" SELECT "); + sql.append(" " + "*"); + sql.append(" FROM "); + sql.append(" " + TBL_NAME); + try { + this.stmt = con.prepareStatement(sql.toString()); + rs = stmt.executeQuery(); + while (rs.next()) { + DepartmentDTO Department = new DepartmentDTO(); + Department.setIdDepartment(rs.getInt(ID_DEPARTMENT)); + Department.setNmDepartment(rs.getString(NM_DEPARTMENT)); + rtnList.add(Department); + } + } finally { + DbUtil.closeStatement(this.stmt); + } + return rtnList; + } + + /** + * 部署テーブルから指定された部署IDに一致するデータを全件取得する + * + * @return 検索結果 + * @throws SQLException + * @throws ClassNotFoundException + */ + public List selectById(int idDepartment) throws SQLException, ClassNotFoundException { + List rtnList = new ArrayList<>(); + StringBuilder sql = new StringBuilder(); + sql.append(" SELECT "); + sql.append(" " + "*"); + sql.append(" FROM "); + sql.append(" " + TBL_NAME); + sql.append(" WHERE "); + sql.append(" " + ID_DEPARTMENT + " = " + "?"); + try { + this.stmt = con.prepareStatement(sql.toString()); + stmt.setInt(1,idDepartment); + rs = stmt.executeQuery(); + int nRecords = 0; + while (rs.next()) { + DepartmentDTO Department = new DepartmentDTO(); + Department.setIdDepartment(rs.getInt(ID_DEPARTMENT)); + Department.setNmDepartment(rs.getString(NM_DEPARTMENT)); + rtnList.add(Department); + nRecords++; + } + if (nRecords==0) { + System.out.println("入力された値で情報がみつかりませんでした"); + } + } finally { + DbUtil.closeStatement(this.stmt); + } + return rtnList; + } + + /** + * 部署テーブルから指定された部署IDに一致する部署名を取得する + * + * @return 部署名 + * @throws SQLException + * @throws ClassNotFoundException + */ + public String getNmDepartment(int idDepartment) throws SQLException, ClassNotFoundException { + String name = Integer.toString(idDepartment); // 部署名が見つからない場合は部署IDを返却する + Connection con = DbUtil.getConnection(); + DepartmentDAO depDao = new DepartmentDAO(con); + if(depDao.selectById(idDepartment).size()!=0) { + DepartmentDTO department = depDao.selectById(idDepartment).get(0); + name = department.getNmDepartment(); + }; + return name; + } + + /** + * 部署テーブルから指定された部署IDに一致するデータを削除する + * + * @throws SQLException + * @throws ClassNotFoundException + */ + public void depDelete(int idDepartment) throws SQLException, ClassNotFoundException { + if (selectById(idDepartment).size()!=0) { + StringBuilder sql = new StringBuilder(); + sql.append(" DELETE FROM "); + sql.append(" " + TBL_NAME); + sql.append(" WHERE "); + sql.append(" " + ID_DEPARTMENT + " = " + "?"); + System.out.println("指定されたIDの情報を削除しますか? Y/y"); + System.out.println("(Y/y以外の入力であればキャンセル)"); + Scanner sc = new Scanner(System.in); + String flag = sc.nextLine(); + if (flag.equals("Y")||flag.equals("y")) { + try { + this.stmt = con.prepareStatement(sql.toString()); + stmt.setInt(1,idDepartment); + stmt.executeUpdate(); + System.out.println("削除が完了しました"); + } finally { + DbUtil.closeStatement(this.stmt); + } + } + } + } + + /** + * 部署テーブルに指定された情報を新規登録する + * + * @throws SQLException + * @throws ClassNotFoundException + */ + public void depInsert(String name) throws SQLException, ClassNotFoundException { + StringBuilder sql = new StringBuilder(); + sql.append(" INSERT INTO "); + sql.append(" " + TBL_NAME); + sql.append(" ("+NM_DEPARTMENT+") "); + sql.append(" VALUES "); + sql.append(" ( ? ) "); + try { + this.stmt = con.prepareStatement(sql.toString()); + stmt.setString(1,name); + stmt.executeUpdate(); + System.out.println("登録が完了しました"); + } finally { + DbUtil.closeStatement(this.stmt); + } + } + + /** + * 部署テーブルの指定された部署IDに一致するデータを引数の内容に更新する + * + * @throws SQLException + * @throws ClassNotFoundException + */ + public void depUpdate(int idDepartment, String name) throws SQLException, ClassNotFoundException { + if (selectById(idDepartment).size()!=0) { + StringBuilder sql = new StringBuilder(); + sql.append(" UPDATE "); + sql.append(" " + TBL_NAME); + sql.append(" SET "); + sql.append(" "+NM_DEPARTMENT+" = ?"+" "); + sql.append(" WHERE "); + sql.append(" " + ID_DEPARTMENT + " = " + "?"); + try { + this.stmt = con.prepareStatement(sql.toString()); + stmt.setString(1,name); + stmt.setInt(2,idDepartment); + stmt.executeUpdate(); + System.out.println("更新が完了しました"); + } finally { + DbUtil.closeStatement(this.stmt); + } + } + } + +} diff --git a/src/com/rhizome/example/dto/DepartmentDTO.java b/src/com/rhizome/example/dto/DepartmentDTO.java new file mode 100644 index 0000000..8f3827e --- /dev/null +++ b/src/com/rhizome/example/dto/DepartmentDTO.java @@ -0,0 +1,27 @@ +package com.rhizome.example.dto; + +public class DepartmentDTO { + + private int idDepartment; + private String nmDepartment; + + public int getIdDepartment() { + return idDepartment; + } + public void setIdDepartment(int idDepartment) { + this.idDepartment = idDepartment; + } + public String getNmDepartment() { + return nmDepartment; + } + public void setNmDepartment(String nmDepartment) { + this.nmDepartment = nmDepartment; + } + + @Override + public String toString() { + return this.getIdDepartment() + " " + +this.getNmDepartment(); + } + +} diff --git a/src/com/rhizome/example/main/MainNew.java b/src/com/rhizome/example/main/MainNew.java new file mode 100644 index 0000000..b45652a --- /dev/null +++ b/src/com/rhizome/example/main/MainNew.java @@ -0,0 +1,406 @@ +package com.rhizome.example.main; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import com.rhizome.example.dao.DepartmentDAO; +import com.rhizome.example.dao.EmployeeDAO; +import com.rhizome.example.dto.DepartmentDTO; +import com.rhizome.example.dto.EmployeeDTO; +import com.rhizome.example.util.DbUtil; + +/** + * @author IT-College + * @version 1.0 + */ +public class MainNew { + static Scanner sc = new Scanner(System.in); + /** + * テーブル切り替え用変数 0:Employeeテーブル 1:Departmentテーブル + */ + static int tblFlg = 0; + + /** + * 処理開始位置 + * + * @param args + */ + public static void main(String[] args) { + System.out.println("システムを開始します"); + while (true) { + try { + new MainNew().start(); + } catch (Exception e) { + System.out.println("予期せぬエラーが発生しました"); + e.printStackTrace(); + } + } + } + + /** + * 処理選択を行う + * + * @throws ClassNotFoundException + * @throws SQLException + */ + public void start() throws ClassNotFoundException, SQLException { + System.out.println(); + System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg)); + System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了"); + String ope = sc.nextLine(); + switch (ope) { + case "1": + Select(tblFlg); + break; + case "2": + Insert(tblFlg); + break; + case "3": + Update(tblFlg); + break; + case "4": + Delete(tblFlg); + break; + case "9": + tblFlg = ChangeTable(tblFlg); + break; + case "0": + System.out.println("システムを終了します"); + System.exit(0); + default: + System.out.println("指定の番号を入力してください"); + break; + } + } + + /** + * 社員テーブルから情報を検索する + * + * @throws ClassNotFoundException + * @throws SQLException + */ + public static void empSelect() throws ClassNotFoundException, SQLException { + List empList = new ArrayList<>(); + Connection con = DbUtil.getConnection(); + EmployeeDAO empDao = new EmployeeDAO(con); + System.out.println("1:全検索 2:条件検索 それ以外:戻る"); + String select = sc.nextLine(); + try { + switch (select) { + case "1": + empList = empDao.selectAll(); + break; + case "2": + empList = empDao.selectById(Integer.parseInt(inputCheck("社員ID", true))); + break; + default: + return; + } + for (EmployeeDTO emp : empList) { + System.out.println(emp); + //System.out.println(toStringEmployee(emp)); // 出力文字調整:ongoing + } + } finally { + DbUtil.closeConnection(empDao.con); + } + } + + /** + * 部署テーブルから情報を検索する + * + * @throws ClassNotFoundException + * @throws SQLException + */ + public static void depSelect() throws ClassNotFoundException, SQLException { + List depList = new ArrayList<>(); + Connection con = DbUtil.getConnection(); + DepartmentDAO depDao = new DepartmentDAO(con); + System.out.println("1:全検索 2:条件検索 それ以外:戻る"); + String select = sc.nextLine(); + try { + switch (select) { + case "1": + depList = depDao.selectAll(); + break; + case "2": + depList = depDao.selectById(Integer.parseInt(inputCheck("部署ID", true))); + break; + default: + return; + } + for (DepartmentDTO dep : depList) { + System.out.println(dep); + //System.out.println(toStringDepartment(dep)); // 出力文字調整:ongoing + } + } finally { + DbUtil.closeConnection(depDao.con); + } + } + + /** + * フラグを判定し、テーブル名を取得する + * + * @param tblFlg + * テーブル名判定用フラグ + * @return テーブル名 + */ + public static String targetTableName(int tblFlg) { + String rtnTblName = ""; + if (tblFlg == 0) { + rtnTblName = "社員テーブル"; + } else { + rtnTblName = "部署テーブル"; + } + return rtnTblName; + } + + /** + * フラグを判定し、検索機能を呼び出す + * + * @param tblFlg + * テーブル名判定用フラグ + * @throws ClassNotFoundException + * @throws SQLException + */ + public static void Select(int tblFlg) throws ClassNotFoundException, SQLException { + if (tblFlg == 0) { + empSelect(); + } else { + depSelect(); + } + } + + /** + * フラグを判定し、登録機能を呼び出す + * + * @param tblFlg + * テーブル名判定用フラグ + * @throws ClassNotFoundException + * @throws SQLException + */ + public static void Insert(int tblFlg) throws ClassNotFoundException, SQLException { + Connection con = DbUtil.getConnection(); + if (tblFlg == 0) { + EmployeeDAO empDao = new EmployeeDAO(con); + empDao.empInsert(inputCheck("社員名", false), + inputCheck("フリガナ", false), + inputCheck("メールアドレス", false), + inputCheck("パスワード", false), + inputCheckIdDepartmentRep()); // 部署IDと照合できるまで入力を受け付ける + } else { + DepartmentDAO depDao = new DepartmentDAO(con); + depDao.depInsert(inputCheck("部署名", false)); + } + } + + /** + * フラグを判定し、更新機能を呼び出す + * + * @param tblFlg + * テーブル名判定用フラグ + * @throws ClassNotFoundException + * @throws SQLException + */ + public static void Update(int tblFlg) throws ClassNotFoundException, SQLException { + Connection con = DbUtil.getConnection(); + if (tblFlg == 0) { + EmployeeDAO empDao = new EmployeeDAO(con); + int id = Integer.parseInt(inputCheck("社員ID", true)); + if (empDao.selectById(id).size()==0) {} // 社員IDの照合に失敗した場合は更新処理を行わない + else { + empDao.empUpdate(id, + inputCheck("社員名", false), + inputCheck("フリガナ", false), + inputCheck("メールアドレス", false), + inputCheck("パスワード", false), + inputCheckIdDepartmentRep()); // 部署IDと照合できるまで入力を受け付ける + } + } else { + DepartmentDAO depDao = new DepartmentDAO(con); + int id = Integer.parseInt(inputCheck("部署ID", true)); + if (depDao.selectById(id).size()==0) {} // 部署IDの照合に失敗した場合は更新処理を行わない + else { + depDao.depUpdate(id, + inputCheck("部署名", false)); + } + } + } + + /** + * フラグを判定し、削除機能を呼び出す + * + * @param tblFlg + * テーブル名判定用フラグ + * @throws ClassNotFoundException + * @throws SQLException + */ + public static void Delete(int tblFlg) throws ClassNotFoundException, SQLException { + Connection con = DbUtil.getConnection(); + if (tblFlg == 0) { + EmployeeDAO empDao = new EmployeeDAO(con); + empDao.empDelete(Integer.parseInt(inputCheck("社員ID", true))); // 社員IDの照合に失敗した場合は削除処理を行わない + } else { + DepartmentDAO depDao = new DepartmentDAO(con); + depDao.depDelete(Integer.parseInt(inputCheck("部署ID", true))); // 部署IDの照合に失敗した場合は削除処理を行わない + } + } + + /** + * テーブルを切り替える + * + * @param tblFlg + * テーブル名判定用フラグ + * @return 変更後フラグ + */ + public static int ChangeTable(int tblFlg) { + if (tblFlg == 0) { + tblFlg = 1; + } else { + tblFlg = 0; + } + return tblFlg; + } + + /** + * 入力値を判定し、返却する + * + * @param titleName + * 入力項目名 + * @param flg + * 数値チェック可否フラグ + * @return + */ + public static String inputCheck(String titleName, boolean flg) { + String str = null; + while (true) { + System.out.println(titleName + "を入力してください"); + str = sc.nextLine(); + if (str.trim().length() == 0) { + System.out.println(titleName + "は入力必須です。"); + continue; + } + if (flg) { + if (str.trim().length() > 0) { + try { + Integer.parseInt(str); + } catch (NumberFormatException e) { + System.out.println(titleName + "は数値入力です"); + continue; + } + } + } + break; + } + return str; + } + + /** + * 入力値と社員IDを照合し、返却する(照合できるまで入力を受け付ける) + * + * @throws ClassNotFoundException + * @throws SQLException + * @return + */ + public static int inputCheckIdEmployeeRep() throws ClassNotFoundException, SQLException { + int id; + Connection con = DbUtil.getConnection(); + EmployeeDAO empDao = new EmployeeDAO(con); + while (true) { + id = Integer.parseInt(inputCheck("社員ID", true)); + if (empDao.selectById(id).size()!=0) { + break; + } + } + return id; + } + + /** + * 入力値と部署IDを照合し、返却する(照合できるまで入力を受け付ける) + * + * @throws ClassNotFoundException + * @throws SQLException + * @return + */ + public static int inputCheckIdDepartmentRep() throws ClassNotFoundException, SQLException { + int id; + Connection con = DbUtil.getConnection(); + DepartmentDAO depDao = new DepartmentDAO(con); + while (true) { + id = Integer.parseInt(inputCheck("部署ID", true)); + if (depDao.selectById(id).size()!=0) { + break; + } + } + return id; + } + + /** + * 社員テーブルの出力文字列を生成する + * ・縦の線が揃うように位置を調整する + * ・部署IDを部署名に変更する + * + * @throws ClassNotFoundException + * @throws SQLException + * @return 出力文字列 + */ + public static String toStringEmployee(EmployeeDTO employee) throws ClassNotFoundException, SQLException { + Connection con = DbUtil.getConnection(); + DepartmentDAO depDao = new DepartmentDAO(con); + return fixCharCount(8, employee.getIdEmployee()) + +fixCharCountFull(8, employee.getNmEmployee()) + +fixCharCountFull(12, employee.getKnEmployee()) + +fixCharCount(20, employee.getMailaddress()) + +fixCharCount(12, employee.getPassword()) + +fixCharCountFull(8, depDao.getNmDepartment(employee.getIdDepartment())); + } + + /** + * 部署テーブルの出力文字列を生成する + * ・縦の線が揃うように位置を調整する + * + * + * @throws ClassNotFoundException + * @throws SQLException + * @return 出力文字列 + */ + public static String toStringDepartment(DepartmentDTO department) throws ClassNotFoundException, SQLException { + return fixCharCount(8, department.getIdDepartment()) + +fixCharCountFull(12, department.getNmDepartment()); + } + + /** + * 入力文字列の文字数が一定になるように空白を追加する(半角文字/数字/全角文字) + * + * @return + */ + public static String fixCharCount(int nChars, String input) { + if (nChars-input.length()>=0) { + for (int i=0; i=0) { + for (int i=0; i=0) { + System.out.println(nChars-input.length()); + for (int i=0; i