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