Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 33070d1dc6 | |||
| 9b993a436c | |||
| c7f723ffe2 | |||
| 395f2f4f90 |
@@ -3,7 +3,6 @@
|
|||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
<classpathentry kind="src" path="src"/>
|
<classpathentry kind="src" path="src"/>
|
||||||
<classpathentry kind="lib" path="lib/mysql-connector-java-8.0.23.jar"/>
|
<classpathentry kind="lib" path="lib/mysql-connector-java-8.0.23.jar"/>
|
||||||
<classpathentry kind="lib" path="mysql-connector-j-8.0.32.jar"/>
|
|
||||||
<classpathentry kind="lib" path="実行例/DAO_Simple.jar"/>
|
<classpathentry kind="lib" path="実行例/DAO_Simple.jar"/>
|
||||||
<classpathentry kind="output" path="bin"/>
|
<classpathentry kind="output" path="bin"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|||||||
6
.project
6
.project
@@ -6,18 +6,18 @@
|
|||||||
</projects>
|
</projects>
|
||||||
<buildSpec>
|
<buildSpec>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.wst.validation.validationbuilder</name>
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
</buildSpec>
|
</buildSpec>
|
||||||
<natures>
|
<natures>
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
|
||||||
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
</natures>
|
</natures>
|
||||||
</projectDescription>
|
</projectDescription>
|
||||||
|
|||||||
Binary file not shown.
@@ -1,3 +1,9 @@
|
|||||||
|
/*
|
||||||
|
* EmployeeDAO.java
|
||||||
|
* 作成 IT-College 2020
|
||||||
|
*------------------------------------------------------------
|
||||||
|
* Copyright(c) Rhizome Inc. All Rights Reserved.
|
||||||
|
*/
|
||||||
package com.rhizome.example.dao;
|
package com.rhizome.example.dao;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@@ -6,7 +12,6 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
import com.rhizome.example.dto.DepartmentDTO;
|
import com.rhizome.example.dto.DepartmentDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
@@ -16,17 +21,6 @@ public class DepartmentDAO {
|
|||||||
private static final String ID_DEPARTMENT = "id_department";
|
private static final String ID_DEPARTMENT = "id_department";
|
||||||
private static final String NM_DEPARTMENT = "nm_department";
|
private static final String NM_DEPARTMENT = "nm_department";
|
||||||
|
|
||||||
/** アクセサ */
|
|
||||||
public static String getTblName() {
|
|
||||||
return TBL_NAME;
|
|
||||||
}
|
|
||||||
public static String getIdColumnName() {
|
|
||||||
return ID_DEPARTMENT;
|
|
||||||
}
|
|
||||||
public static String getNmColumnName() {
|
|
||||||
return NM_DEPARTMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** DBコネクション */
|
/** DBコネクション */
|
||||||
public Connection con;
|
public Connection con;
|
||||||
/** DBステートメント */
|
/** DBステートメント */
|
||||||
@@ -45,7 +39,7 @@ public class DepartmentDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部署テーブルの情報を全件取得する
|
* 社員テーブルの情報を全件取得する
|
||||||
*
|
*
|
||||||
* @return 検索結果
|
* @return 検索結果
|
||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
@@ -59,13 +53,15 @@ public class DepartmentDAO {
|
|||||||
sql.append(" FROM ");
|
sql.append(" FROM ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" " + TBL_NAME);
|
||||||
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()) {
|
||||||
DepartmentDTO Department = new DepartmentDTO();
|
DepartmentDTO dto = new DepartmentDTO();
|
||||||
Department.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
|
||||||
Department.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||||
rtnList.add(Department);
|
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||||
|
rtnList.add(dto);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
@@ -73,186 +69,175 @@ public class DepartmentDAO {
|
|||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//部署ID検索
|
||||||
* 部署テーブルから指定された部署IDに一致するデータを全件取得する
|
public List<DepartmentDTO> selectById(int id) throws SQLException {
|
||||||
*
|
//戻り値用リスト
|
||||||
* @return 検索結果
|
|
||||||
* @throws SQLException
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
*/
|
|
||||||
public List<DepartmentDTO> selectById(int idDepartment) throws SQLException, ClassNotFoundException {
|
|
||||||
List<DepartmentDTO> rtnList = new ArrayList<>();
|
List<DepartmentDTO> rtnList = new ArrayList<>();
|
||||||
|
//SQL作成
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" SELECT ");
|
sql.append(" SELECT * ");
|
||||||
sql.append(" " + "*");
|
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(" FROM ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" " + TBL_NAME);
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE ");
|
||||||
sql.append(" " + ID_DEPARTMENT + " = " + "?");
|
sql.append(" " + ID_DEPARTMENT + " = ?");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
//PreparedStatement生成
|
||||||
stmt.setInt(1,idDepartment);
|
stmt = con.prepareStatement(sql.toString());
|
||||||
rs = stmt.executeQuery();
|
//値設定
|
||||||
int nRecords = 0;
|
stmt.setInt(1, id);
|
||||||
while (rs.next()) {
|
//SQL実行
|
||||||
DepartmentDTO Department = new DepartmentDTO();
|
stmt.executeUpdate();
|
||||||
Department.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
|
||||||
Department.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
|
||||||
rtnList.add(Department);
|
|
||||||
nRecords++;
|
|
||||||
}
|
|
||||||
if (nRecords==0) {
|
|
||||||
System.out.println("入力された部署IDで情報がみつかりませんでした");
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
|
||||||
|
DbUtil.closeStatement(stmt);
|
||||||
}
|
}
|
||||||
return rtnList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//部署情報登録
|
||||||
* 部署テーブルから指定された部署名に一致するデータを全件取得する
|
public void deptInsert(
|
||||||
*
|
String departmentName)
|
||||||
* @return 検索結果
|
throws SQLException {
|
||||||
* @throws SQLException
|
//SQL作成
|
||||||
* @throws ClassNotFoundException
|
|
||||||
*/
|
|
||||||
public List<DepartmentDTO> selectByNm(String NmDepartment) throws SQLException, ClassNotFoundException {
|
|
||||||
List<DepartmentDTO> rtnList = new ArrayList<>();
|
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" SELECT ");
|
|
||||||
sql.append(" " + "*");
|
|
||||||
sql.append(" FROM ");
|
|
||||||
sql.append(" " + TBL_NAME);
|
|
||||||
sql.append(" WHERE ");
|
|
||||||
sql.append(" " + NM_DEPARTMENT + " LIKE " + " CONCAT('%',?,'%') ");
|
|
||||||
try {
|
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
|
||||||
stmt.setString(1,NmDepartment);
|
|
||||||
rs = stmt.executeQuery();
|
|
||||||
int nRecords = 0;
|
|
||||||
while (rs.next()) {
|
|
||||||
DepartmentDTO Department = new DepartmentDTO();
|
|
||||||
Department.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
|
||||||
Department.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
|
||||||
rtnList.add(Department);
|
|
||||||
nRecords++;
|
|
||||||
}
|
|
||||||
if (nRecords==0) {
|
|
||||||
System.out.println("入力された部署名で情報がみつかりませんでした");
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
DbUtil.closeStatement(this.stmt);
|
|
||||||
}
|
|
||||||
return rtnList;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
sql.append(" INSERT INTO department ");
|
||||||
* 部署テーブルから指定された部署IDに一致する部署名を取得する
|
sql.append(" ( ");
|
||||||
*
|
sql.append(" nm_department ");
|
||||||
* @return 部署名
|
sql.append(" ) ");
|
||||||
* @throws SQLException
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
*/
|
|
||||||
public String getNmDepartment(int idDepartment) throws SQLException, ClassNotFoundException {
|
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
|
||||||
String name = "";
|
|
||||||
if(depDao.selectById(idDepartment).size()==1) {
|
|
||||||
DepartmentDTO department = depDao.selectById(idDepartment).get(0);
|
|
||||||
name = department.getNmDepartment();
|
|
||||||
};
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 部署テーブルから指定された部署IDに一致するデータを削除する
|
|
||||||
*
|
|
||||||
* @throws SQLException
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
*/
|
|
||||||
public void depDelete(int idDepartment) throws SQLException, ClassNotFoundException {
|
|
||||||
if (selectById(idDepartment).size()!=0) {
|
|
||||||
StringBuilder sql = new StringBuilder();
|
|
||||||
sql.append(" DELETE FROM ");
|
|
||||||
sql.append(" " + TBL_NAME);
|
|
||||||
sql.append(" WHERE ");
|
|
||||||
sql.append(" " + ID_DEPARTMENT + " = " + "?");
|
|
||||||
System.out.println("指定されたIDの情報を削除しますか? Y/y");
|
|
||||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
String flag = sc.nextLine();
|
|
||||||
if (flag.equals("Y")||flag.equals("y")) {
|
|
||||||
try {
|
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
|
||||||
stmt.setInt(1,idDepartment);
|
|
||||||
stmt.executeUpdate();
|
|
||||||
System.out.println("削除が完了しました");
|
|
||||||
} finally {
|
|
||||||
DbUtil.closeStatement(this.stmt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 部署テーブルに指定された情報を新規登録する
|
|
||||||
*
|
|
||||||
* @throws SQLException
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
*/
|
|
||||||
public void depInsert(String name) throws SQLException, ClassNotFoundException {
|
|
||||||
StringBuilder sql = new StringBuilder();
|
|
||||||
sql.append(" INSERT INTO ");
|
|
||||||
sql.append(" " + TBL_NAME);
|
|
||||||
sql.append(" (" + NM_DEPARTMENT + ") ");
|
|
||||||
sql.append(" VALUES ");
|
sql.append(" VALUES ");
|
||||||
sql.append(" ( ? ) ");
|
sql.append(" (?) ");
|
||||||
System.out.println("指定された情報を登録しますか? Y/y");
|
|
||||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
try {
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
String flag = sc.nextLine();
|
//PreparedStatement生成
|
||||||
if (flag.equals("Y")||flag.equals("y")) {
|
stmt = con.prepareStatement(sql.toString());
|
||||||
try {
|
//値設定
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
stmt.setString(1, departmentName);
|
||||||
stmt.setString(1,name);
|
//SQL実行
|
||||||
stmt.executeUpdate();
|
stmt.executeUpdate();
|
||||||
System.out.println("登録が完了しました");
|
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
|
||||||
}
|
DbUtil.closeStatement(stmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//部署情報更新
|
||||||
* 部署テーブルの指定された部署IDに一致するデータを引数の内容に更新する
|
public void deptUpdate(
|
||||||
*
|
int id,
|
||||||
* @throws SQLException
|
String departmentName)
|
||||||
* @throws ClassNotFoundException
|
throws SQLException {
|
||||||
*/
|
|
||||||
public void depUpdate(int idDepartment, String name) throws SQLException, ClassNotFoundException {
|
//SQL作成
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" UPDATE ");
|
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" UPDATE department ");
|
||||||
sql.append(" SET ");
|
sql.append(" SET ");
|
||||||
sql.append(" " + NM_DEPARTMENT + " = " + "?" + " ");
|
sql.append(" nm_department = ? ");
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE id_department = ? ");
|
||||||
sql.append(" " + ID_DEPARTMENT + " = " + "?");
|
|
||||||
System.out.println("指定されたIDの情報を更新しますか? Y/y");
|
try {
|
||||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
|
||||||
Scanner sc = new Scanner(System.in);
|
//PreparedStatement生成
|
||||||
String flag = sc.nextLine();
|
stmt = con.prepareStatement(sql.toString());
|
||||||
if (flag.equals("Y")||flag.equals("y")) {
|
//値設定
|
||||||
try {
|
stmt.setString(1, departmentName);
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
stmt.setInt(2, id);
|
||||||
stmt.setString(1,name);
|
|
||||||
stmt.setInt(2,idDepartment);
|
//SQL実行
|
||||||
stmt.executeUpdate();
|
stmt.executeUpdate();
|
||||||
System.out.println("更新が完了しました");
|
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
|
||||||
}
|
//Statement切断
|
||||||
|
DbUtil.closeStatement(stmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//部署ID存在確認
|
||||||
|
public boolean existsDepartment(int idDepartment)
|
||||||
|
throws SQLException {
|
||||||
|
|
||||||
|
List<DepartmentDTO> deptList = selectById(idDepartment);
|
||||||
|
|
||||||
|
return deptList.size() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,7 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
import com.rhizome.example.dto.DepartmentDTO;
|
|
||||||
import com.rhizome.example.dto.EmployeeDTO;
|
import com.rhizome.example.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
@@ -26,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;
|
||||||
@@ -55,21 +55,31 @@ public class EmployeeDAO {
|
|||||||
List<EmployeeDTO> 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()) {
|
||||||
EmployeeDTO employee = new EmployeeDTO();
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
employee.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
|
||||||
employee.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
employee.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
employee.setMailaddress(rs.getString(MAIL_ADDRESS));
|
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
employee.setPassword(rs.getString(PASSWORD));
|
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
|
||||||
employee.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
dto.setPassword(rs.getString(PASSWORD));
|
||||||
rtnList.add(employee);
|
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);
|
||||||
@@ -77,353 +87,370 @@ public class EmployeeDAO {
|
|||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//社員ID検索
|
||||||
* 社員テーブルから指定された社員IDに一致するデータを全件取得する
|
public List<EmployeeDTO> selectById(int id) throws SQLException {
|
||||||
*
|
//戻り値用リスト
|
||||||
* @return 検索結果
|
|
||||||
* @throws SQLException
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
*/
|
|
||||||
public List<EmployeeDTO> selectById(int idEmployee) throws SQLException, ClassNotFoundException {
|
|
||||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
//SQL作成
|
||||||
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(" WHERE ");
|
sql.append(" ,employee.mail_address ");
|
||||||
sql.append(" " + ID_EMPLOYEE + " = " + "?");
|
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 {
|
try {
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
//PreparedStatement生成
|
||||||
stmt.setInt(1,idEmployee);
|
stmt = con.prepareStatement(sql.toString());
|
||||||
|
//社員IDを設定
|
||||||
|
stmt.setInt(1, id);
|
||||||
|
//SQL実行
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
int nRecords = 0;
|
//検索結果をDTOへ格納
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
EmployeeDTO employee = new EmployeeDTO();
|
//DTO生成
|
||||||
employee.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
employee.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
//検索結果をDTOへセット
|
||||||
employee.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
employee.setMailaddress(rs.getString(MAIL_ADDRESS));
|
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
employee.setPassword(rs.getString(PASSWORD));
|
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
employee.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
|
||||||
rtnList.add(employee);
|
dto.setPassword(rs.getString(PASSWORD));
|
||||||
nRecords++;
|
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||||
}
|
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||||
if (nRecords==0) {
|
|
||||||
System.out.println("入力された社員IDで情報がみつかりませんでした");
|
//リストへ追加
|
||||||
|
rtnList.add(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
|
||||||
|
DbUtil.closeStatement(stmt);
|
||||||
}
|
}
|
||||||
|
//検索結果を返却
|
||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// 社員名検索
|
||||||
* 社員テーブルから条件に一致するデータを全件取得する(AND検索)
|
public List<EmployeeDTO> selectByName(String name) throws SQLException {
|
||||||
*
|
//戻り値用リスト
|
||||||
* @return 検索結果
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
* @throws SQLException
|
//SQL作成
|
||||||
* @throws ClassNotFoundException
|
StringBuilder sql = new StringBuilder();
|
||||||
*/
|
|
||||||
/**/
|
sql.append(" SELECT ");
|
||||||
// 部署ID入力
|
sql.append(" employee.id_employee ");
|
||||||
public List<EmployeeDTO> empSelect(int idEmployee, String name, String kn, String mailaddress, String password, int idDepartment) throws SQLException, ClassNotFoundException {
|
sql.append(" ,employee.nm_employee ");
|
||||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
sql.append(" ,employee.kn_employee ");
|
||||||
StringBuilder sql = new StringBuilder();
|
sql.append(" ,employee.mail_address ");
|
||||||
sql.append(" SELECT ");
|
sql.append(" ,employee.password ");
|
||||||
sql.append(" " + "*");
|
sql.append(" ,employee.id_department ");
|
||||||
sql.append(" FROM ");
|
sql.append(" ,department.nm_department ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" FROM employee ");
|
||||||
sql.append(" WHERE ");
|
sql.append(" INNER JOIN department ");
|
||||||
if (idEmployee!=0) sql.append(" " + ID_EMPLOYEE + " = " + "?" + "AND");
|
sql.append(" ON employee.id_department = department.id_department ");
|
||||||
if (name.trim().length()!=0) sql.append(" " + NM_EMPLOYEE + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
sql.append(" WHERE employee.nm_employee = ? ");
|
||||||
if (kn.trim().length()!=0) sql.append(" " + KN_EMPLOYEE + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
|
||||||
if (mailaddress.trim().length()!=0) sql.append(" " + MAIL_ADDRESS + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
try {
|
||||||
if (password.trim().length()!=0) sql.append(" " + PASSWORD + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
|
||||||
if (idDepartment!=0) sql.append(" " + ID_DEPARTMENT + " = " + "?" + "AND");
|
//PreparedStatement生成
|
||||||
sql.delete(sql.length()-3, sql.length()); // 末尾"AND"を削除する
|
stmt = con.prepareStatement(sql.toString());
|
||||||
try {
|
|
||||||
int nConditions = 0;
|
//社員名を設定
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
stmt.setString(1, name);
|
||||||
if (idEmployee!=0) stmt.setInt(++nConditions, idEmployee);
|
|
||||||
if (name.trim().length()!=0) stmt.setString(++nConditions, name);
|
//SQL実行
|
||||||
if (kn.trim().length()!=0) stmt.setString(++nConditions, kn);
|
rs = stmt.executeQuery();
|
||||||
if (mailaddress.trim().length()!=0) stmt.setString(++nConditions, mailaddress);
|
|
||||||
if (password.trim().length()!=0) stmt.setString(++nConditions, password);
|
//検索結果をDTOへ格納
|
||||||
if (idDepartment!=0) stmt.setInt(++nConditions, idDepartment);
|
while (rs.next()) {
|
||||||
if (nConditions==0) {
|
|
||||||
System.out.println("情報が入力されていません");
|
//DTO生成
|
||||||
} else {
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
rs = stmt.executeQuery();
|
|
||||||
int nRecords = 0;
|
//検索結果をDTOへセット
|
||||||
while (rs.next()) {
|
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
EmployeeDTO employee = new EmployeeDTO();
|
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
employee.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
employee.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
|
||||||
employee.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
dto.setPassword(rs.getString(PASSWORD));
|
||||||
employee.setMailaddress(rs.getString(MAIL_ADDRESS));
|
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||||
employee.setPassword(rs.getString(PASSWORD));
|
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||||
employee.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
|
||||||
rtnList.add(employee);
|
//リストへ追加
|
||||||
nRecords++;
|
rtnList.add(dto);
|
||||||
}
|
}
|
||||||
if (nRecords==0) {
|
|
||||||
System.out.println("入力された社員情報で情報がみつかりませんでした");
|
} finally {
|
||||||
}
|
|
||||||
}
|
DbUtil.closeStatement(stmt);
|
||||||
} finally {
|
}
|
||||||
DbUtil.closeStatement(this.stmt);
|
|
||||||
}
|
//検索結果を返却
|
||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
|
||||||
// 部署名入力
|
|
||||||
public List<EmployeeDTO> empSelect(int idEmployee, String name, String kn, String mailaddress, String password, String nmDepartment) throws SQLException, ClassNotFoundException {
|
|
||||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
|
||||||
StringBuilder sql = new StringBuilder();
|
|
||||||
sql.append(" SELECT ");
|
|
||||||
sql.append(" " + "*");
|
|
||||||
sql.append(" FROM ");
|
|
||||||
sql.append(" " + TBL_NAME);
|
|
||||||
if (nmDepartment.trim().length()!=0) { // テーブル結合
|
|
||||||
sql.append(" INNER JOIN " + DepartmentDAO.getTblName() + " ON ");
|
|
||||||
sql.append(TBL_NAME+"."+ID_DEPARTMENT + " = " + DepartmentDAO.getTblName()+"."+DepartmentDAO.getIdColumnName());
|
|
||||||
}
|
|
||||||
sql.append(" WHERE ");
|
|
||||||
if (idEmployee!=0) sql.append(" " + ID_EMPLOYEE + " = " + "?" + "AND");
|
|
||||||
if (name.trim().length()!=0) sql.append(" " + NM_EMPLOYEE + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
|
||||||
if (kn.trim().length()!=0) sql.append(" "+KN_EMPLOYEE + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
|
||||||
if (mailaddress.trim().length()!=0) sql.append(" " + MAIL_ADDRESS + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
|
||||||
if (password.trim().length()!=0) sql.append(" " + PASSWORD + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
|
||||||
if (nmDepartment.trim().length()!=0) sql.append(" " + DepartmentDAO.getNmColumnName() + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
|
||||||
sql.delete(sql.length()-3, sql.length()); // 末尾"AND"を削除する
|
|
||||||
try {
|
|
||||||
int nConditions = 0;
|
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
|
||||||
if (idEmployee!=0) stmt.setInt(++nConditions, idEmployee);
|
|
||||||
if (name.trim().length()!=0) stmt.setString(++nConditions, name);
|
|
||||||
if (kn.trim().length()!=0) stmt.setString(++nConditions, kn);
|
|
||||||
if (mailaddress.trim().length()!=0) stmt.setString(++nConditions, mailaddress);
|
|
||||||
if (password.trim().length()!=0) stmt.setString(++nConditions, password);
|
|
||||||
if (nmDepartment.trim().length()!=0) stmt.setString(++nConditions, nmDepartment);
|
|
||||||
if (nConditions==0) {
|
|
||||||
System.out.println("情報が入力されていません");
|
|
||||||
} else {
|
|
||||||
rs = stmt.executeQuery();
|
|
||||||
int nRecords = 0;
|
|
||||||
while (rs.next()) {
|
|
||||||
EmployeeDTO employee = new EmployeeDTO();
|
|
||||||
employee.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
|
||||||
employee.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
|
||||||
employee.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
|
||||||
employee.setMailaddress(rs.getString(MAIL_ADDRESS));
|
|
||||||
employee.setPassword(rs.getString(PASSWORD));
|
|
||||||
employee.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
|
||||||
rtnList.add(employee);
|
|
||||||
nRecords++;
|
|
||||||
}
|
|
||||||
if (nRecords==0) {
|
|
||||||
System.out.println("入力された社員情報で情報がみつかりませんでした");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
DbUtil.closeStatement(this.stmt);
|
|
||||||
}
|
|
||||||
return rtnList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//フリガナ検索
|
||||||
* 社員テーブルから指定された社員IDに一致するデータを削除する
|
public List<EmployeeDTO> selectByKana(String kana) throws SQLException {
|
||||||
*
|
//戻り値用リスト
|
||||||
* @throws SQLException
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
* @throws ClassNotFoundException
|
//SQL作成
|
||||||
*/
|
StringBuilder sql = new StringBuilder();
|
||||||
public void empDelete(int idEmployee) throws SQLException, ClassNotFoundException {
|
|
||||||
if (selectById(idEmployee).size()!=0) {
|
sql.append(" SELECT ");
|
||||||
StringBuilder sql = new StringBuilder();
|
sql.append(" employee.id_employee ");
|
||||||
sql.append(" DELETE FROM ");
|
sql.append(" ,employee.nm_employee ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" ,employee.kn_employee ");
|
||||||
sql.append(" WHERE ");
|
sql.append(" ,employee.mail_address ");
|
||||||
sql.append(" " + ID_EMPLOYEE + " = " + "?");
|
sql.append(" ,employee.password ");
|
||||||
System.out.println("指定されたIDの情報を削除しますか? Y/y");
|
sql.append(" ,employee.id_department ");
|
||||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
sql.append(" ,department.nm_department ");
|
||||||
Scanner sc = new Scanner(System.in);
|
sql.append(" FROM employee ");
|
||||||
String flag = sc.nextLine();
|
sql.append(" INNER JOIN department ");
|
||||||
if (flag.equals("Y")||flag.equals("y")) {
|
sql.append(" ON employee.id_department = department.id_department ");
|
||||||
try {
|
sql.append(" WHERE employee.kn_employee = ? ");
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
|
||||||
stmt.setInt(1,idEmployee);
|
try {
|
||||||
stmt.executeUpdate();
|
//PreparedStatement生成
|
||||||
System.out.println("削除が完了しました");
|
stmt = con.prepareStatement(sql.toString());
|
||||||
} finally {
|
//フリガナ設定
|
||||||
DbUtil.closeStatement(this.stmt);
|
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<>();
|
||||||
* @throws SQLException
|
StringBuilder sql = new StringBuilder();
|
||||||
* @throws ClassNotFoundException
|
|
||||||
*/
|
sql.append(" SELECT ");
|
||||||
// 部署ID入力
|
sql.append(" employee.id_employee ");
|
||||||
public void empInsert(String name, String kn, String mailaddress, String password, int idDepartment) throws SQLException, ClassNotFoundException {
|
sql.append(" ,employee.nm_employee ");
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
sql.append(" ,employee.kn_employee ");
|
||||||
if (depDao.selectById(idDepartment).size()!=0) { // 存在しない部署IDが入力されたときは登録処理を行わない
|
sql.append(" ,employee.mail_address ");
|
||||||
StringBuilder sql = new StringBuilder();
|
sql.append(" ,employee.password ");
|
||||||
sql.append(" INSERT INTO ");
|
sql.append(" ,employee.id_department ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" ,department.nm_department ");
|
||||||
sql.append(" (" + NM_EMPLOYEE + "," + KN_EMPLOYEE + "," + MAIL_ADDRESS + "," + PASSWORD + "," + ID_DEPARTMENT + ") ");
|
sql.append(" FROM employee ");
|
||||||
sql.append(" VALUES ");
|
sql.append(" INNER JOIN department ");
|
||||||
sql.append(" (?, ?, ?, ?, ?) ");
|
sql.append(" ON employee.id_department = department.id_department ");
|
||||||
System.out.println("指定された情報を登録しますか? Y/y");
|
sql.append(" WHERE employee.mail_address = ? ");
|
||||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
|
||||||
Scanner sc = new Scanner(System.in);
|
try {
|
||||||
String flag = sc.nextLine();
|
|
||||||
if (flag.equals("Y")||flag.equals("y")) {
|
stmt = con.prepareStatement(sql.toString());
|
||||||
try {
|
stmt.setString(1, mail);
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
rs = stmt.executeQuery();
|
||||||
stmt.setString(1,name);
|
|
||||||
stmt.setString(2,kn);
|
while (rs.next()) {
|
||||||
stmt.setString(3,mailaddress);
|
|
||||||
stmt.setString(4,password);
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
stmt.setInt(5,idDepartment);
|
|
||||||
stmt.executeUpdate();
|
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
System.out.println("登録が完了しました");
|
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
} finally {
|
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
DbUtil.closeStatement(this.stmt);
|
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);
|
||||||
public void empInsert(String name, String kn, String mailaddress, String password, String nmDepartment) throws SQLException, ClassNotFoundException {
|
}
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
|
||||||
List<DepartmentDTO> rtnList = depDao.selectByNm(nmDepartment);
|
} finally {
|
||||||
if (rtnList.size()==1) { // 部署名が特定できないときは登録処理を行わない
|
|
||||||
DepartmentDTO department = rtnList.get(0);
|
DbUtil.closeStatement(stmt);
|
||||||
StringBuilder sql = new StringBuilder();
|
}
|
||||||
sql.append(" INSERT INTO ");
|
|
||||||
sql.append(" " + TBL_NAME);
|
return rtnList;
|
||||||
sql.append(" (" + NM_EMPLOYEE + "," + KN_EMPLOYEE + "," + MAIL_ADDRESS + "," + PASSWORD + "," + ID_DEPARTMENT + ") ");
|
|
||||||
sql.append(" VALUES ");
|
|
||||||
sql.append(" (?, ?, ?, ?, ?) ");
|
|
||||||
System.out.println("指定された情報を登録しますか? Y/y");
|
|
||||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
String flag = sc.nextLine();
|
|
||||||
if (flag.equals("Y")||flag.equals("y")) {
|
|
||||||
try {
|
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
|
||||||
stmt.setString(1,name);
|
|
||||||
stmt.setString(2,kn);
|
|
||||||
stmt.setString(3,mailaddress);
|
|
||||||
stmt.setString(4,password);
|
|
||||||
stmt.setInt(5,department.getIdDepartment());
|
|
||||||
stmt.executeUpdate();
|
|
||||||
System.out.println("登録が完了しました");
|
|
||||||
} finally {
|
|
||||||
DbUtil.closeStatement(this.stmt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (rtnList.size()>1) {
|
|
||||||
System.out.println("入力された情報で部署を1つに特定できません");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//部署名検索
|
||||||
* 社員テーブルの指定された社員IDに一致するデータを引数の内容に更新する
|
|
||||||
*
|
public List<EmployeeDTO> selectByDepartment(String department) throws SQLException {
|
||||||
* @throws SQLException
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
* @throws ClassNotFoundException
|
StringBuilder sql = new StringBuilder();
|
||||||
*/
|
|
||||||
// 部署ID入力
|
sql.append(" SELECT ");
|
||||||
public void empUpdate(int idEmployee, String name, String kn, String mailaddress, String password, int idDepartment) throws SQLException, ClassNotFoundException {
|
sql.append(" employee.id_employee ");
|
||||||
EmployeeDTO employee = selectById(idEmployee).get(0);
|
sql.append(" ,employee.nm_employee ");
|
||||||
if (name.trim().length()==0) name = employee.getNmEmployee();
|
sql.append(" ,employee.kn_employee ");
|
||||||
if (kn.trim().length()==0) kn = employee.getKnEmployee();
|
sql.append(" ,employee.mail_address ");
|
||||||
if (mailaddress.trim().length()==0) mailaddress = employee.getMailaddress();
|
sql.append(" ,employee.password ");
|
||||||
if (password.trim().length()==0) password = employee.getPassword();
|
sql.append(" ,employee.id_department ");
|
||||||
if (idDepartment==0) idDepartment = employee.getIdDepartment();
|
sql.append(" ,department.nm_department ");
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
sql.append(" FROM employee ");
|
||||||
if (depDao.selectById(idDepartment).size()!=0) { // 存在しない部署IDが入力されたときは更新処理を行わない
|
sql.append(" INNER JOIN department ");
|
||||||
StringBuilder sql = new StringBuilder();
|
sql.append(" ON employee.id_department = department.id_department ");
|
||||||
sql.append(" UPDATE ");
|
sql.append(" WHERE department.nm_department = ? ");
|
||||||
sql.append(" " + TBL_NAME);
|
|
||||||
sql.append(" SET ");
|
try {
|
||||||
sql.append(" " + NM_EMPLOYEE + " = " + "?" + ",");
|
|
||||||
sql.append(" " + KN_EMPLOYEE + " = " + "?" +",");
|
stmt = con.prepareStatement(sql.toString());
|
||||||
sql.append(" " + MAIL_ADDRESS + " = " + "?" + ",");
|
stmt.setString(1, department);
|
||||||
sql.append(" " + PASSWORD + " = " + "?" + ",");
|
rs = stmt.executeQuery();
|
||||||
sql.append(" " + ID_DEPARTMENT + " = " + "?");
|
|
||||||
sql.append(" WHERE ");
|
while (rs.next()) {
|
||||||
sql.append(" " + ID_EMPLOYEE + " = " + "?");
|
|
||||||
System.out.println("指定されたIDの情報を更新しますか? Y/y");
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
|
||||||
Scanner sc = new Scanner(System.in);
|
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
String flag = sc.nextLine();
|
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
if (flag.equals("Y")||flag.equals("y")) {
|
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
try {
|
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
dto.setPassword(rs.getString(PASSWORD));
|
||||||
stmt.setString(1,name);
|
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||||
stmt.setString(2,kn);
|
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||||
stmt.setString(3,mailaddress);
|
|
||||||
stmt.setString(4,password);
|
rtnList.add(dto);
|
||||||
stmt.setInt(5,idDepartment);
|
}
|
||||||
stmt.setInt(6,idEmployee);
|
|
||||||
stmt.executeUpdate();
|
} finally {
|
||||||
System.out.println("更新が完了しました");
|
|
||||||
} finally {
|
DbUtil.closeStatement(stmt);
|
||||||
DbUtil.closeStatement(this.stmt);
|
}
|
||||||
}
|
|
||||||
}
|
return rtnList;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// 部署名入力
|
|
||||||
public void empUpdate(int idEmployee, String name, String kn, String mailaddress, String password, String NmDepartment) throws SQLException, ClassNotFoundException {
|
//社員情報削除
|
||||||
EmployeeDTO employee = selectById(idEmployee).get(0);
|
public void empDelete(int id) throws SQLException {
|
||||||
if (name.trim().length()==0) name = employee.getNmEmployee();
|
//SQL作成
|
||||||
if (kn.trim().length()==0) kn = employee.getKnEmployee();
|
StringBuilder sql = new StringBuilder();
|
||||||
if (mailaddress.trim().length()==0) mailaddress = employee.getMailaddress();
|
sql.append(" DELETE ");
|
||||||
if (password.trim().length()==0) password = employee.getPassword();
|
sql.append(" FROM ");
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
sql.append(" " + TBL_NAME);
|
||||||
List<DepartmentDTO> rtnList = depDao.selectByNm(NmDepartment);
|
sql.append(" WHERE ");
|
||||||
if (rtnList.size()==1) { // 部署名が特定できないときは登録処理を行わない
|
sql.append(" " + ID_EMPLOYEE + " = ?");
|
||||||
DepartmentDTO department = rtnList.get(0);
|
|
||||||
StringBuilder sql = new StringBuilder();
|
try {
|
||||||
sql.append(" UPDATE ");
|
//PreparedStatement生成
|
||||||
sql.append(" " + TBL_NAME);
|
stmt = con.prepareStatement(sql.toString());
|
||||||
sql.append(" SET ");
|
//値設定
|
||||||
sql.append(" " + NM_EMPLOYEE + " = " + "?" + ",");
|
stmt.setInt(1, id);
|
||||||
sql.append(" " + KN_EMPLOYEE + " = " + "?" + ",");
|
//SQL実行
|
||||||
sql.append(" " + MAIL_ADDRESS + " = " + "?" + ",");
|
stmt.executeUpdate();
|
||||||
sql.append(" " + PASSWORD + " = " + "?" + ",");
|
|
||||||
sql.append(" " + ID_DEPARTMENT + " = " + "?");
|
} finally {
|
||||||
sql.append(" WHERE ");
|
|
||||||
sql.append(" " + ID_EMPLOYEE + " = " + "?");
|
DbUtil.closeStatement(stmt);
|
||||||
System.out.println("指定されたIDの情報を更新しますか? Y/y");
|
}
|
||||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
}
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
String flag = sc.nextLine();
|
//社員情報登録
|
||||||
if (flag.equals("Y")||flag.equals("y")) {
|
public void empInsert(
|
||||||
try {
|
String name,
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
String kana,
|
||||||
stmt.setString(1,name);
|
String mail,
|
||||||
stmt.setString(2,kn);
|
String password,
|
||||||
stmt.setString(3,mailaddress);
|
int departmentId)
|
||||||
stmt.setString(4,password);
|
throws SQLException {
|
||||||
stmt.setInt(5,department.getIdDepartment());
|
//SQL作成
|
||||||
stmt.setInt(6,idEmployee);
|
StringBuilder sql = new StringBuilder();
|
||||||
stmt.executeUpdate();
|
|
||||||
System.out.println("更新が完了しました");
|
sql.append(" INSERT INTO employee ");
|
||||||
} finally {
|
sql.append(" ( ");
|
||||||
DbUtil.closeStatement(this.stmt);
|
sql.append(" nm_employee, ");
|
||||||
}
|
sql.append(" kn_employee, ");
|
||||||
}
|
sql.append(" mail_address, ");
|
||||||
} else if (rtnList.size()>1) {
|
sql.append(" password, ");
|
||||||
System.out.println("入力された情報で部署を1つに特定できません");
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,26 +2,22 @@ package com.rhizome.example.dto;
|
|||||||
|
|
||||||
public class DepartmentDTO {
|
public class DepartmentDTO {
|
||||||
|
|
||||||
private int idDepartment;
|
private Integer idDepartment;
|
||||||
private String nmDepartment;
|
private String nmDepartment;
|
||||||
|
|
||||||
public int getIdDepartment() {
|
public Integer getIdDepartment() {
|
||||||
return idDepartment;
|
return idDepartment;
|
||||||
}
|
}
|
||||||
public void setIdDepartment(int idDepartment) {
|
|
||||||
|
public void setIdDepartment(Integer idDepartment) {
|
||||||
this.idDepartment = idDepartment;
|
this.idDepartment = idDepartment;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getNmDepartment() {
|
public String getNmDepartment() {
|
||||||
return nmDepartment;
|
return nmDepartment;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNmDepartment(String nmDepartment) {
|
public void setNmDepartment(String nmDepartment) {
|
||||||
this.nmDepartment = nmDepartment;
|
this.nmDepartment = nmDepartment;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return this.getIdDepartment() + " "
|
|
||||||
+this.getNmDepartment();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,17 +2,19 @@ package com.rhizome.example.dto;
|
|||||||
|
|
||||||
public class EmployeeDTO {
|
public class EmployeeDTO {
|
||||||
|
|
||||||
private int idEmployee;
|
private Integer idEmployee;
|
||||||
private String nmEmployee;
|
private String nmEmployee;
|
||||||
private String knEmployee;
|
private String knEmployee;
|
||||||
private String mailaddress;
|
private String mailAddress;
|
||||||
private String password;
|
private String password;
|
||||||
private int idDepartment;
|
private Integer idDepartment;
|
||||||
|
private String nmDepartment;
|
||||||
|
|
||||||
public int getIdEmployee() {
|
|
||||||
|
public Integer getIdEmployee() {
|
||||||
return idEmployee;
|
return idEmployee;
|
||||||
}
|
}
|
||||||
public void setIdEmployee(int idEmployee) {
|
public void setIdEmployee(Integer idEmployee) {
|
||||||
this.idEmployee = idEmployee;
|
this.idEmployee = idEmployee;
|
||||||
}
|
}
|
||||||
public String getNmEmployee() {
|
public String getNmEmployee() {
|
||||||
@@ -27,11 +29,11 @@ public class EmployeeDTO {
|
|||||||
public void setKnEmployee(String knEmployee) {
|
public void setKnEmployee(String knEmployee) {
|
||||||
this.knEmployee = knEmployee;
|
this.knEmployee = knEmployee;
|
||||||
}
|
}
|
||||||
public String getMailaddress() {
|
public String getMailAddress() {
|
||||||
return mailaddress;
|
return mailAddress;
|
||||||
}
|
}
|
||||||
public void setMailaddress(String mailaddress) {
|
public void setMailAddress(String mailAddress) {
|
||||||
this.mailaddress = mailaddress;
|
this.mailAddress = mailAddress;
|
||||||
}
|
}
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
return password;
|
return password;
|
||||||
@@ -39,21 +41,19 @@ public class EmployeeDTO {
|
|||||||
public void setPassword(String password) {
|
public void setPassword(String password) {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
public int getIdDepartment() {
|
public Integer getIdDepartment() {
|
||||||
return idDepartment;
|
return idDepartment;
|
||||||
}
|
}
|
||||||
public void setIdDepartment(int idDepartment) {
|
public void setIdDepartment(Integer idDepartment) {
|
||||||
this.idDepartment = idDepartment;
|
this.idDepartment = idDepartment;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public String getNmDepartment() {
|
||||||
public String toString() {
|
return nmDepartment;
|
||||||
return this.getIdEmployee() + " "
|
}
|
||||||
+this.getNmEmployee() + " "
|
|
||||||
+this.getKnEmployee() + " "
|
public void setNmDepartment(String nmDepartment) {
|
||||||
+this.getMailaddress() + " "
|
this.nmDepartment = nmDepartment;
|
||||||
+this.getPassword() + " "
|
|
||||||
+this.getIdDepartment();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,9 @@ 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.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
@@ -49,35 +51,53 @@ public class Main {
|
|||||||
System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg));
|
System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg));
|
||||||
System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了");
|
System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了");
|
||||||
String ope = sc.nextLine();
|
String ope = sc.nextLine();
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
|
||||||
switch (ope) {
|
switch (ope) {
|
||||||
case "1":
|
case "1":
|
||||||
empSelect();
|
//社員テーブル
|
||||||
|
if (tblFlg == 0) {
|
||||||
|
empSelect();
|
||||||
|
|
||||||
|
//部署テーブル
|
||||||
|
} else {
|
||||||
|
deptSelect();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
empDao.empInsert(inputCheck("社員名", false),
|
//社員テーブル
|
||||||
inputCheck("フリガナ", false),
|
if (tblFlg == 0) {
|
||||||
inputCheck("メールアドレス", false),
|
empInsert();
|
||||||
inputCheck("パスワード", false),
|
|
||||||
Integer.parseInt(inputCheck("部署ID", true)));
|
//部署テーブル
|
||||||
|
} else {
|
||||||
|
deptInsert();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "3":
|
case "3":
|
||||||
int id = Integer.parseInt(inputCheck("ID", true));
|
//社員テーブル
|
||||||
if (empDao.selectById(id).size()==0) break; // 社員IDの照合に失敗した場合は処理を抜ける
|
if (tblFlg == 0) {
|
||||||
else {
|
empUpdate();
|
||||||
empDao.empUpdate(id,
|
|
||||||
inputCheck("社員名", false),
|
//部署テーブル
|
||||||
inputCheck("フリガナ", false),
|
} else {
|
||||||
inputCheck("メールアドレス", false),
|
deptUpdate();
|
||||||
inputCheck("パスワード", false),
|
|
||||||
Integer.parseInt(inputCheck("部署ID", true)));
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
case "4":
|
case "4":
|
||||||
empDao.empDelete(Integer.parseInt(inputCheck("ID", true)));
|
//社員テーブル
|
||||||
|
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("システムを終了します");
|
||||||
@@ -105,19 +125,137 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "2":
|
case "2":
|
||||||
empList = empDao.selectById(Integer.parseInt(inputCheck("ID", true)));
|
//検索項目選択
|
||||||
break;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
for (EmployeeDTO emp : empList) {
|
||||||
System.out.println(emp);
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* フラグを判定し、テーブル名を取得する
|
* フラグを判定し、テーブル名を取得する
|
||||||
@@ -168,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,413 +0,0 @@
|
|||||||
package com.rhizome.example.main;
|
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
import com.rhizome.example.dao.DepartmentDAO;
|
|
||||||
import com.rhizome.example.dao.EmployeeDAO;
|
|
||||||
import com.rhizome.example.dto.DepartmentDTO;
|
|
||||||
import com.rhizome.example.dto.EmployeeDTO;
|
|
||||||
import com.rhizome.example.util.DbUtil;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author IT-College
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class MainNew {
|
|
||||||
static Scanner sc = new Scanner(System.in);
|
|
||||||
/**
|
|
||||||
* テーブル切り替え用変数 0:Employeeテーブル 1:Departmentテーブル
|
|
||||||
*/
|
|
||||||
static int tblFlg = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 処理開始位置
|
|
||||||
*
|
|
||||||
* @param args
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
System.out.println("システムを開始します");
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
new MainNew().start();
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("予期せぬエラーが発生しました");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 処理選択を行う
|
|
||||||
*
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
*/
|
|
||||||
public void start() throws ClassNotFoundException, SQLException {
|
|
||||||
System.out.println();
|
|
||||||
System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg));
|
|
||||||
System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了");
|
|
||||||
String ope = sc.nextLine();
|
|
||||||
switch (ope) {
|
|
||||||
case "1":
|
|
||||||
Select(tblFlg);
|
|
||||||
break;
|
|
||||||
case "2":
|
|
||||||
Insert(tblFlg);
|
|
||||||
break;
|
|
||||||
case "3":
|
|
||||||
Update(tblFlg);
|
|
||||||
break;
|
|
||||||
case "4":
|
|
||||||
Delete(tblFlg);
|
|
||||||
break;
|
|
||||||
case "9":
|
|
||||||
tblFlg = ChangeTable(tblFlg);
|
|
||||||
break;
|
|
||||||
case "0":
|
|
||||||
System.out.println("システムを終了します");
|
|
||||||
System.exit(0);
|
|
||||||
default:
|
|
||||||
System.out.println("指定の番号を入力してください");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 社員テーブルから情報を検索する
|
|
||||||
*
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
*/
|
|
||||||
public static void empSelect() throws ClassNotFoundException, SQLException {
|
|
||||||
List<EmployeeDTO> empList = new ArrayList<>();
|
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
|
||||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
|
||||||
String select = sc.nextLine();
|
|
||||||
try {
|
|
||||||
switch (select) {
|
|
||||||
case "1":
|
|
||||||
empList = empDao.selectAll();
|
|
||||||
break;
|
|
||||||
case "2":
|
|
||||||
String infoEmployee = inputCheck("社員ID", false, "AllowEmpty");
|
|
||||||
int idEmployee = 0; // 未入力時は0を渡す
|
|
||||||
try {
|
|
||||||
idEmployee = Integer.parseInt(infoEmployee);
|
|
||||||
} catch (NumberFormatException err) {}
|
|
||||||
String name = inputCheck("社員名", false, "AllowEmpty");
|
|
||||||
String kn = inputCheck("フリガナ", false, "AllowEmpty");
|
|
||||||
String mailaddress = inputCheck("メールアドレス", false, "AllowEmpty");
|
|
||||||
String password = inputCheck("パスワード", false, "AllowEmpty");
|
|
||||||
String infoDepartment = (inputCheck("部署情報", false, "AllowEmpty"));
|
|
||||||
try {
|
|
||||||
int idDepartment = Integer.parseInt(infoDepartment);
|
|
||||||
empList = empDao.empSelect(idEmployee, name, kn, mailaddress, password, idDepartment);
|
|
||||||
} catch (NumberFormatException err) {
|
|
||||||
if (infoDepartment.trim().length()==0) {
|
|
||||||
empList = empDao.empSelect(idEmployee, name, kn, mailaddress, password, 0); // 未入力時は0を渡す
|
|
||||||
} else {
|
|
||||||
empList = empDao.empSelect(idEmployee, name, kn, mailaddress, password, infoDepartment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (EmployeeDTO emp : empList) {
|
|
||||||
//System.out.println(emp);
|
|
||||||
System.out.println(toStringEmployee(emp));
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
DbUtil.closeConnection(empDao.con);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 部署テーブルから情報を検索する
|
|
||||||
*
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
*/
|
|
||||||
public static void depSelect() throws ClassNotFoundException, SQLException {
|
|
||||||
List<DepartmentDTO> depList = new ArrayList<>();
|
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
|
||||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
|
||||||
String select = sc.nextLine();
|
|
||||||
try {
|
|
||||||
switch (select) {
|
|
||||||
case "1":
|
|
||||||
depList = depDao.selectAll();
|
|
||||||
break;
|
|
||||||
case "2":
|
|
||||||
depList = depDao.selectById(Integer.parseInt(inputCheck("部署ID", true)));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (DepartmentDTO dep : depList) {
|
|
||||||
//System.out.println(dep);
|
|
||||||
System.out.println(toStringDepartment(dep));
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
DbUtil.closeConnection(depDao.con);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* フラグを判定し、テーブル名を取得する
|
|
||||||
*
|
|
||||||
* @param tblFlg
|
|
||||||
* テーブル名判定用フラグ
|
|
||||||
* @return テーブル名
|
|
||||||
*/
|
|
||||||
public static String targetTableName(int tblFlg) {
|
|
||||||
String rtnTblName = "";
|
|
||||||
if (tblFlg == 0) {
|
|
||||||
rtnTblName = "社員テーブル";
|
|
||||||
} else {
|
|
||||||
rtnTblName = "部署テーブル";
|
|
||||||
}
|
|
||||||
return rtnTblName;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* フラグを判定し、検索機能を呼び出す
|
|
||||||
*
|
|
||||||
* @param tblFlg
|
|
||||||
* テーブル名判定用フラグ
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
*/
|
|
||||||
public static void Select(int tblFlg) throws ClassNotFoundException, SQLException {
|
|
||||||
if (tblFlg == 0) {
|
|
||||||
empSelect();
|
|
||||||
} else {
|
|
||||||
depSelect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* フラグを判定し、登録機能を呼び出す
|
|
||||||
*
|
|
||||||
* @param tblFlg
|
|
||||||
* テーブル名判定用フラグ
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
*/
|
|
||||||
public static void Insert(int tblFlg) throws ClassNotFoundException, SQLException {
|
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
if (tblFlg == 0) {
|
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
|
||||||
String name = inputCheck("社員名", false);
|
|
||||||
String kn = inputCheck("フリガナ", false);
|
|
||||||
String mailaddress = inputCheck("メールアドレス", false);
|
|
||||||
String password = inputCheck("パスワード", false);
|
|
||||||
String infoDepartment = inputCheck("部署情報", false);
|
|
||||||
try {
|
|
||||||
int idDepartment = Integer.parseInt(infoDepartment);
|
|
||||||
empDao.empInsert(name, kn, mailaddress, password, idDepartment);
|
|
||||||
} catch (NumberFormatException err) {
|
|
||||||
empDao.empInsert(name, kn, mailaddress, password, infoDepartment);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
|
||||||
depDao.depInsert(inputCheck("部署名", false));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* フラグを判定し、更新機能を呼び出す
|
|
||||||
*
|
|
||||||
* @param tblFlg
|
|
||||||
* テーブル名判定用フラグ
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
*/
|
|
||||||
public static void Update(int tblFlg) throws ClassNotFoundException, SQLException {
|
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
if (tblFlg == 0) {
|
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
|
||||||
int idEmployee = Integer.parseInt(inputCheck("社員ID", true));
|
|
||||||
if (empDao.selectById(idEmployee).size()==0) {} // 社員IDの照合に失敗した場合は更新処理を行わない
|
|
||||||
else {
|
|
||||||
String name = inputCheck("社員名", false, "AllowEmpty");
|
|
||||||
String kn = inputCheck("フリガナ", false, "AllowEmpty");
|
|
||||||
String mailaddress = inputCheck("メールアドレス", false, "AllowEmpty");
|
|
||||||
String password = inputCheck("パスワード", false, "AllowEmpty");
|
|
||||||
String infoDepartment = (inputCheck("部署情報", false, "AllowEmpty"));
|
|
||||||
try {
|
|
||||||
int idDepartment = Integer.parseInt(infoDepartment);
|
|
||||||
empDao.empUpdate(idEmployee, name, kn, mailaddress, password, idDepartment);
|
|
||||||
} catch (NumberFormatException err) {
|
|
||||||
if (infoDepartment.trim().length()==0) {
|
|
||||||
empDao.empUpdate(idEmployee, name, kn, mailaddress, password, 0); // 未入力時は0を渡す
|
|
||||||
} else {
|
|
||||||
empDao.empUpdate(idEmployee, name, kn, mailaddress, password, infoDepartment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
|
||||||
int id = Integer.parseInt(inputCheck("部署ID", true));
|
|
||||||
if (depDao.selectById(id).size()==0) {} // 部署IDの照合に失敗した場合は更新処理を行わない
|
|
||||||
else {
|
|
||||||
depDao.depUpdate(id,
|
|
||||||
inputCheck("部署名", false));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* フラグを判定し、削除機能を呼び出す
|
|
||||||
*
|
|
||||||
* @param tblFlg
|
|
||||||
* テーブル名判定用フラグ
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
*/
|
|
||||||
public static void Delete(int tblFlg) throws ClassNotFoundException, SQLException {
|
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
if (tblFlg == 0) {
|
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
|
||||||
empDao.empDelete(Integer.parseInt(inputCheck("社員ID", true))); // 社員IDの照合に失敗した場合は削除処理を行わない
|
|
||||||
} else {
|
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
|
||||||
depDao.depDelete(Integer.parseInt(inputCheck("部署ID", true))); // 部署IDの照合に失敗した場合は削除処理を行わない
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* テーブルを切り替える
|
|
||||||
*
|
|
||||||
* @param tblFlg
|
|
||||||
* テーブル名判定用フラグ
|
|
||||||
* @return 変更後テーブル名判定用フラグ
|
|
||||||
*/
|
|
||||||
public static int ChangeTable(int tblFlg) {
|
|
||||||
if (tblFlg==0) {
|
|
||||||
tblFlg = 1;
|
|
||||||
} else {
|
|
||||||
tblFlg = 0;
|
|
||||||
}
|
|
||||||
return tblFlg;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 入力値を判定し、返却する
|
|
||||||
*
|
|
||||||
* @param titleName
|
|
||||||
* 入力項目名
|
|
||||||
* @param flg
|
|
||||||
* 数値チェック可否フラグ
|
|
||||||
* @return 入力内容
|
|
||||||
*/
|
|
||||||
// 入力必須
|
|
||||||
public static String inputCheck(String titleName, boolean flg) {
|
|
||||||
String str = null;
|
|
||||||
while (true) {
|
|
||||||
System.out.println(titleName + "を入力してください");
|
|
||||||
str = sc.nextLine();
|
|
||||||
if (str.trim().length() == 0) {
|
|
||||||
System.out.println(titleName + "は入力必須です。");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (flg) {
|
|
||||||
if (str.trim().length() > 0) {
|
|
||||||
try {
|
|
||||||
Integer.parseInt(str);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
System.out.println(titleName + "は数値入力です");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
// 未入力許可
|
|
||||||
public static String inputCheck(String titleName, boolean flg, String AllowEmpty) {
|
|
||||||
String str = null;
|
|
||||||
while (true) {
|
|
||||||
System.out.println(titleName + "を入力してください");
|
|
||||||
str = sc.nextLine();
|
|
||||||
if (flg) {
|
|
||||||
if (str.trim().length() > 0) {
|
|
||||||
try {
|
|
||||||
Integer.parseInt(str);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
System.out.println(titleName + "は数値入力です");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 社員テーブルの出力文字列を生成する
|
|
||||||
* ・各列の横幅が揃うように位置を調整する
|
|
||||||
* ・部署IDを部署名に変更する
|
|
||||||
*
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
* @return 出力文字列
|
|
||||||
*/
|
|
||||||
public static String toStringEmployee(EmployeeDTO employee) throws ClassNotFoundException, SQLException {
|
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
|
||||||
return format(8, employee.getIdEmployee())
|
|
||||||
+format(16, employee.getNmEmployee())
|
|
||||||
+format(24, employee.getKnEmployee())
|
|
||||||
+format(24, employee.getMailaddress())
|
|
||||||
+format(8, employee.getPassword())
|
|
||||||
+format(16, depDao.getNmDepartment(employee.getIdDepartment()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 部署テーブルの出力文字列を生成する
|
|
||||||
* ・各列の横幅が揃うように位置を調整する
|
|
||||||
*
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
* @return 出力文字列
|
|
||||||
*/
|
|
||||||
public static String toStringDepartment(DepartmentDTO department) throws ClassNotFoundException, SQLException {
|
|
||||||
return format(8, department.getIdDepartment())
|
|
||||||
+format(12, department.getNmDepartment());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 出力文字数の表示幅が一定になるように空白を追加する
|
|
||||||
*
|
|
||||||
* @param bytelength
|
|
||||||
* 指定表示幅
|
|
||||||
* @param input
|
|
||||||
* 空白を追加する文字列
|
|
||||||
* @return 空白を追加した文字列
|
|
||||||
*/
|
|
||||||
// 文字列入力
|
|
||||||
public static String format(int bytelength, String input) {
|
|
||||||
int diff = (input.getBytes(Charset.forName("UTF-8")).length - input.length())/2; // diff=全角文字(3byte)→文字数/半角文字(1byte)→0
|
|
||||||
if (bytelength-diff>=0) {
|
|
||||||
return String.format("%-"+(bytelength-diff)+"s", input); // 表示幅=指定幅-全角文字表示に必要な追加表示幅
|
|
||||||
} else {
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 整数入力
|
|
||||||
public static String format(int bytelength, int id) {
|
|
||||||
String input = Integer.toString(id);
|
|
||||||
return format(bytelength, input);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -44,10 +44,17 @@ public class DbUtil {
|
|||||||
* 生成されたConnectionを返却する
|
* 生成されたConnectionを返却する
|
||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
|
||||||
|
public static Connection getConnection()
|
||||||
|
throws ClassNotFoundException, SQLException {
|
||||||
|
|
||||||
Class.forName(DB_DRIVER);
|
Class.forName(DB_DRIVER);
|
||||||
// Connection : not null && 接続状態 → フィールドを戻す
|
|
||||||
if( !((con!=null)&&(con.isValid(1))) ) con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
con = DriverManager.getConnection(
|
||||||
|
DB_URL,
|
||||||
|
DB_USER,
|
||||||
|
DB_PASS);
|
||||||
|
|
||||||
return con;
|
return con;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,8 +67,13 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
|
||||||
if (stmt!=null) stmt.close();
|
public static void closeStatement(PreparedStatement stmt)
|
||||||
|
throws SQLException {
|
||||||
|
|
||||||
|
if (stmt != null) {
|
||||||
|
stmt.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -74,9 +86,12 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
public static void closeConnection(Connection con) throws SQLException {
|
|
||||||
if (con!=null) con.close();
|
public static void closeConnection(Connection con)
|
||||||
|
throws SQLException {
|
||||||
|
|
||||||
|
if (con != null) {
|
||||||
|
con.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user