Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 33070d1dc6 | |||
| 9b993a436c | |||
| c7f723ffe2 | |||
| 395f2f4f90 |
@@ -1,10 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<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"/>
|
||||
|
||||
6
.project
6
.project
@@ -6,18 +6,18 @@
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,25 +7,32 @@
|
||||
package com.rhizome.example.dao;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.rhizome.example.dto.EmployeeDTO;
|
||||
//import com.rhizome.example.main.EmployeeDTO;
|
||||
//実行結果をDTOに格納し、リストに追加
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
public class EmployeeDAO {
|
||||
private static final String TBL_NAME = "employee";
|
||||
private static final String ID_EMPLOYEE = "id_employee";
|
||||
private static final String NM_EMPLOYEE = "nm_employee";
|
||||
private static final String KN_EMPLOYEE = "kn_employee";
|
||||
private static final String MAIL_ADDRESS = "mail_address";
|
||||
private static final String PASSWORD = "password";
|
||||
private static final String ID_DEPARTMENT = "id_department";
|
||||
private static final String NM_DEPARTMENT = "nm_department";
|
||||
|
||||
|
||||
/** DBコネクション */
|
||||
//public Connection con;
|
||||
public Connection con;
|
||||
/** DBステートメント */
|
||||
//PreparedStatement stmt;
|
||||
PreparedStatement stmt;
|
||||
/** 検索結果 */
|
||||
//ResultSet rs;
|
||||
ResultSet rs;
|
||||
|
||||
/**
|
||||
* インスタンス生成時DBコネクション
|
||||
@@ -33,9 +40,9 @@ public class EmployeeDAO {
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
*/
|
||||
//public EmployeeDAO(Connection con) {
|
||||
//this.con = con;
|
||||
//}
|
||||
public EmployeeDAO(Connection con) {
|
||||
this.con = con;
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの情報を全件取得する
|
||||
@@ -46,116 +53,404 @@ public class EmployeeDAO {
|
||||
*/
|
||||
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
|
||||
Connection con = null;
|
||||
Statement stmt = null;
|
||||
|
||||
|
||||
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 ");
|
||||
try {
|
||||
// JDBCドライバの読み込み
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
// Connectionの生成
|
||||
con = DriverManager.getConnection(
|
||||
"jdbc:mysql://127.0.0.1:3306/rezodb", // データベース
|
||||
"rezouser", // ユーザ名
|
||||
"Rezo_0000" // パスワード
|
||||
);
|
||||
|
||||
String sql = "SELECT * FROM employee";
|
||||
stmt = con.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql);
|
||||
|
||||
// 実行結果をDTOに格納し、リストに追加
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
EmployeeDTO emp = new EmployeeDTO();
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
|
||||
emp.setId_employee(rs.getInt("id_employee"));
|
||||
emp.setNm_employee(rs.getString("nm_employee"));
|
||||
emp.setKn_employee(rs.getString("kn_employee"));
|
||||
emp.setMail_address(rs.getString("mail_address"));
|
||||
emp.setPassword(rs.getString("password"));
|
||||
emp.setId_department(rs.getInt("id_department"));
|
||||
|
||||
rtnList.add(emp);
|
||||
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);
|
||||
}
|
||||
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// Statement、Connectionの切断
|
||||
try {
|
||||
if (stmt != null) stmt.close();
|
||||
if (con != null) con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//IDによる条件検索
|
||||
public java.util.List<EmployeeDTO> selectById(int id) throws SQLException, ClassNotFoundException, NumberFormatException{
|
||||
//社員ID検索
|
||||
public List<EmployeeDTO> selectById(int id) throws SQLException {
|
||||
//戻り値用リスト
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
|
||||
Connection con = null;
|
||||
Statement stmt = null;
|
||||
//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 {
|
||||
// JDBCドライバの読み込み
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
// Connectionの生成
|
||||
con = DriverManager.getConnection(
|
||||
"jdbc:mysql://127.0.0.1:3306/rezodb", // データベース
|
||||
"rezouser", // ユーザ名
|
||||
"Rezo_0000" // パスワード
|
||||
);
|
||||
|
||||
String sql = "SELECT id_employee FROM employee WHERE id_employee";
|
||||
stmt = con.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql);
|
||||
|
||||
// 実行結果をDTOに格納し、リストに追加
|
||||
//PreparedStatement生成
|
||||
stmt = con.prepareStatement(sql.toString());
|
||||
//社員IDを設定
|
||||
stmt.setInt(1, id);
|
||||
//SQL実行
|
||||
rs = stmt.executeQuery();
|
||||
//検索結果をDTOへ格納
|
||||
while (rs.next()) {
|
||||
EmployeeDTO emp = new EmployeeDTO();
|
||||
//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));
|
||||
|
||||
emp.setId_employee(rs.getInt("id_employee"));
|
||||
emp.setNm_employee(rs.getString("nm_employee"));
|
||||
emp.setKn_employee(rs.getString("kn_employee"));
|
||||
emp.setMail_address(rs.getString("mail_address"));
|
||||
emp.setPassword(rs.getString("password"));
|
||||
emp.setId_department(rs.getInt("id_department"));
|
||||
|
||||
rtnList.add(emp);
|
||||
//リストへ追加
|
||||
rtnList.add(dto);
|
||||
}
|
||||
|
||||
List<EmployeeDTO> selectList = new ArrayList<>();
|
||||
|
||||
for (EmployeeDTO employee : rtnList) {
|
||||
if (employee.getPrimaryKeyValue().equals(id)) {
|
||||
selectList.add(employee);
|
||||
}
|
||||
}
|
||||
|
||||
return selectList;
|
||||
|
||||
|
||||
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// Statement、Connectionの切断
|
||||
try {
|
||||
if (stmt != null) stmt.close();
|
||||
if (con != null) con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -2,38 +2,38 @@ package com.rhizome.example.dto;
|
||||
|
||||
public class EmployeeDTO {
|
||||
|
||||
//Employeeテーブルのカラム分のフィールド
|
||||
private Integer id_employee;
|
||||
private String nm_employee;
|
||||
private String kn_employee;
|
||||
private String mail_address;
|
||||
private Integer idEmployee;
|
||||
private String nmEmployee;
|
||||
private String knEmployee;
|
||||
private String mailAddress;
|
||||
private String password;
|
||||
private Integer id_department;
|
||||
private Integer idDepartment;
|
||||
private String nmDepartment;
|
||||
|
||||
|
||||
public Integer getId_employee() {
|
||||
return id_employee;
|
||||
public Integer getIdEmployee() {
|
||||
return idEmployee;
|
||||
}
|
||||
public void setId_employee(Integer id_employee) {
|
||||
this.id_employee = id_employee;
|
||||
public void setIdEmployee(Integer idEmployee) {
|
||||
this.idEmployee = idEmployee;
|
||||
}
|
||||
public String getNm_employee() {
|
||||
return nm_employee;
|
||||
public String getNmEmployee() {
|
||||
return nmEmployee;
|
||||
}
|
||||
public void setNm_employee(String nm_employee) {
|
||||
this.nm_employee = nm_employee;
|
||||
public void setNmEmployee(String nmEmployee) {
|
||||
this.nmEmployee = nmEmployee;
|
||||
}
|
||||
public String getKn_employee() {
|
||||
return kn_employee;
|
||||
public String getKnEmployee() {
|
||||
return knEmployee;
|
||||
}
|
||||
public void setKn_employee(String kn_employee) {
|
||||
this.kn_employee = kn_employee;
|
||||
public void setKnEmployee(String knEmployee) {
|
||||
this.knEmployee = knEmployee;
|
||||
}
|
||||
public String getMail_address() {
|
||||
return mail_address;
|
||||
public String getMailAddress() {
|
||||
return mailAddress;
|
||||
}
|
||||
public void setMail_address(String mail_address) {
|
||||
this.mail_address = mail_address;
|
||||
public void setMailAddress(String mailAddress) {
|
||||
this.mailAddress = mailAddress;
|
||||
}
|
||||
public String getPassword() {
|
||||
return password;
|
||||
@@ -41,20 +41,19 @@ public class EmployeeDTO {
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
public Integer getId_department() {
|
||||
return id_department;
|
||||
public Integer getIdDepartment() {
|
||||
return idDepartment;
|
||||
}
|
||||
public void setId_department(Integer id_department) {
|
||||
this.id_department = id_department;
|
||||
}
|
||||
public Object getPrimaryKeyValue() {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
return null;
|
||||
public void setIdDepartment(Integer idDepartment) {
|
||||
this.idDepartment = idDepartment;
|
||||
}
|
||||
|
||||
public String getNmDepartment() {
|
||||
return nmDepartment;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void setNmDepartment(String nmDepartment) {
|
||||
this.nmDepartment = nmDepartment;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,7 +6,9 @@ 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;
|
||||
|
||||
@@ -51,15 +53,51 @@ public class Main {
|
||||
String ope = sc.nextLine();
|
||||
switch (ope) {
|
||||
case "1":
|
||||
//社員テーブル
|
||||
if (tblFlg == 0) {
|
||||
empSelect();
|
||||
|
||||
//部署テーブル
|
||||
} else {
|
||||
deptSelect();
|
||||
}
|
||||
break;
|
||||
case "2":
|
||||
//社員テーブル
|
||||
if (tblFlg == 0) {
|
||||
empInsert();
|
||||
|
||||
//部署テーブル
|
||||
} else {
|
||||
deptInsert();
|
||||
}
|
||||
break;
|
||||
case "3":
|
||||
//社員テーブル
|
||||
if (tblFlg == 0) {
|
||||
empUpdate();
|
||||
|
||||
//部署テーブル
|
||||
} else {
|
||||
deptUpdate();
|
||||
}
|
||||
break;
|
||||
case "4":
|
||||
//社員テーブル
|
||||
if (tblFlg == 0) {
|
||||
empDelete();
|
||||
|
||||
//部署テーブル
|
||||
} else {
|
||||
deptDelete();
|
||||
}
|
||||
break;
|
||||
case "9":
|
||||
if (tblFlg == 0) {
|
||||
tblFlg = 1;
|
||||
} else {
|
||||
tblFlg = 0;
|
||||
}
|
||||
return;
|
||||
case "0":
|
||||
System.out.println("システムを終了します");
|
||||
@@ -79,7 +117,7 @@ public class Main {
|
||||
public static void empSelect() throws ClassNotFoundException, SQLException {
|
||||
List<EmployeeDTO> empList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||
String select = sc.nextLine();
|
||||
try {
|
||||
@@ -87,26 +125,137 @@ public class Main {
|
||||
case "1":
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
|
||||
case "2":
|
||||
String id = sc.nextLine();//IDを入力
|
||||
int num = Integer.valueOf(id);//String型のIDをint型に変換
|
||||
empList = empDao.selectById(num);
|
||||
//検索項目選択
|
||||
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:
|
||||
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) {
|
||||
System.out.println(emp.getId_employee() + "\t"
|
||||
+ emp.getNm_employee() + "\t"
|
||||
+ emp.getKn_employee() + "\t"
|
||||
+ emp.getMail_address() + "\t"
|
||||
+ emp.getPassword() + "\t"
|
||||
+ emp.getId_department());
|
||||
|
||||
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 {
|
||||
DbUtil.closeConnection();
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* フラグを判定し、テーブル名を取得する
|
||||
@@ -133,9 +282,6 @@ public class Main {
|
||||
* @param flg
|
||||
* 数値チェック可否フラグ
|
||||
* @return
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
* @throws NumberFormatException
|
||||
*/
|
||||
public static String inputCheck(String titleName, boolean flg) {
|
||||
String str = null;
|
||||
@@ -155,11 +301,406 @@ public class Main {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
public class DbUtil {
|
||||
// 定数定義(共通)
|
||||
private static final String DB_HOST = "127.0.0.1";
|
||||
@@ -45,27 +44,20 @@ public class DbUtil {
|
||||
* 生成されたConnectionを返却する
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
//DB接続用メソッドを作成する
|
||||
public static Connection getConnection() throws ClassNotFoundException {
|
||||
//データベースの接続オブジェクトを初期化
|
||||
Connection con = null;
|
||||
// JDBCドライバの読み込み
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
// Connectionの生成
|
||||
try {
|
||||
public static Connection getConnection()
|
||||
throws ClassNotFoundException, SQLException {
|
||||
|
||||
Class.forName(DB_DRIVER);
|
||||
|
||||
con = DriverManager.getConnection(
|
||||
"jdbc:mysql://127.0.0.1:3306/rezodb" // データベース
|
||||
, "rezouser" // ユーザ
|
||||
, "Rezo_0000" // パスワード
|
||||
);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
DB_URL,
|
||||
DB_USER,
|
||||
DB_PASS);
|
||||
|
||||
return con;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*
|
||||
@@ -75,15 +67,15 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
public static void closeStatement() {
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
if (stmt != null) stmt.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
public static void closeStatement(PreparedStatement stmt)
|
||||
throws SQLException {
|
||||
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
@@ -94,12 +86,12 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection() {
|
||||
try {
|
||||
if (con != null) con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeConnection(Connection con)
|
||||
throws SQLException {
|
||||
|
||||
if (con != null) {
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user