Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6493baccef | |||
| f909550b87 | |||
| df7972c662 |
@@ -1,243 +0,0 @@
|
||||
/*
|
||||
* EmployeeDAO.java
|
||||
* 作成 IT-College 2020
|
||||
*------------------------------------------------------------
|
||||
* Copyright(c) Rhizome Inc. All Rights Reserved.
|
||||
*/
|
||||
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 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<DepartmentDTO> selectAll() throws SQLException, ClassNotFoundException {
|
||||
List<DepartmentDTO> 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 dto = new DepartmentDTO();
|
||||
|
||||
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||
rtnList.add(dto);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//部署ID検索
|
||||
public List<DepartmentDTO> selectById(int id) throws SQLException {
|
||||
//戻り値用リスト
|
||||
List<DepartmentDTO> rtnList = new ArrayList<>();
|
||||
//SQL作成
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" SELECT * ");
|
||||
sql.append(" FROM department ");
|
||||
sql.append(" WHERE id_department = ? ");
|
||||
|
||||
try {
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
//部署IDを設定
|
||||
stmt.setInt(1, id);
|
||||
//SQL実行
|
||||
rs = stmt.executeQuery();
|
||||
//検索結果をDTOへ格納
|
||||
while (rs.next()) {
|
||||
//DTO生成
|
||||
DepartmentDTO dto = new DepartmentDTO();
|
||||
//検索結果をDTOへセット
|
||||
dto.setIdDepartment( rs.getInt(ID_DEPARTMENT));
|
||||
dto.setNmDepartment( rs.getString(NM_DEPARTMENT));
|
||||
|
||||
//リストへ追加
|
||||
rtnList.add(dto);
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
//検索結果を返却
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//部署名検索
|
||||
public List<DepartmentDTO> selectByName(String name) throws SQLException {
|
||||
//戻り値用リスト
|
||||
List<DepartmentDTO> rtnList = new ArrayList<>();
|
||||
//SQL作成
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.append(" SELECT * ");
|
||||
sql.append(" FROM department ");
|
||||
sql.append(" WHERE nm_department = ? ");
|
||||
|
||||
try {
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
//部署名を設定
|
||||
stmt.setString(1, name);
|
||||
//SQL実行
|
||||
rs = stmt.executeQuery();
|
||||
//検索結果をDTOへ格納
|
||||
while (rs.next()) {
|
||||
//DTO生成
|
||||
DepartmentDTO dto = new DepartmentDTO();
|
||||
//検索結果をDTOへセット
|
||||
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||
|
||||
//リストへ追加
|
||||
rtnList.add(dto);
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
|
||||
//検索結果を返却
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//部署情報削除
|
||||
public void deptDelete(int id) throws SQLException {
|
||||
//SQL作成
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" DELETE ");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" " + ID_DEPARTMENT + " = ?");
|
||||
|
||||
try {
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
//値設定
|
||||
stmt.setInt(1, id);
|
||||
//SQL実行
|
||||
stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
//部署情報登録
|
||||
public void deptInsert(
|
||||
String departmentName)
|
||||
throws SQLException {
|
||||
//SQL作成
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.append(" INSERT INTO department ");
|
||||
sql.append(" ( ");
|
||||
sql.append(" nm_department ");
|
||||
sql.append(" ) ");
|
||||
sql.append(" VALUES ");
|
||||
sql.append(" (?) ");
|
||||
|
||||
try {
|
||||
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
//値設定
|
||||
stmt.setString(1, departmentName);
|
||||
//SQL実行
|
||||
stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
//部署情報更新
|
||||
public void deptUpdate(
|
||||
int id,
|
||||
String departmentName)
|
||||
throws SQLException {
|
||||
|
||||
//SQL作成
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.append(" UPDATE department ");
|
||||
sql.append(" SET ");
|
||||
sql.append(" nm_department = ? ");
|
||||
sql.append(" WHERE id_department = ? ");
|
||||
|
||||
try {
|
||||
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
//値設定
|
||||
stmt.setString(1, departmentName);
|
||||
stmt.setInt(2, id);
|
||||
|
||||
//SQL実行
|
||||
stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
|
||||
//Statement切断
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
//部署ID存在確認
|
||||
public boolean existsDepartment(int idDepartment)
|
||||
throws SQLException {
|
||||
|
||||
List<DepartmentDTO> deptList = selectById(idDepartment);
|
||||
|
||||
return deptList.size() > 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,8 +24,6 @@ public class EmployeeDAO {
|
||||
private static final String MAIL_ADDRESS = "mail_address";
|
||||
private static final String PASSWORD = "password";
|
||||
private static final String ID_DEPARTMENT = "id_department";
|
||||
private static final String NM_DEPARTMENT = "nm_department";
|
||||
|
||||
|
||||
/** DBコネクション */
|
||||
public Connection con;
|
||||
@@ -55,402 +53,133 @@ public class EmployeeDAO {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" employee.id_employee ");
|
||||
sql.append(" ,employee.nm_employee ");
|
||||
sql.append(" ,employee.kn_employee ");
|
||||
sql.append(" ,employee.mail_address ");
|
||||
sql.append(" ,employee.password ");
|
||||
sql.append(" ,employee.id_department ");
|
||||
sql.append(" ,department.nm_department ");
|
||||
sql.append(" FROM employee ");
|
||||
sql.append(" INNER JOIN department ");
|
||||
sql.append(" ON employee.id_department = department.id_department ");
|
||||
sql.append(" " + "*");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
try {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
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"));
|
||||
|
||||
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));
|
||||
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||
rtnList.add(dto);
|
||||
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//社員ID検索
|
||||
public List<EmployeeDTO> selectById(int id) throws SQLException {
|
||||
//戻り値用リスト
|
||||
public java.util.List<EmployeeDTO> selectById(int id)
|
||||
throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
//SQL作成
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" employee.id_employee ");
|
||||
sql.append(" ,employee.nm_employee ");
|
||||
sql.append(" ,employee.kn_employee ");
|
||||
sql.append(" ,employee.mail_address ");
|
||||
sql.append(" ,employee.password ");
|
||||
sql.append(" ,employee.id_department ");
|
||||
sql.append(" ,department.nm_department ");
|
||||
sql.append(" FROM employee ");
|
||||
sql.append(" INNER JOIN department ");
|
||||
sql.append(" ON employee.id_department = department.id_department ");
|
||||
sql.append(" WHERE employee.id_employee = ? ");
|
||||
sql.append(" SELECT * FROM ");
|
||||
sql.append(TBL_NAME);
|
||||
sql.append(" WHERE id_employee = ?");
|
||||
|
||||
try {
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
//社員IDを設定
|
||||
stmt.setInt(1, id);
|
||||
//SQL実行
|
||||
rs = stmt.executeQuery();
|
||||
//検索結果をDTOへ格納
|
||||
while (rs.next()) {
|
||||
//DTO生成
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
//検索結果をDTOへセット
|
||||
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));
|
||||
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||
|
||||
//リストへ追加
|
||||
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(stmt);
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
//検索結果を返却
|
||||
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
// 社員名検索
|
||||
public List<EmployeeDTO> selectByName(String name) throws SQLException {
|
||||
//戻り値用リスト
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
//SQL作成
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" employee.id_employee ");
|
||||
sql.append(" ,employee.nm_employee ");
|
||||
sql.append(" ,employee.kn_employee ");
|
||||
sql.append(" ,employee.mail_address ");
|
||||
sql.append(" ,employee.password ");
|
||||
sql.append(" ,employee.id_department ");
|
||||
sql.append(" ,department.nm_department ");
|
||||
sql.append(" FROM employee ");
|
||||
sql.append(" INNER JOIN department ");
|
||||
sql.append(" ON employee.id_department = department.id_department ");
|
||||
sql.append(" WHERE employee.nm_employee = ? ");
|
||||
|
||||
try {
|
||||
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
|
||||
//社員名を設定
|
||||
stmt.setString(1, name);
|
||||
|
||||
//SQL実行
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
//検索結果をDTOへ格納
|
||||
while (rs.next()) {
|
||||
|
||||
//DTO生成
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
|
||||
//検索結果をDTOへセット
|
||||
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));
|
||||
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||
|
||||
//リストへ追加
|
||||
rtnList.add(dto);
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
|
||||
//検索結果を返却
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//フリガナ検索
|
||||
public List<EmployeeDTO> selectByKana(String kana) throws SQLException {
|
||||
//戻り値用リスト
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
//SQL作成
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" employee.id_employee ");
|
||||
sql.append(" ,employee.nm_employee ");
|
||||
sql.append(" ,employee.kn_employee ");
|
||||
sql.append(" ,employee.mail_address ");
|
||||
sql.append(" ,employee.password ");
|
||||
sql.append(" ,employee.id_department ");
|
||||
sql.append(" ,department.nm_department ");
|
||||
sql.append(" FROM employee ");
|
||||
sql.append(" INNER JOIN department ");
|
||||
sql.append(" ON employee.id_department = department.id_department ");
|
||||
sql.append(" WHERE employee.kn_employee = ? ");
|
||||
|
||||
try {
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
//フリガナ設定
|
||||
stmt.setString(1, kana);
|
||||
//SQL実行
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
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));
|
||||
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||
|
||||
rtnList.add(dto);
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//メールアドレス検索
|
||||
public List<EmployeeDTO> selectByMail(String mail) throws SQLException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" employee.id_employee ");
|
||||
sql.append(" ,employee.nm_employee ");
|
||||
sql.append(" ,employee.kn_employee ");
|
||||
sql.append(" ,employee.mail_address ");
|
||||
sql.append(" ,employee.password ");
|
||||
sql.append(" ,employee.id_department ");
|
||||
sql.append(" ,department.nm_department ");
|
||||
sql.append(" FROM employee ");
|
||||
sql.append(" INNER JOIN department ");
|
||||
sql.append(" ON employee.id_department = department.id_department ");
|
||||
sql.append(" WHERE employee.mail_address = ? ");
|
||||
|
||||
try {
|
||||
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
stmt.setString(1, mail);
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
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));
|
||||
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||
|
||||
rtnList.add(dto);
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//部署名検索
|
||||
|
||||
public List<EmployeeDTO> selectByDepartment(String department) throws SQLException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" employee.id_employee ");
|
||||
sql.append(" ,employee.nm_employee ");
|
||||
sql.append(" ,employee.kn_employee ");
|
||||
sql.append(" ,employee.mail_address ");
|
||||
sql.append(" ,employee.password ");
|
||||
sql.append(" ,employee.id_department ");
|
||||
sql.append(" ,department.nm_department ");
|
||||
sql.append(" FROM employee ");
|
||||
sql.append(" INNER JOIN department ");
|
||||
sql.append(" ON employee.id_department = department.id_department ");
|
||||
sql.append(" WHERE department.nm_department = ? ");
|
||||
|
||||
try {
|
||||
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
stmt.setString(1, department);
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
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));
|
||||
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||
|
||||
rtnList.add(dto);
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//社員情報削除
|
||||
public void empDelete(int id) throws SQLException {
|
||||
//SQL作成
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" DELETE ");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" " + ID_EMPLOYEE + " = ?");
|
||||
|
||||
try {
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
//値設定
|
||||
stmt.setInt(1, id);
|
||||
//SQL実行
|
||||
stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
//社員情報登録
|
||||
public void empInsert(
|
||||
String name,
|
||||
String kana,
|
||||
String mail,
|
||||
String password,
|
||||
int departmentId)
|
||||
throws SQLException {
|
||||
//SQL作成
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.append(" INSERT INTO employee ");
|
||||
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 ");
|
||||
sql.append(" (?, ?, ?, ?, ?) ");
|
||||
|
||||
try {
|
||||
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
//値設定
|
||||
stmt.setString(1, name);
|
||||
stmt.setString(2, kana);
|
||||
stmt.setString(3, mail);
|
||||
stmt.setString(4, password);
|
||||
stmt.setInt(5, departmentId);
|
||||
|
||||
//SQL実行
|
||||
stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
//社員情報更新
|
||||
public void empUpdate(
|
||||
int id,
|
||||
String name,
|
||||
String kana,
|
||||
String mail,
|
||||
String password,
|
||||
int departmentId)
|
||||
throws SQLException {
|
||||
|
||||
//SQL作成
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.append(" UPDATE employee ");
|
||||
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 id_employee = ? ");
|
||||
|
||||
try {
|
||||
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
//値設定
|
||||
stmt.setString(1, name);
|
||||
stmt.setString(2, kana);
|
||||
stmt.setString(3, mail);
|
||||
stmt.setString(4, password);
|
||||
stmt.setInt(5, departmentId);
|
||||
stmt.setInt(6, id);
|
||||
|
||||
// SQL実行
|
||||
stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
|
||||
// Statement切断
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
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 ");
|
||||
sql.append(TBL_NAME);
|
||||
sql.append(" SET ");
|
||||
sql.append(" nm_employee = ?, kn_employee = ?, mail_address = ?, password = ?, id_department = ? ");
|
||||
sql.append(" WHERE id_employee = ? ");
|
||||
|
||||
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.setInt(6, id);
|
||||
|
||||
this.stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.rhizome.example.dto;
|
||||
|
||||
public class DepartmentDTO {
|
||||
|
||||
private Integer idDepartment;
|
||||
private String nmDepartment;
|
||||
|
||||
public Integer getIdDepartment() {
|
||||
return idDepartment;
|
||||
}
|
||||
|
||||
public void setIdDepartment(Integer idDepartment) {
|
||||
this.idDepartment = idDepartment;
|
||||
}
|
||||
|
||||
public String getNmDepartment() {
|
||||
return nmDepartment;
|
||||
}
|
||||
|
||||
public void setNmDepartment(String nmDepartment) {
|
||||
this.nmDepartment = nmDepartment;
|
||||
}
|
||||
}
|
||||
@@ -2,58 +2,59 @@ package com.rhizome.example.dto;
|
||||
|
||||
public class EmployeeDTO {
|
||||
|
||||
private Integer idEmployee;
|
||||
private String nmEmployee;
|
||||
private String knEmployee;
|
||||
private String mailAddress;
|
||||
private String password;
|
||||
private Integer idDepartment;
|
||||
private String nmDepartment;
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String kana;
|
||||
private String email;
|
||||
private String pass;
|
||||
private Integer idDep;
|
||||
|
||||
|
||||
public Integer getIdEmployee() {
|
||||
return idEmployee;
|
||||
}
|
||||
public void setIdEmployee(Integer 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 getMailAddress() {
|
||||
return mailAddress;
|
||||
}
|
||||
public void setMailAddress(String mailAddress) {
|
||||
this.mailAddress = mailAddress;
|
||||
}
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
public Integer getIdDepartment() {
|
||||
return idDepartment;
|
||||
}
|
||||
public void setIdDepartment(Integer idDepartment) {
|
||||
this.idDepartment = idDepartment;
|
||||
}
|
||||
|
||||
public String getNmDepartment() {
|
||||
return nmDepartment;
|
||||
}
|
||||
|
||||
public void setNmDepartment(String nmDepartment) {
|
||||
this.nmDepartment = nmDepartment;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,9 +6,7 @@ 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;
|
||||
|
||||
@@ -53,51 +51,18 @@ public class Main {
|
||||
String ope = sc.nextLine();
|
||||
switch (ope) {
|
||||
case "1":
|
||||
//社員テーブル
|
||||
if (tblFlg == 0) {
|
||||
empSelect();
|
||||
|
||||
//部署テーブル
|
||||
} else {
|
||||
deptSelect();
|
||||
}
|
||||
empSelect();
|
||||
break;
|
||||
case "2":
|
||||
//社員テーブル
|
||||
if (tblFlg == 0) {
|
||||
empInsert();
|
||||
|
||||
//部署テーブル
|
||||
} else {
|
||||
deptInsert();
|
||||
}
|
||||
empInsert();
|
||||
break;
|
||||
case "3":
|
||||
//社員テーブル
|
||||
if (tblFlg == 0) {
|
||||
empUpdate();
|
||||
|
||||
//部署テーブル
|
||||
} else {
|
||||
deptUpdate();
|
||||
}
|
||||
empUpdate();
|
||||
break;
|
||||
case "4":
|
||||
//社員テーブル
|
||||
if (tblFlg == 0) {
|
||||
empDelete();
|
||||
|
||||
//部署テーブル
|
||||
} else {
|
||||
deptDelete();
|
||||
}
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
if (tblFlg == 0) {
|
||||
tblFlg = 1;
|
||||
} else {
|
||||
tblFlg = 0;
|
||||
}
|
||||
return;
|
||||
case "0":
|
||||
System.out.println("システムを終了します");
|
||||
@@ -125,136 +90,121 @@ public class Main {
|
||||
case "1":
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
|
||||
case "2":
|
||||
//検索項目選択
|
||||
System.out.println("1:ID 2:社員名 3:フリガナ 4:メールアドレス 5:部署名");
|
||||
String condition = sc.nextLine();
|
||||
switch (condition) {
|
||||
|
||||
//ID検索
|
||||
case "1":
|
||||
String id = inputCheck("ID", true);
|
||||
empList = empDao.selectById(Integer.parseInt(id));
|
||||
|
||||
break;
|
||||
|
||||
//社員名検索
|
||||
case "2":
|
||||
String name = inputCheck("社員名", false);
|
||||
empList = empDao.selectByName(name);
|
||||
|
||||
break;
|
||||
|
||||
//フリガナ検索
|
||||
case "3":
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
empList = empDao.selectByKana(kana);
|
||||
|
||||
break;
|
||||
|
||||
//メールアドレス検索
|
||||
case "4":
|
||||
String mail = inputCheck("メールアドレス", false);
|
||||
empList = empDao.selectByMail(mail);
|
||||
|
||||
break;
|
||||
|
||||
//部署名検索
|
||||
case "5":
|
||||
String department = inputCheck("部署名", false);
|
||||
empList = empDao.selectByDepartment(department);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
String idStr = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(idStr);
|
||||
empList = empDao.selectById(id);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
for (EmployeeDTO emp : empList) {
|
||||
System.out.println(
|
||||
|
||||
if (empList.size() == 0) {
|
||||
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);
|
||||
int id = Integer.parseInt(idstr);
|
||||
|
||||
List<EmployeeDTO> checkList = empDao.selectById(id);
|
||||
if (checkList.size() == 0) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
return;
|
||||
}
|
||||
|
||||
//最大文字数取得
|
||||
int maxName = 0;
|
||||
int maxKana = 0;
|
||||
int maxMail = 0;
|
||||
int maxPassword = 0;
|
||||
int maxDepartment = 0;
|
||||
String name = inputCheck("社員名", false);
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
String mail = inputCheck("メールアドレス", false);
|
||||
String pass = inputCheck("パスワード", false);
|
||||
String depIdstr = inputCheck("部署ID", true);
|
||||
|
||||
for (EmployeeDTO emp : empList) {
|
||||
int depId = Integer.parseInt(depIdstr);
|
||||
|
||||
if (getDisplayLength(emp.getNmEmployee()) > maxName) {
|
||||
maxName = getDisplayLength(emp.getNmEmployee());
|
||||
}
|
||||
empDao.empUpdate(id, name, kana, mail, pass, depId);
|
||||
|
||||
if (getDisplayLength(emp.getKnEmployee()) > maxKana) {
|
||||
maxKana = getDisplayLength(emp.getKnEmployee());
|
||||
}
|
||||
System.out.println("更新が完了しました");
|
||||
|
||||
if (getDisplayLength(emp.getMailAddress()) > maxMail) {
|
||||
maxMail = getDisplayLength(emp.getMailAddress());
|
||||
}
|
||||
|
||||
if (getDisplayLength(emp.getPassword()) > maxPassword) {
|
||||
maxPassword = getDisplayLength(emp.getPassword());
|
||||
}
|
||||
|
||||
if (getDisplayLength(emp.getNmDepartment())
|
||||
> maxDepartment) {
|
||||
maxDepartment = getDisplayLength(emp.getNmDepartment());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (EmployeeDTO emp : empList) {
|
||||
|
||||
System.out.println(
|
||||
adjust(String.valueOf(emp.getIdEmployee()),10)
|
||||
+ adjust(emp.getNmEmployee(), maxName + 5)
|
||||
+ adjust(emp.getKnEmployee(), maxKana + 5)
|
||||
+ adjust(emp.getMailAddress(), maxMail + 5)
|
||||
+ adjust(emp.getPassword(), maxPassword + 5)
|
||||
+ adjust(emp.getNmDepartment(), maxDepartment + 5));
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
|
||||
}
|
||||
//表示幅取得
|
||||
//半角=1
|
||||
//全角=2
|
||||
public static int getDisplayLength(String str) {
|
||||
int length = 0;
|
||||
for (char c : str.toCharArray()) {
|
||||
//ASCIIコードで算出
|
||||
if (c <= 127) {
|
||||
length += 1;
|
||||
} else {
|
||||
length += 2;
|
||||
|
||||
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 {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
|
||||
//指定文字数まで空白を追加
|
||||
public static String adjust(String str, int length) {
|
||||
StringBuilder sb = new StringBuilder(str);
|
||||
|
||||
while (getDisplayLength(sb.toString()) < length) {
|
||||
sb.append(" ");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -306,401 +256,4 @@ public class Main {
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
//更新用入力
|
||||
//Enterのみの場合は現在値を返す
|
||||
public static String inputUpdate(
|
||||
String titleName,
|
||||
String currentValue) {
|
||||
|
||||
System.out.println(
|
||||
titleName
|
||||
+ "を入力してください"
|
||||
+ "(現在値:" + currentValue + ")");
|
||||
|
||||
String str = sc.nextLine();
|
||||
|
||||
//Enterのみの場合は現在値を返却
|
||||
if (str.trim().length() == 0) {
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//社員情報を削除する
|
||||
public static void empDelete() throws ClassNotFoundException, SQLException {
|
||||
//DB接続
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
|
||||
try {
|
||||
//削除対象ID入力
|
||||
String strId = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(strId);
|
||||
|
||||
//入力されたID確認
|
||||
List<EmployeeDTO> empList = empDao.selectById(id);
|
||||
//データが存在しない場合
|
||||
if (empList.size() == 0) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//削除確認メッセージ表示
|
||||
System.out.println("指定されたIDの情報を削除しますか? Y/y");
|
||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||
|
||||
String answer = sc.nextLine();
|
||||
|
||||
//Yまたはyが入力された場合のみ削除
|
||||
if ("Y".equals(answer) || "y".equals(answer)) {
|
||||
//社員情報削除
|
||||
empDao.empDelete(id);
|
||||
|
||||
System.out.println("削除が完了しました");
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
//社員情報登録
|
||||
public static void empInsert() throws ClassNotFoundException, SQLException {
|
||||
|
||||
//DB接続
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
DepartmentDAO deptDao = new DepartmentDAO(con);
|
||||
|
||||
try {
|
||||
|
||||
//社員名入力
|
||||
String name = inputCheck("社員名", false);
|
||||
//フリガナ入力
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
//メールアドレス入力
|
||||
String mail = inputCheck("メールアドレス", false);
|
||||
//パスワード入力
|
||||
String password = inputCheck("パスワード", false);
|
||||
//部署ID入力
|
||||
String departmentId = inputCheck("部署ID", true);
|
||||
|
||||
//部署IDを数値変換
|
||||
int deptId = Integer.parseInt(departmentId);
|
||||
|
||||
//入力された部署IDが存在するかチェック
|
||||
if (!deptDao.existsDepartment(deptId)) {
|
||||
|
||||
System.out.println("入力された部署IDは存在しません");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//登録確認
|
||||
System.out.println("社員情報を登録しますか? Y/y");
|
||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||
|
||||
String answer = sc.nextLine();
|
||||
|
||||
if (!"Y".equals(answer) && !"y".equals(answer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//登録処理
|
||||
empDao.empInsert(
|
||||
name,
|
||||
kana,
|
||||
mail,
|
||||
password,
|
||||
deptId);
|
||||
|
||||
System.out.println("登録が完了しました");
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
//社員情報更新
|
||||
public static void empUpdate() throws ClassNotFoundException, SQLException {
|
||||
//DB接続
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
//部署テーブル存在確認用DAO生成
|
||||
DepartmentDAO deptDao = new DepartmentDAO(con);
|
||||
|
||||
try {
|
||||
//更新対象ID入力
|
||||
String strId = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(strId);
|
||||
|
||||
//存在チェック
|
||||
List<EmployeeDTO> empList = empDao.selectById(id);
|
||||
|
||||
if (empList.size() == 0) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
EmployeeDTO emp = empList.get(0);
|
||||
|
||||
//Enterのみの場合は現在値をそのまま使用
|
||||
//社員名入力
|
||||
String name = inputUpdate("社員名", emp.getNmEmployee());
|
||||
//フリガナ入力
|
||||
String kana = inputUpdate("フリガナ", emp.getKnEmployee());
|
||||
//メールアドレス入力
|
||||
String mail = inputUpdate("メールアドレス", emp.getMailAddress());
|
||||
//パスワード入力
|
||||
String password = inputUpdate("パスワード", emp.getPassword());
|
||||
//部署ID入力
|
||||
String departmentId = inputUpdate("部署ID", String.valueOf(emp.getIdDepartment()));
|
||||
|
||||
//部署IDを数値変換
|
||||
int deptId = Integer.parseInt(departmentId);
|
||||
//入力された部署IDが存在するかチェック
|
||||
if (!deptDao.existsDepartment(deptId)) {
|
||||
System.out.println("入力された部署IDは存在しません");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//更新確認
|
||||
System.out.println("社員情報を更新しますか? Y/y");
|
||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||
|
||||
String answer = sc.nextLine();
|
||||
|
||||
if (!"Y".equals(answer) && !"y".equals(answer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//更新処理
|
||||
empDao.empUpdate(
|
||||
id,
|
||||
name,
|
||||
kana,
|
||||
mail,
|
||||
password,
|
||||
deptId);
|
||||
|
||||
System.out.println("更新が完了しました");
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
//テーブル切り替え
|
||||
//ここから部署テーブル
|
||||
//部署テーブルから情報を検索する
|
||||
public static void deptSelect() throws ClassNotFoundException, SQLException {
|
||||
|
||||
//検索結果格納用リスト
|
||||
List<DepartmentDTO> deptList = new ArrayList<>();
|
||||
//DB接続
|
||||
Connection con = DbUtil.getConnection();
|
||||
//DAO生成
|
||||
DepartmentDAO deptDao = new DepartmentDAO(con);
|
||||
//検索方法選択
|
||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||
String select = sc.nextLine();
|
||||
|
||||
try {
|
||||
|
||||
switch (select) {
|
||||
|
||||
//全件検索
|
||||
case "1":
|
||||
deptList = deptDao.selectAll();
|
||||
break;
|
||||
|
||||
//条件検索
|
||||
case "2":
|
||||
//検索項目選択
|
||||
System.out.println("1:ID 2:部署名");
|
||||
String condition = sc.nextLine();
|
||||
switch (condition) {
|
||||
|
||||
//ID検索
|
||||
case "1":
|
||||
String id = inputCheck("ID", true);
|
||||
deptList = deptDao.selectById(Integer.parseInt(id));
|
||||
|
||||
break;
|
||||
|
||||
//部署名検索
|
||||
case "2":
|
||||
String name = inputCheck("部署名", false);
|
||||
deptList = deptDao.selectByName(name);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
//メニューへ戻る
|
||||
default:
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//検索結果0件の場合
|
||||
if (deptList.size() == 0) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//検索結果表示
|
||||
for (DepartmentDTO dept : deptList) {
|
||||
|
||||
System.out.println(
|
||||
dept.getIdDepartment()
|
||||
+ "\t"
|
||||
+ dept.getNmDepartment());
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeConnection(deptDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
//部署情報登録
|
||||
public static void deptInsert() throws ClassNotFoundException, SQLException {
|
||||
//DB接続
|
||||
Connection con = DbUtil.getConnection();
|
||||
//DAO生成
|
||||
DepartmentDAO deptDao =
|
||||
new DepartmentDAO(con);
|
||||
|
||||
try {
|
||||
//部署名入力
|
||||
String departmentName = inputCheck("部署名", false);
|
||||
|
||||
//登録確認
|
||||
System.out.println("部署情報を登録しますか? Y/y");
|
||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||
|
||||
String answer = sc.nextLine();
|
||||
|
||||
if (!"Y".equals(answer) && !"y".equals(answer)) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//登録処理
|
||||
deptDao.deptInsert(departmentName);
|
||||
System.out.println("登録が完了しました");
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeConnection(deptDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
//部署情報更新
|
||||
public static void deptUpdate() throws ClassNotFoundException, SQLException {
|
||||
//DB接続
|
||||
Connection con = DbUtil.getConnection();
|
||||
//DAO生成
|
||||
DepartmentDAO deptDao = new DepartmentDAO(con);
|
||||
|
||||
try {
|
||||
//更新対象ID入力
|
||||
String strId = inputCheck("ID", true);
|
||||
|
||||
int id = Integer.parseInt(strId);
|
||||
|
||||
//存在チェック
|
||||
List<DepartmentDTO> deptList = deptDao.selectById(id);
|
||||
|
||||
//データが存在しない場合
|
||||
if (deptList.size() == 0) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
return;
|
||||
}
|
||||
|
||||
//部署名入力
|
||||
String departmentName = inputCheck("部署名", false);
|
||||
|
||||
//更新確認
|
||||
System.out.println("部署情報を更新しますか? Y/y");
|
||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||
|
||||
String answer = sc.nextLine();
|
||||
|
||||
if (!"Y".equals(answer) && !"y".equals(answer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//更新処理
|
||||
deptDao.deptUpdate(id, departmentName);
|
||||
|
||||
System.out.println("更新が完了しました");
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeConnection(
|
||||
deptDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
//部署情報を削除する
|
||||
public static void deptDelete() throws ClassNotFoundException, SQLException {
|
||||
//DB接続
|
||||
Connection con = DbUtil.getConnection();
|
||||
//DAO生成
|
||||
DepartmentDAO deptDao = new DepartmentDAO(con);
|
||||
|
||||
try {
|
||||
//削除対象ID入力
|
||||
String strId = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(strId);
|
||||
|
||||
//入力されたIDの存在確認
|
||||
List<DepartmentDTO> deptList = deptDao.selectById(id);
|
||||
|
||||
//データが存在しない場合
|
||||
if (deptList.size() == 0) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//削除確認メッセージ表示
|
||||
System.out.println("指定されたIDの情報を削除しますか? Y/y");
|
||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||
String answer = sc.nextLine();
|
||||
|
||||
//Yまたはyが入力された場合のみ削除
|
||||
if ("Y".equals(answer) || "y".equals(answer)) {
|
||||
|
||||
//部署情報削除
|
||||
deptDao.deptDelete(id);
|
||||
System.out.println("削除が完了しました");
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
||||
DbUtil.closeConnection(deptDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package com.rhizome.example.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DbUtil {
|
||||
// 定数定義(共通)
|
||||
@@ -45,16 +43,8 @@ public class DbUtil {
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
|
||||
public static Connection getConnection()
|
||||
throws ClassNotFoundException, SQLException {
|
||||
|
||||
Class.forName(DB_DRIVER);
|
||||
|
||||
con = DriverManager.getConnection(
|
||||
DB_URL,
|
||||
DB_USER,
|
||||
DB_PASS);
|
||||
|
||||
public static java.sql.Connection getConnection() throws java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
return con;
|
||||
}
|
||||
|
||||
@@ -68,15 +58,12 @@ public class DbUtil {
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
|
||||
public static void closeStatement(PreparedStatement stmt)
|
||||
throws SQLException {
|
||||
|
||||
public static void closeStatement(java.sql.PreparedStatement stmt) throws java.sql.SQLException {
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*
|
||||
@@ -87,11 +74,15 @@ public class DbUtil {
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
|
||||
public static void closeConnection(Connection con)
|
||||
throws SQLException {
|
||||
//public static void closeConnection(Connection con) throws SQLException {
|
||||
//if (con != null) {
|
||||
//con.close();
|
||||
//}
|
||||
//
|
||||
|
||||
if (con != null) {
|
||||
public static void closeConnection(java.sql.Connection con) throws java.sql.SQLException {
|
||||
if (con != null)
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user