5段階目まで完了
This commit is contained in:
@@ -61,7 +61,16 @@ 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()) {
|
||||||
rtnList.add(rs.getString(NM_EMPLOYEE));
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
|
dto.setId(rs.getInt("id_employee"));
|
||||||
|
dto.setName(rs.getString("nm_employee"));
|
||||||
|
dto.setKana(rs.getString("kn_employee"));
|
||||||
|
dto.setEmail(rs.getString("mail_address"));
|
||||||
|
dto.setPass(rs.getString("password"));
|
||||||
|
dto.setIdDep(rs.getInt("id_department"));
|
||||||
|
|
||||||
|
rtnList.add(dto);
|
||||||
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
@@ -69,4 +78,102 @@ public class EmployeeDAO {
|
|||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public java.util.List<EmployeeDTO> selectById(int id) throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException {
|
||||||
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" SELECT * FROM ");
|
||||||
|
sql.append(TBL_NAME);
|
||||||
|
sql.append(" WHERE id_employee = ?");
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
stmt.setInt(1, id);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
|
||||||
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
|
dto.setId(rs.getInt("id_employee"));
|
||||||
|
dto.setName(rs.getString("nm_employee"));
|
||||||
|
dto.setKana(rs.getString("kn_employee"));
|
||||||
|
dto.setEmail(rs.getString("mail_address"));
|
||||||
|
dto.setPass(rs.getString("password"));
|
||||||
|
dto.setIdDep(rs.getInt("id_department"));
|
||||||
|
|
||||||
|
rtnList.add(dto);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rtnList;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void empDelete(int id) throws java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" DELETE FROM ");
|
||||||
|
sql.append(TBL_NAME);
|
||||||
|
sql.append(" WHERE id_employee = ?");
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.stmt = this.con.prepareStatement(sql.toString());
|
||||||
|
this.stmt.setInt(1, id);
|
||||||
|
this.stmt.executeUpdate();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void empInsert(java.lang.String name, java.lang.String kana, java.lang.String mail, java.lang.String pass, int depId) throws java.lang.NumberFormatException, java.sql.SQLException, java.lang.ClassNotFoundException{
|
||||||
|
//List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" INSERT INTO " + TBL_NAME);
|
||||||
|
sql.append(" (nm_employee, kn_employee, mail_address, password, id_department) ");
|
||||||
|
sql.append(" VALUES ");
|
||||||
|
sql.append(" (?, ?, ?, ?, ?) ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
|
||||||
|
this.stmt.setString(1, name);
|
||||||
|
this.stmt.setString(2, kana);
|
||||||
|
this.stmt.setString(3, mail);
|
||||||
|
this.stmt.setString(4, pass);
|
||||||
|
this.stmt.setInt(5, depId);
|
||||||
|
|
||||||
|
this.stmt.executeUpdate();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void empUpdate(int id, java.lang.String name, java.lang.String kana, java.lang.String mail, java.lang.String pass, int depId) throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException{
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" UPDATE " + TBL_NAME + " SET ");
|
||||||
|
sql.append(" (id_employee=?,nm_employee=?, kn_employee=?, mail_address=?, password=?, id_department=?) ");
|
||||||
|
sql.append(" WHERE id_employee = ? ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
|
||||||
|
this.stmt.setInt(1, id);
|
||||||
|
this.stmt.setString(2, name);
|
||||||
|
this.stmt.setString(3, kana);
|
||||||
|
this.stmt.setString(4, mail);
|
||||||
|
this.stmt.setString(5, pass);
|
||||||
|
this.stmt.setInt(6, depId);
|
||||||
|
|
||||||
|
this.stmt.executeUpdate();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,59 @@ package com.rhizome.example.dto;
|
|||||||
|
|
||||||
public class EmployeeDTO {
|
public class EmployeeDTO {
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
|
private String name;
|
||||||
|
private String kana;
|
||||||
|
private String email;
|
||||||
|
private String pass;
|
||||||
|
private Integer idDep;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKana() {
|
||||||
|
return kana;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKana(String kana) {
|
||||||
|
this.kana = kana;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPass() {
|
||||||
|
return pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPass(String pass) {
|
||||||
|
this.pass = pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getIdDep() {
|
||||||
|
return idDep;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdDep(Integer idDep) {
|
||||||
|
this.idDep = idDep;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import java.util.List;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import com.rhizome.example.dao.EmployeeDAO;
|
import com.rhizome.example.dao.EmployeeDAO;
|
||||||
|
import com.rhizome.example.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -53,10 +54,13 @@ 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();
|
||||||
break;
|
break;
|
||||||
case "9":
|
case "9":
|
||||||
return;
|
return;
|
||||||
@@ -76,7 +80,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<>();
|
||||||
Connection con = DbUtil.getConnection();
|
Connection con = DbUtil.getConnection();
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||||
@@ -86,12 +90,116 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
|
case "2":
|
||||||
|
String idStr = inputCheck("ID", true);
|
||||||
|
int id = Integer.parseInt(idStr);
|
||||||
|
empList = empDao.selectById(id);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (String emp : empList) {
|
for (EmployeeDTO emp : empList) {
|
||||||
System.out.println(emp);
|
System.out.println(
|
||||||
|
|
||||||
|
emp.getId() + "\t" +
|
||||||
|
emp.getName() + "\t" +
|
||||||
|
emp.getKana() + "\t" +
|
||||||
|
emp.getEmail() + "\t" +
|
||||||
|
emp.getPass() + "\t" +
|
||||||
|
emp.getIdDep()
|
||||||
|
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void empInsert()
|
||||||
|
throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||||
|
//List<EmployeeDTO> empList = new ArrayList<>();
|
||||||
|
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);
|
||||||
|
String depIdstr = inputCheck("部署ID", true);
|
||||||
|
|
||||||
|
int depId = Integer.parseInt(depIdstr);
|
||||||
|
|
||||||
|
System.out.println("登録が完了しました");
|
||||||
|
|
||||||
|
empDao.empInsert(name, kana, mail, pass, depId);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void empUpdate() throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException{
|
||||||
|
Connection con = DbUtil.getConnection();
|
||||||
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
|
||||||
|
String idstr = inputCheck("ID",true);
|
||||||
|
String name = inputCheck("社員名", false);
|
||||||
|
String kana = inputCheck("フリガナ", false);
|
||||||
|
String mail = inputCheck("メールアドレス", false);
|
||||||
|
String pass = inputCheck("パスワード", false);
|
||||||
|
String depIdstr = inputCheck("部署ID", true);
|
||||||
|
|
||||||
|
int id = Integer.parseInt(idstr);
|
||||||
|
|
||||||
|
int depId = Integer.parseInt(depIdstr);
|
||||||
|
|
||||||
|
System.out.println("更新が完了しました");
|
||||||
|
|
||||||
|
empDao.empUpdate(id,name, kana, mail, pass, depId);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void empDelete() throws ClassNotFoundException, SQLException {
|
||||||
|
//List<EmployeeDTO> empList = new ArrayList<>();
|
||||||
|
Connection con = DbUtil.getConnection();
|
||||||
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
String idStr = inputCheck("ID", true);
|
||||||
|
int id = Integer.parseInt(idStr);
|
||||||
|
|
||||||
|
List<EmployeeDTO> checkList = empDao.selectById(id);
|
||||||
|
if (checkList.size() == 0) {
|
||||||
|
System.out.println("入力された値で情報がみつかりませんでした");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("指定されたIDの情報を削除します? Y/y");
|
||||||
|
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||||
|
String yY = sc.nextLine();
|
||||||
|
if (!"y".equals(yY) && !"Y".equals(yY)) {
|
||||||
|
System.out.println("削除をキャンセルしました");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
empDao.empDelete(id);
|
||||||
|
System.out.println("削除が完了しました");
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection(empDao.con);
|
DbUtil.closeConnection(empDao.con);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user