10段階目まで完了済
This commit is contained in:
@@ -16,6 +16,17 @@ public class DepartmentDAO {
|
|||||||
private static final String ID_DEPARTMENT = "id_department";
|
private static final String ID_DEPARTMENT = "id_department";
|
||||||
private static final String NM_DEPARTMENT = "nm_department";
|
private static final String NM_DEPARTMENT = "nm_department";
|
||||||
|
|
||||||
|
/** アクセサ */
|
||||||
|
public static String getTblName() {
|
||||||
|
return TBL_NAME;
|
||||||
|
}
|
||||||
|
public static String getIdColumnName() {
|
||||||
|
return ID_DEPARTMENT;
|
||||||
|
}
|
||||||
|
public static String getNmColumnName() {
|
||||||
|
return NM_DEPARTMENT;
|
||||||
|
}
|
||||||
|
|
||||||
/** DBコネクション */
|
/** DBコネクション */
|
||||||
public Connection con;
|
public Connection con;
|
||||||
/** DBステートメント */
|
/** DBステートメント */
|
||||||
@@ -91,7 +102,44 @@ public class DepartmentDAO {
|
|||||||
nRecords++;
|
nRecords++;
|
||||||
}
|
}
|
||||||
if (nRecords==0) {
|
if (nRecords==0) {
|
||||||
System.out.println("入力された値で情報がみつかりませんでした");
|
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 {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
@@ -107,10 +155,10 @@ public class DepartmentDAO {
|
|||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
*/
|
*/
|
||||||
public String getNmDepartment(int idDepartment) throws SQLException, ClassNotFoundException {
|
public String getNmDepartment(int idDepartment) throws SQLException, ClassNotFoundException {
|
||||||
String name = Integer.toString(idDepartment); // 部署名が見つからない場合は部署IDを返却する
|
|
||||||
Connection con = DbUtil.getConnection();
|
Connection con = DbUtil.getConnection();
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
DepartmentDAO depDao = new DepartmentDAO(con);
|
||||||
if(depDao.selectById(idDepartment).size()!=0) {
|
String name = "";
|
||||||
|
if(depDao.selectById(idDepartment).size()==1) {
|
||||||
DepartmentDTO department = depDao.selectById(idDepartment).get(0);
|
DepartmentDTO department = depDao.selectById(idDepartment).get(0);
|
||||||
name = department.getNmDepartment();
|
name = department.getNmDepartment();
|
||||||
};
|
};
|
||||||
@@ -160,6 +208,11 @@ public class DepartmentDAO {
|
|||||||
sql.append(" (" + NM_DEPARTMENT + ") ");
|
sql.append(" (" + NM_DEPARTMENT + ") ");
|
||||||
sql.append(" VALUES ");
|
sql.append(" VALUES ");
|
||||||
sql.append(" ( ? ) ");
|
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 {
|
try {
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
stmt.setString(1,name);
|
stmt.setString(1,name);
|
||||||
@@ -169,6 +222,7 @@ public class DepartmentDAO {
|
|||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部署テーブルの指定された部署IDに一致するデータを引数の内容に更新する
|
* 部署テーブルの指定された部署IDに一致するデータを引数の内容に更新する
|
||||||
@@ -177,14 +231,18 @@ public class DepartmentDAO {
|
|||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
*/
|
*/
|
||||||
public void depUpdate(int idDepartment, String name) throws SQLException, ClassNotFoundException {
|
public void depUpdate(int idDepartment, String name) throws SQLException, ClassNotFoundException {
|
||||||
if (selectById(idDepartment).size()!=0) {
|
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" UPDATE ");
|
sql.append(" UPDATE ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" " + TBL_NAME);
|
||||||
sql.append(" SET ");
|
sql.append(" SET ");
|
||||||
sql.append(" "+NM_DEPARTMENT+" = ?"+" ");
|
sql.append(" " + NM_DEPARTMENT + " = " + "?" + " ");
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE ");
|
||||||
sql.append(" " + ID_DEPARTMENT + " = " + "?");
|
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 {
|
try {
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
stmt.setString(1,name);
|
stmt.setString(1,name);
|
||||||
|
|||||||
@@ -14,6 +14,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.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;
|
||||||
|
|
||||||
@@ -109,7 +110,120 @@ public class EmployeeDAO {
|
|||||||
nRecords++;
|
nRecords++;
|
||||||
}
|
}
|
||||||
if (nRecords==0) {
|
if (nRecords==0) {
|
||||||
System.out.println("入力された値で情報がみつかりませんでした");
|
System.out.println("入力された社員IDで情報がみつかりませんでした");
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
return rtnList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 社員テーブルから条件に一致するデータを全件取得する(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();
|
||||||
|
sql.append(" SELECT ");
|
||||||
|
sql.append(" " + "*");
|
||||||
|
sql.append(" FROM ");
|
||||||
|
sql.append(" " + TBL_NAME);
|
||||||
|
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 (idDepartment!=0) sql.append(" " + ID_DEPARTMENT + " = " + "?" + "AND");
|
||||||
|
sql.delete(sql.length()-3, sql.length()); // 末尾"AND"を削除する
|
||||||
|
try {
|
||||||
|
int nConditions = 0;
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
if (idEmployee!=0) stmt.setInt(++nConditions, idEmployee);
|
||||||
|
if (name.trim().length()!=0) stmt.setString(++nConditions, name);
|
||||||
|
if (kn.trim().length()!=0) stmt.setString(++nConditions, kn);
|
||||||
|
if (mailaddress.trim().length()!=0) stmt.setString(++nConditions, mailaddress);
|
||||||
|
if (password.trim().length()!=0) stmt.setString(++nConditions, password);
|
||||||
|
if (idDepartment!=0) stmt.setInt(++nConditions, idDepartment);
|
||||||
|
if (nConditions==0) {
|
||||||
|
System.out.println("情報が入力されていません");
|
||||||
|
} else {
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
int nRecords = 0;
|
||||||
|
while (rs.next()) {
|
||||||
|
EmployeeDTO employee = new EmployeeDTO();
|
||||||
|
employee.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
|
employee.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
|
employee.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
|
employee.setMailaddress(rs.getString(MAIL_ADDRESS));
|
||||||
|
employee.setPassword(rs.getString(PASSWORD));
|
||||||
|
employee.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||||
|
rtnList.add(employee);
|
||||||
|
nRecords++;
|
||||||
|
}
|
||||||
|
if (nRecords==0) {
|
||||||
|
System.out.println("入力された社員情報で情報がみつかりませんでした");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
return rtnList;
|
||||||
|
}
|
||||||
|
// 部署名入力
|
||||||
|
public List<EmployeeDTO> empSelect(int idEmployee, String name, String kn, String mailaddress, String password, String nmDepartment) throws SQLException, ClassNotFoundException {
|
||||||
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" SELECT ");
|
||||||
|
sql.append(" " + "*");
|
||||||
|
sql.append(" FROM ");
|
||||||
|
sql.append(" " + TBL_NAME);
|
||||||
|
if (nmDepartment.trim().length()!=0) { // テーブル結合
|
||||||
|
sql.append(" INNER JOIN " + DepartmentDAO.getTblName() + " ON ");
|
||||||
|
sql.append(TBL_NAME+"."+ID_DEPARTMENT + " = " + DepartmentDAO.getTblName()+"."+DepartmentDAO.getIdColumnName());
|
||||||
|
}
|
||||||
|
sql.append(" WHERE ");
|
||||||
|
if (idEmployee!=0) sql.append(" " + ID_EMPLOYEE + " = " + "?" + "AND");
|
||||||
|
if (name.trim().length()!=0) sql.append(" " + NM_EMPLOYEE + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
||||||
|
if (kn.trim().length()!=0) sql.append(" "+KN_EMPLOYEE + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
||||||
|
if (mailaddress.trim().length()!=0) sql.append(" " + MAIL_ADDRESS + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
||||||
|
if (password.trim().length()!=0) sql.append(" " + PASSWORD + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
||||||
|
if (nmDepartment.trim().length()!=0) sql.append(" " + DepartmentDAO.getNmColumnName() + " LIKE " + " CONCAT('%',?,'%')" + "AND");
|
||||||
|
sql.delete(sql.length()-3, sql.length()); // 末尾"AND"を削除する
|
||||||
|
try {
|
||||||
|
int nConditions = 0;
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
if (idEmployee!=0) stmt.setInt(++nConditions, idEmployee);
|
||||||
|
if (name.trim().length()!=0) stmt.setString(++nConditions, name);
|
||||||
|
if (kn.trim().length()!=0) stmt.setString(++nConditions, kn);
|
||||||
|
if (mailaddress.trim().length()!=0) stmt.setString(++nConditions, mailaddress);
|
||||||
|
if (password.trim().length()!=0) stmt.setString(++nConditions, password);
|
||||||
|
if (nmDepartment.trim().length()!=0) stmt.setString(++nConditions, nmDepartment);
|
||||||
|
if (nConditions==0) {
|
||||||
|
System.out.println("情報が入力されていません");
|
||||||
|
} else {
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
int nRecords = 0;
|
||||||
|
while (rs.next()) {
|
||||||
|
EmployeeDTO employee = new EmployeeDTO();
|
||||||
|
employee.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
|
employee.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
|
employee.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
|
employee.setMailaddress(rs.getString(MAIL_ADDRESS));
|
||||||
|
employee.setPassword(rs.getString(PASSWORD));
|
||||||
|
employee.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||||
|
rtnList.add(employee);
|
||||||
|
nRecords++;
|
||||||
|
}
|
||||||
|
if (nRecords==0) {
|
||||||
|
System.out.println("入力された社員情報で情報がみつかりませんでした");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
@@ -153,13 +267,21 @@ public class EmployeeDAO {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
*/
|
*/
|
||||||
|
// 部署ID入力
|
||||||
public void empInsert(String name, String kn, String mailaddress, String password, int idDepartment) throws SQLException, ClassNotFoundException {
|
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();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" INSERT INTO ");
|
sql.append(" INSERT INTO ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" " + TBL_NAME);
|
||||||
sql.append(" (" + NM_EMPLOYEE + "," + KN_EMPLOYEE + "," + MAIL_ADDRESS + "," + PASSWORD + "," + ID_DEPARTMENT + ") ");
|
sql.append(" (" + NM_EMPLOYEE + "," + KN_EMPLOYEE + "," + MAIL_ADDRESS + "," + PASSWORD + "," + ID_DEPARTMENT + ") ");
|
||||||
sql.append(" VALUES ");
|
sql.append(" VALUES ");
|
||||||
sql.append(" (?, ?, ?, ?, ?) ");
|
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 {
|
try {
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
stmt.setString(1,name);
|
stmt.setString(1,name);
|
||||||
@@ -173,6 +295,42 @@ public class EmployeeDAO {
|
|||||||
DbUtil.closeStatement(this.stmt);
|
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に一致するデータを引数の内容に更新する
|
* 社員テーブルの指定された社員IDに一致するデータを引数の内容に更新する
|
||||||
@@ -180,19 +338,32 @@ public class EmployeeDAO {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
*/
|
*/
|
||||||
|
// 部署ID入力
|
||||||
public void empUpdate(int idEmployee, String name, String kn, String mailaddress, String password, int idDepartment) throws SQLException, ClassNotFoundException {
|
public void empUpdate(int idEmployee, String name, String kn, String mailaddress, String password, int idDepartment) throws SQLException, ClassNotFoundException {
|
||||||
if (selectById(idEmployee).size()!=0) {
|
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();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" UPDATE ");
|
sql.append(" UPDATE ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" " + TBL_NAME);
|
||||||
sql.append(" SET ");
|
sql.append(" SET ");
|
||||||
sql.append(" "+NM_EMPLOYEE+" = ?"+",");
|
sql.append(" " + NM_EMPLOYEE + " = " + "?" + ",");
|
||||||
sql.append(" "+KN_EMPLOYEE+" = ?"+",");
|
sql.append(" " + KN_EMPLOYEE + " = " + "?" +",");
|
||||||
sql.append(" "+MAIL_ADDRESS+" = ?"+",");
|
sql.append(" " + MAIL_ADDRESS + " = " + "?" + ",");
|
||||||
sql.append(" "+PASSWORD+" = ?"+",");
|
sql.append(" " + PASSWORD + " = " + "?" + ",");
|
||||||
sql.append(" "+ID_DEPARTMENT+" = ?");
|
sql.append(" " + ID_DEPARTMENT + " = " + "?");
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE ");
|
||||||
sql.append(" " + ID_EMPLOYEE + " = " + "?");
|
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 {
|
try {
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
stmt.setString(1,name);
|
stmt.setString(1,name);
|
||||||
@@ -208,5 +379,51 @@ public class EmployeeDAO {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// 部署名入力
|
||||||
|
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つに特定できません");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.rhizome.example.main;
|
package com.rhizome.example.main;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -94,14 +95,33 @@ public class MainNew {
|
|||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
empList = empDao.selectById(Integer.parseInt(inputCheck("社員ID", true)));
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (EmployeeDTO emp : empList) {
|
for (EmployeeDTO emp : empList) {
|
||||||
System.out.println(emp);
|
//System.out.println(emp);
|
||||||
//System.out.println(toStringEmployee(emp)); // 出力文字調整:ongoing
|
System.out.println(toStringEmployee(emp));
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection(empDao.con);
|
DbUtil.closeConnection(empDao.con);
|
||||||
@@ -132,8 +152,8 @@ public class MainNew {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (DepartmentDTO dep : depList) {
|
for (DepartmentDTO dep : depList) {
|
||||||
System.out.println(dep);
|
//System.out.println(dep);
|
||||||
//System.out.println(toStringDepartment(dep)); // 出力文字調整:ongoing
|
System.out.println(toStringDepartment(dep));
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection(depDao.con);
|
DbUtil.closeConnection(depDao.con);
|
||||||
@@ -185,11 +205,17 @@ public class MainNew {
|
|||||||
Connection con = DbUtil.getConnection();
|
Connection con = DbUtil.getConnection();
|
||||||
if (tblFlg == 0) {
|
if (tblFlg == 0) {
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
empDao.empInsert(inputCheck("社員名", false),
|
String name = inputCheck("社員名", false);
|
||||||
inputCheck("フリガナ", false),
|
String kn = inputCheck("フリガナ", false);
|
||||||
inputCheck("メールアドレス", false),
|
String mailaddress = inputCheck("メールアドレス", false);
|
||||||
inputCheck("パスワード", false),
|
String password = inputCheck("パスワード", false);
|
||||||
inputCheckIdDepartmentRep()); // 部署IDと照合できるまで入力を受け付ける
|
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 {
|
} else {
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
DepartmentDAO depDao = new DepartmentDAO(con);
|
||||||
depDao.depInsert(inputCheck("部署名", false));
|
depDao.depInsert(inputCheck("部署名", false));
|
||||||
@@ -208,15 +234,24 @@ public class MainNew {
|
|||||||
Connection con = DbUtil.getConnection();
|
Connection con = DbUtil.getConnection();
|
||||||
if (tblFlg == 0) {
|
if (tblFlg == 0) {
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
int id = Integer.parseInt(inputCheck("社員ID", true));
|
int idEmployee = Integer.parseInt(inputCheck("社員ID", true));
|
||||||
if (empDao.selectById(id).size()==0) {} // 社員IDの照合に失敗した場合は更新処理を行わない
|
if (empDao.selectById(idEmployee).size()==0) {} // 社員IDの照合に失敗した場合は更新処理を行わない
|
||||||
else {
|
else {
|
||||||
empDao.empUpdate(id,
|
String name = inputCheck("社員名", false, "AllowEmpty");
|
||||||
inputCheck("社員名", false),
|
String kn = inputCheck("フリガナ", false, "AllowEmpty");
|
||||||
inputCheck("フリガナ", false),
|
String mailaddress = inputCheck("メールアドレス", false, "AllowEmpty");
|
||||||
inputCheck("メールアドレス", false),
|
String password = inputCheck("パスワード", false, "AllowEmpty");
|
||||||
inputCheck("パスワード", false),
|
String infoDepartment = (inputCheck("部署情報", false, "AllowEmpty"));
|
||||||
inputCheckIdDepartmentRep()); // 部署IDと照合できるまで入力を受け付ける
|
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 {
|
} else {
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
DepartmentDAO depDao = new DepartmentDAO(con);
|
||||||
@@ -253,7 +288,7 @@ public class MainNew {
|
|||||||
*
|
*
|
||||||
* @param tblFlg
|
* @param tblFlg
|
||||||
* テーブル名判定用フラグ
|
* テーブル名判定用フラグ
|
||||||
* @return 変更後フラグ
|
* @return 変更後テーブル名判定用フラグ
|
||||||
*/
|
*/
|
||||||
public static int ChangeTable(int tblFlg) {
|
public static int ChangeTable(int tblFlg) {
|
||||||
if (tblFlg==0) {
|
if (tblFlg==0) {
|
||||||
@@ -271,8 +306,9 @@ public class MainNew {
|
|||||||
* 入力項目名
|
* 入力項目名
|
||||||
* @param flg
|
* @param flg
|
||||||
* 数値チェック可否フラグ
|
* 数値チェック可否フラグ
|
||||||
* @return
|
* @return 入力内容
|
||||||
*/
|
*/
|
||||||
|
// 入力必須
|
||||||
public static String inputCheck(String titleName, boolean flg) {
|
public static String inputCheck(String titleName, boolean flg) {
|
||||||
String str = null;
|
String str = null;
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -296,50 +332,30 @@ public class MainNew {
|
|||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
// 未入力許可
|
||||||
/**
|
public static String inputCheck(String titleName, boolean flg, String AllowEmpty) {
|
||||||
* 入力値と社員IDを照合し、返却する(照合できるまで入力を受け付ける)
|
String str = null;
|
||||||
*
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static int inputCheckIdEmployeeRep() throws ClassNotFoundException, SQLException {
|
|
||||||
int id;
|
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
|
||||||
while (true) {
|
while (true) {
|
||||||
id = Integer.parseInt(inputCheck("社員ID", true));
|
System.out.println(titleName + "を入力してください");
|
||||||
if (empDao.selectById(id).size()!=0) {
|
str = sc.nextLine();
|
||||||
|
if (flg) {
|
||||||
|
if (str.trim().length() > 0) {
|
||||||
|
try {
|
||||||
|
Integer.parseInt(str);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
System.out.println(titleName + "は数値入力です");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
return str;
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 入力値と部署IDを照合し、返却する(照合できるまで入力を受け付ける)
|
|
||||||
*
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @throws SQLException
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static int inputCheckIdDepartmentRep() throws ClassNotFoundException, SQLException {
|
|
||||||
int id;
|
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
|
||||||
while (true) {
|
|
||||||
id = Integer.parseInt(inputCheck("部署ID", true));
|
|
||||||
if (depDao.selectById(id).size()!=0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 社員テーブルの出力文字列を生成する
|
* 社員テーブルの出力文字列を生成する
|
||||||
* ・縦の線が揃うように位置を調整する
|
* ・各列の横幅が揃うように位置を調整する
|
||||||
* ・部署IDを部署名に変更する
|
* ・部署IDを部署名に変更する
|
||||||
*
|
*
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
@@ -349,58 +365,49 @@ public class MainNew {
|
|||||||
public static String toStringEmployee(EmployeeDTO employee) throws ClassNotFoundException, SQLException {
|
public static String toStringEmployee(EmployeeDTO employee) throws ClassNotFoundException, SQLException {
|
||||||
Connection con = DbUtil.getConnection();
|
Connection con = DbUtil.getConnection();
|
||||||
DepartmentDAO depDao = new DepartmentDAO(con);
|
DepartmentDAO depDao = new DepartmentDAO(con);
|
||||||
return fixCharCount(8, employee.getIdEmployee())
|
return format(8, employee.getIdEmployee())
|
||||||
+fixCharCountFull(8, employee.getNmEmployee())
|
+format(16, employee.getNmEmployee())
|
||||||
+fixCharCountFull(12, employee.getKnEmployee())
|
+format(24, employee.getKnEmployee())
|
||||||
+fixCharCount(20, employee.getMailaddress())
|
+format(24, employee.getMailaddress())
|
||||||
+fixCharCount(12, employee.getPassword())
|
+format(8, employee.getPassword())
|
||||||
+fixCharCountFull(8, depDao.getNmDepartment(employee.getIdDepartment()));
|
+format(16, depDao.getNmDepartment(employee.getIdDepartment()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部署テーブルの出力文字列を生成する
|
* 部署テーブルの出力文字列を生成する
|
||||||
* ・縦の線が揃うように位置を調整する
|
* ・各列の横幅が揃うように位置を調整する
|
||||||
*
|
|
||||||
*
|
*
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
* @return 出力文字列
|
* @return 出力文字列
|
||||||
*/
|
*/
|
||||||
public static String toStringDepartment(DepartmentDTO department) throws ClassNotFoundException, SQLException {
|
public static String toStringDepartment(DepartmentDTO department) throws ClassNotFoundException, SQLException {
|
||||||
return fixCharCount(8, department.getIdDepartment())
|
return format(8, department.getIdDepartment())
|
||||||
+fixCharCountFull(12, department.getNmDepartment());
|
+format(12, department.getNmDepartment());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 入力文字列の文字数が一定になるように空白を追加する(半角文字/数字/全角文字)
|
* 出力文字数の表示幅が一定になるように空白を追加する
|
||||||
*
|
*
|
||||||
* @return
|
* @param bytelength
|
||||||
|
* 指定表示幅
|
||||||
|
* @param input
|
||||||
|
* 空白を追加する文字列
|
||||||
|
* @return 空白を追加した文字列
|
||||||
*/
|
*/
|
||||||
public static String fixCharCount(int nChars, String input) {
|
// 文字列入力
|
||||||
if (nChars-input.length()>=0) {
|
public static String format(int bytelength, String input) {
|
||||||
for (int i=0; i<nChars-input.length(); i++) {
|
int diff = (input.getBytes(Charset.forName("UTF-8")).length - input.length())/2; // diff=全角文字(3byte)→文字数/半角文字(1byte)→0
|
||||||
input = input + " ";
|
if (bytelength-diff>=0) {
|
||||||
}
|
return String.format("%-"+(bytelength-diff)+"s", input); // 表示幅=指定幅-全角文字表示に必要な追加表示幅
|
||||||
}
|
} else {
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
public static String fixCharCount(int nChars, int id) {
|
}
|
||||||
|
// 整数入力
|
||||||
|
public static String format(int bytelength, int id) {
|
||||||
String input = Integer.toString(id);
|
String input = Integer.toString(id);
|
||||||
if (nChars-input.length()>=0) {
|
return format(bytelength, input);
|
||||||
for (int i=0; i<nChars-input.length(); i++) {
|
|
||||||
input = input + " ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
public static String fixCharCountFull(int nChars, String input) {
|
|
||||||
if (nChars-input.length()>=0) {
|
|
||||||
System.out.println(nChars-input.length());
|
|
||||||
for (int i=0; i<nChars-input.length(); i++) {
|
|
||||||
input = input + " ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return input;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user