Compare commits
4 Commits
9932633268
...
ishihara
| Author | SHA1 | Date | |
|---|---|---|---|
| 33070d1dc6 | |||
| 9b993a436c | |||
| c7f723ffe2 | |||
| 395f2f4f90 |
8
.classpath
Normal file
8
.classpath
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="lib" path="lib/mysql-connector-java-8.0.23.jar"/>
|
||||||
|
<classpathentry kind="lib" path="実行例/DAO_Simple.jar"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/bin/
|
||||||
23
.project
Normal file
23
.project
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>em-jdbc</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
7
.settings/.jsdtscope
Normal file
7
.settings/.jsdtscope
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
|
||||||
|
<classpathentry kind="src" path=""/>
|
||||||
|
<classpathentry kind="output" path=""/>
|
||||||
|
</classpath>
|
||||||
1
.settings/org.eclipse.wst.jsdt.ui.superType.container
Normal file
1
.settings/org.eclipse.wst.jsdt.ui.superType.container
Normal file
@@ -0,0 +1 @@
|
|||||||
|
org.eclipse.wst.jsdt.launching.JRE_CONTAINER
|
||||||
1
.settings/org.eclipse.wst.jsdt.ui.superType.name
Normal file
1
.settings/org.eclipse.wst.jsdt.ui.superType.name
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Global
|
||||||
243
src/com/rhizome/example/dao/DepartmentDAO.java
Normal file
243
src/com/rhizome/example/dao/DepartmentDAO.java
Normal file
@@ -0,0 +1,243 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -13,6 +13,7 @@ import java.sql.SQLException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.rhizome.example.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
public class EmployeeDAO {
|
public class EmployeeDAO {
|
||||||
@@ -23,6 +24,8 @@ public class EmployeeDAO {
|
|||||||
private static final String MAIL_ADDRESS = "mail_address";
|
private static final String MAIL_ADDRESS = "mail_address";
|
||||||
private static final String PASSWORD = "password";
|
private static final String PASSWORD = "password";
|
||||||
private static final String ID_DEPARTMENT = "id_department";
|
private static final String ID_DEPARTMENT = "id_department";
|
||||||
|
private static final String NM_DEPARTMENT = "nm_department";
|
||||||
|
|
||||||
|
|
||||||
/** DBコネクション */
|
/** DBコネクション */
|
||||||
public Connection con;
|
public Connection con;
|
||||||
@@ -48,19 +51,35 @@ public class EmployeeDAO {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
*/
|
*/
|
||||||
public List<String> selectAll() throws SQLException, ClassNotFoundException {
|
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
|
||||||
List<String> 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(" employee.id_employee ");
|
||||||
sql.append(" FROM ");
|
sql.append(" ,employee.nm_employee ");
|
||||||
sql.append(" " + TBL_NAME);
|
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 ");
|
||||||
try {
|
try {
|
||||||
|
|
||||||
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.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 {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
@@ -68,4 +87,370 @@ public class EmployeeDAO {
|
|||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//社員ID検索
|
||||||
|
public List<EmployeeDTO> selectById(int id) 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.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));
|
||||||
|
|
||||||
|
//リストへ追加
|
||||||
|
rtnList.add(dto);
|
||||||
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
DbUtil.closeStatement(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/com/rhizome/example/dto/DepartmentDTO.java
Normal file
23
src/com/rhizome/example/dto/DepartmentDTO.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
59
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
59
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
@@ -0,0 +1,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;
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,7 +6,10 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import com.rhizome.example.dao.DepartmentDAO;
|
||||||
import com.rhizome.example.dao.EmployeeDAO;
|
import com.rhizome.example.dao.EmployeeDAO;
|
||||||
|
import com.rhizome.example.dto.DepartmentDTO;
|
||||||
|
import com.rhizome.example.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -50,15 +53,51 @@ public class Main {
|
|||||||
String ope = sc.nextLine();
|
String ope = sc.nextLine();
|
||||||
switch (ope) {
|
switch (ope) {
|
||||||
case "1":
|
case "1":
|
||||||
|
//社員テーブル
|
||||||
|
if (tblFlg == 0) {
|
||||||
empSelect();
|
empSelect();
|
||||||
|
|
||||||
|
//部署テーブル
|
||||||
|
} else {
|
||||||
|
deptSelect();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
|
//社員テーブル
|
||||||
|
if (tblFlg == 0) {
|
||||||
|
empInsert();
|
||||||
|
|
||||||
|
//部署テーブル
|
||||||
|
} else {
|
||||||
|
deptInsert();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "3":
|
case "3":
|
||||||
|
//社員テーブル
|
||||||
|
if (tblFlg == 0) {
|
||||||
|
empUpdate();
|
||||||
|
|
||||||
|
//部署テーブル
|
||||||
|
} else {
|
||||||
|
deptUpdate();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "4":
|
case "4":
|
||||||
|
//社員テーブル
|
||||||
|
if (tblFlg == 0) {
|
||||||
|
empDelete();
|
||||||
|
|
||||||
|
//部署テーブル
|
||||||
|
} else {
|
||||||
|
deptDelete();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "9":
|
case "9":
|
||||||
|
if (tblFlg == 0) {
|
||||||
|
tblFlg = 1;
|
||||||
|
} else {
|
||||||
|
tblFlg = 0;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
case "0":
|
case "0":
|
||||||
System.out.println("システムを終了します");
|
System.out.println("システムを終了します");
|
||||||
@@ -76,7 +115,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,16 +125,137 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
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;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (String emp : empList) {
|
|
||||||
System.out.println(emp);
|
if (empList.size() == 0) {
|
||||||
|
System.out.println("入力された値で情報がみつかりませんでした");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//最大文字数取得
|
||||||
|
int maxName = 0;
|
||||||
|
int maxKana = 0;
|
||||||
|
int maxMail = 0;
|
||||||
|
int maxPassword = 0;
|
||||||
|
int maxDepartment = 0;
|
||||||
|
|
||||||
|
for (EmployeeDTO emp : empList) {
|
||||||
|
|
||||||
|
if (getDisplayLength(emp.getNmEmployee()) > maxName) {
|
||||||
|
maxName = getDisplayLength(emp.getNmEmployee());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (getDisplayLength(emp.getKnEmployee()) > maxKana) {
|
||||||
|
maxKana = getDisplayLength(emp.getKnEmployee());
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
} 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* フラグを判定し、テーブル名を取得する
|
* フラグを判定し、テーブル名を取得する
|
||||||
@@ -146,4 +306,401 @@ public class Main {
|
|||||||
}
|
}
|
||||||
return str;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package com.rhizome.example.util;
|
package com.rhizome.example.util;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class DbUtil {
|
public class DbUtil {
|
||||||
// 定数定義(共通)
|
// 定数定義(共通)
|
||||||
@@ -42,6 +45,18 @@ public class DbUtil {
|
|||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
public static Connection getConnection()
|
||||||
|
throws ClassNotFoundException, SQLException {
|
||||||
|
|
||||||
|
Class.forName(DB_DRIVER);
|
||||||
|
|
||||||
|
con = DriverManager.getConnection(
|
||||||
|
DB_URL,
|
||||||
|
DB_USER,
|
||||||
|
DB_PASS);
|
||||||
|
|
||||||
|
return con;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
@@ -53,6 +68,14 @@ public class DbUtil {
|
|||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
public static void closeStatement(PreparedStatement stmt)
|
||||||
|
throws SQLException {
|
||||||
|
|
||||||
|
if (stmt != null) {
|
||||||
|
stmt.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
@@ -64,5 +87,11 @@ public class DbUtil {
|
|||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
public static void closeConnection(Connection con)
|
||||||
|
throws SQLException {
|
||||||
|
|
||||||
|
if (con != null) {
|
||||||
|
con.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user