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