diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..af2cfcb --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/mysql-connector-java-8.0.23.jar b/mysql-connector-java-8.0.23.jar new file mode 100644 index 0000000..1e0cb19 Binary files /dev/null and b/mysql-connector-java-8.0.23.jar differ diff --git a/snippet/Snippet.java b/snippet/Snippet.java new file mode 100644 index 0000000..1146648 --- /dev/null +++ b/snippet/Snippet.java @@ -0,0 +1,8 @@ +package snippet; + +public class Snippet { + public static void main(String[] args) { + + } +} + diff --git a/src/com/rhizome/example/dao/EmployeeDAO.java b/src/com/rhizome/example/dao/EmployeeDAO.java index 4922c56..ee43741 100644 --- a/src/com/rhizome/example/dao/EmployeeDAO.java +++ b/src/com/rhizome/example/dao/EmployeeDAO.java @@ -1,10 +1,4 @@ -/* - * EmployeeDAO.java - * 作成 IT-College 2020 - *------------------------------------------------------------ - * Copyright(c) Rhizome Inc. All Rights Reserved. - */ -package com.rhizome.example.dao; +package src.com.rhizome.example.dao; import java.sql.Connection; import java.sql.PreparedStatement; @@ -13,8 +7,8 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import com.rhizome.example.dto.EmployeeDTO; -import com.rhizome.example.util.DbUtil; +import src.com.rhizome.example.dto.EmployeeDTO; +import src.com.rhizome.example.util.DbUtil; public class EmployeeDAO { private static final String TBL_NAME = "employee"; @@ -92,9 +86,8 @@ public class EmployeeDAO { public List selectById(int employeeId) throws SQLException{ ListrtnList = new ArrayList<>(); - String sql = - "SELECT * FROM employee" + - "WHERE id_employee = ?"; + java.lang.String sql = + "SELECT * FROM employee WHERE id_employee = ?"; try { stmt = con.prepareStatement(sql); stmt.setInt(1, employeeId); @@ -108,8 +101,8 @@ public class EmployeeDAO { dto.setIdEmployee(rs.getInt(ID_EMPLOYEE)); dto.setNmEmployee(rs.getString(NM_EMPLOYEE)); dto.setKnEmployee(rs.getString(KN_EMPLOYEE)); - dto.setKnEmployee(rs.getString(MAIL_ADDRESS)); - dto.setKnEmployee(rs.getString(PASSWORD)); + dto.setMailEmployee(rs.getString(MAIL_ADDRESS)); + dto.setPassEmployee(rs.getString(PASSWORD)); dto.setIdDepartment(rs.getInt(ID_DEPARTMENT)); rtnList.add(dto); @@ -121,26 +114,70 @@ public class EmployeeDAO { return rtnList; } - - public void empDelete(int employeeId)throws SQLException{ - String sql = - "DELETE FROM employee" + - "WHERE id_employee = ?"; + public int empDelete (int employeeId)throws SQLException{ + java.lang.String sql = + "DELETE FROM employee WHERE id_employee = ?"; try { stmt = con.prepareStatement(sql); + stmt.setInt(1, employeeId); - stmt.setInt(1, employeeId); + return stmt.executeUpdate(); + } finally { + DbUtil.closeStatement(stmt); - stmt.executeUpdate(); + } + } + public int empInsert(String employeeName,String employeeKn,String employeeMail + ,String employeePass, int departmentId)throws SQLException{ + java.lang.String sql = + "INSERT INTO employee " + + "(nm_employee, kn_employee, mail_address, password, id_department) " + + "VALUES (?, ?, ?, ?, ?)"; + try { + stmt = con.prepareStatement(sql); + stmt.setString(1, employeeName); + stmt.setString(2, employeeKn); + stmt.setString(3, employeeMail); + stmt.setString(4, employeePass); + stmt.setInt(5, departmentId); + + return stmt.executeUpdate(); } finally { - DbUtil.closeStatement(stmt); } + + } + + public int empUpdate(int employeeId,String employeeName,String employeeKn,String employeeMail + ,String employeePass, int departmentId)throws SQLException{ + java.lang.String sql = + "UPDATE employee " + + "SET nm_employee = ?, " + + "kn_employee = ?, " + + "mail_address = ?, " + + "password = ?, " + + "id_department = ? " + + "WHERE id_employee = ?"; - - + try { + + stmt = con.prepareStatement(sql); + + stmt.setString(1, employeeName); + stmt.setString(2, employeeKn); + stmt.setString(3, employeeMail); + stmt.setString(4, employeePass); + stmt.setInt(5, departmentId); + stmt.setInt(6, employeeId); + + return 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..559614d --- /dev/null +++ b/src/com/rhizome/example/dto/EmployeeDTO.java @@ -0,0 +1,51 @@ +package src.com.rhizome.example.dto; + +public class EmployeeDTO { + private int idEmployee; + private String nmEmployee; + private String knEmployee; + private String mailEmployee; + private String passEmployee; + 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 getMailEmployee() { + return mailEmployee; + } + public void setMailEmployee(String mailEmployee) { + this.mailEmployee = mailEmployee; + } + public String getPassEmployee() { + return passEmployee; + } + public void setPassEmployee(String passEmployee) { + this.passEmployee = passEmployee; + } + public int getIdDepartment() { + return idDepartment; + } + public void setIdDepartment(int idDepartment) { + this.idDepartment = idDepartment; + } + + + +} diff --git a/src/com/rhizome/example/main/Main.java b/src/com/rhizome/example/main/Main.java index 14fb220..60b6469 100644 --- a/src/com/rhizome/example/main/Main.java +++ b/src/com/rhizome/example/main/Main.java @@ -1,4 +1,4 @@ -package com.rhizome.example.main; +package src.com.rhizome.example.main; import java.sql.Connection; import java.sql.SQLException; @@ -6,8 +6,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Scanner; -import com.rhizome.example.dao.EmployeeDAO; -import com.rhizome.example.util.DbUtil; +import src.com.rhizome.example.dao.EmployeeDAO; +import src.com.rhizome.example.dto.EmployeeDTO; +import src.com.rhizome.example.util.DbUtil; /** * @author IT-College @@ -53,10 +54,13 @@ public class Main { empSelect(); break; case "2": + empInsert(); break; case "3": + empUpdate(); break; case "4": + empDelete(); break; case "9": return; @@ -76,7 +80,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,15 +90,35 @@ public class Main { case "1": empList = empDao.selectAll(); break; + case "2": + + String strId = inputCheck("ID", true); + int id = Integer.parseInt(strId); + empList = empDao.selectById(id); + break; default: + return; } - for (String emp : empList) { - System.out.println(emp); + if(empList.isEmpty()){ + System.out.println("入力された値で情報がみつかりませんでした"); + }else { + + for (EmployeeDTO emp : empList) { + System.out.println( + emp.getIdEmployee() + "\t" + + emp.getNmEmployee() + "\t" + + emp.getKnEmployee() + "\t" + + emp.getMailEmployee() + "\t" + + emp.getPassEmployee() + "\t" + + emp.getIdDepartment() + "\t"); + } } - } finally { - DbUtil.closeConnection(empDao.con); + }finally { + DbUtil.closeConnection(empDao.con); + } + } /** @@ -146,4 +170,100 @@ public class Main { } return str; } + public static void empDelete() throws ClassNotFoundException, SQLException { + + Connection con = DbUtil.getConnection(); + EmployeeDAO empDao = new EmployeeDAO(con); + + try { + + String strId = inputCheck("ID", true); + int id = Integer.parseInt(strId); + + int count = empDao.empDelete(id); + + if (count == 0) { + System.out.println("入力されたIDの情報は見つかりませんでした。"); + } else { + 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 { + + String name= inputCheck("社員名", false); + String kana = inputCheck("フリガナ", false); + String mail = inputCheck("メールアドレス", false); + String pass = inputCheck("パスワード", false); + + int depId = Integer.parseInt(inputCheck("部署ID", true)); + + int count = empDao.empInsert( + name, + kana, + mail, + pass, + depId); + + if (count > 0) { + System.out.println("登録が完了しました"); + } + + } finally { + DbUtil.closeConnection(empDao.con); + } + } + public static void empUpdate() throws ClassNotFoundException, SQLException { + + Connection con = DbUtil.getConnection(); + EmployeeDAO empDao = new EmployeeDAO(con); + String strId = inputCheck("ID", true); + + int id = Integer.parseInt(strId); + try { + + + String name = + inputCheck("社員名", false); + + String kana = + inputCheck("フリガナ", false); + + String mail = + inputCheck("メールアドレス", false); + + String pass = + inputCheck("パスワード", false); + + int depId = + Integer.parseInt(inputCheck("部署ID", true)); + + + int count = empDao.empUpdate( + id, + name, + kana, + mail, + pass, + depId); + + if (count > 0) { + System.out.println("更新が完了しました"); + } else { + System.out.println("入力されたIDの情報は見つかりませんでした。"); + } + + } 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..adc8cc2 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; +package src.com.rhizome.example.util; import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; public class DbUtil { // 定数定義(共通) @@ -41,7 +44,24 @@ public class DbUtil { * 生成されたConnectionを返却する * ※Connection生成には上記の定数を活用してください */ + public static Connection getConnection() + throws ClassNotFoundException, SQLException{ + + if(con != null && !con.isClosed()) { + return con; + } + Class.forName("com.mysql.cj.jdbc.Driver"); + con = DriverManager.getConnection( + DB_URL, + DB_USER, + DB_PASS); + return con; + + } + + + /* * TODO 以下の仕様でメソッドを作成してください。 @@ -52,7 +72,12 @@ public class DbUtil { * 例外 :SQLException * 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする */ - + public static void closeStatement( + PreparedStatement stmt) throws SQLException{ + if(stmt !=null) + stmt.close(); + + } /* * TODO 以下の仕様でメソッドを作成してください。 @@ -63,6 +88,9 @@ public class DbUtil { * 例外 :SQLException * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する */ - - + public static void closeConnection(Connection con) + throws SQLException{ + if(con != null) + con.close(); + } }