Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AccountDataServlet extends HttpServlet {
- private AccountService accountService;
- @Override
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- System.out.println("my servlet init() call");
- accountService = new AccountService();
- }
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
- HttpSession http = req.getSession(true);
- http.setMaxInactiveInterval(60);
- StringBuilder sb = new StringBuilder();
- resp.setContentType("text/html;charset=utf-8");
- PrintWriter pw = resp.getWriter();
- sb.append("<html><body>");
- sb.append("<h1>Serlvet</h1>").append("<br>");
- sb.append("<h1>Before database reading....</h1>");
- try {
- for (String s : accountService.getAccountsShortInfo()) {
- sb.append(s);
- }
- } catch (DAOException e) {
- throw new IOException();
- } finally {
- sb.append("<h1>After database reading...</h1>");
- sb.append("</body><html>");
- resp.setStatus(HttpServletResponse.SC_OK);
- pw.append(sb.toString());
- pw.close();
- }
- }
- __________________________________________________________________________________
- public class AccountService {
- private final AccountDAO accountDAO;
- public List<String> getAccountsShortInfo() throws DAOException {
- List<String> shortInfo = new ArrayList<>();
- for (Account a : accountDAO.getAll().values()) {
- String accountInfo = a.getName() + " " + a.getMiddleName() + " " + a.getEmail() + " " + a.getHomeAddress();
- shortInfo.add(accountInfo);
- }
- System.out.println(shortInfo.toString());
- return shortInfo;
- }
- }
- __________________________________________________________________________________
- public class AccountDAO extends DAO {
- public Map<Long, Account> getAll() throws DAOException {
- return super.getAll("SELECT * FROM account");
- }
- protected Account buildFromResult(ResultSet resultSet) throws SQLException {
- Account account;
- account = new Account(
- resultSet.getLong("account_id"),
- resultSet.getString("name"),
- resultSet.getString("middle_name"),
- resultSet.getString("last_name"),
- resultSet.getString("sex"),
- resultSet.getDate("birth_date").toLocalDate(),
- resultSet.getString("home_address"),
- resultSet.getString("work_address"),
- resultSet.getString("email"),
- resultSet.getString("skype"),
- resultSet.getString("additional_info")
- );
- return account;
- }
- }
- __________________________________________________________________________________
- abstract class DAO {
- public <E extends DataSet> Map<Long, E> getAll(String query) throws DAOException {
- Connection connection = manager.get();
- Map<Long, E> map = new HashMap<>();
- try (ResultSet resultSet = connection.createStatement().executeQuery(query)) {
- while (resultSet.next()) {
- E e = buildFromResult(resultSet);
- map.put(e.getId(), e);
- }
- } catch (SQLException e) {
- throw new DAOException(e);
- } finally {
- manager.closePool();
- }
- return map;
- }
- }
- __________________________________________________________________________________
- public class DAOException extends Exception {
- public DAOException(String text) {
- super(text);
- }
- public DAOException(Throwable cause) {
- super(cause);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment