3段階目(DAOのIDによる検索メソッドを作成→条件検索完成)
This commit is contained in:
@@ -12,6 +12,7 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
import com.rhizome.example.dto.EmployeeDTO;
|
import com.rhizome.example.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
@@ -72,4 +73,45 @@ public class EmployeeDAO {
|
|||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 社員テーブルから社員IDに合致する情報を取得する
|
||||||
|
*
|
||||||
|
* @return 検索結果
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
* @throws NumberFormatException
|
||||||
|
*/
|
||||||
|
public List<EmployeeDTO> selectById(int id)
|
||||||
|
throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException {
|
||||||
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" SELECT ");
|
||||||
|
sql.append(" " + "*");
|
||||||
|
sql.append(" FROM ");
|
||||||
|
sql.append(" " + TBL_NAME);
|
||||||
|
sql.append(" WHERE ");
|
||||||
|
sql.append(" " + ID_EMPLOYEE);
|
||||||
|
sql.append(" = ");
|
||||||
|
sql.append(" " + id);
|
||||||
|
try {
|
||||||
|
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
while (rs.next()) {
|
||||||
|
|
||||||
|
EmployeeDTO dto = new EmployeeDTO(rs.getInt(ID_EMPLOYEE), rs.getString(NM_EMPLOYEE),
|
||||||
|
rs.getString(KN_EMPLOYEE), rs.getString(MAIL_ADDRESS), rs.getString(PASSWORD),
|
||||||
|
rs.getInt(ID_DEPARTMENT));
|
||||||
|
rtnList.add(dto);
|
||||||
|
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
if (rtnList.size() == 0) {
|
||||||
|
System.out.println("入力された値で情報がみつかりませんでした");
|
||||||
|
}
|
||||||
|
return rtnList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,6 +87,9 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
|
case "2":
|
||||||
|
empList = empDao.selectById(Integer.parseInt(inputCheck("ID", true)));
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user