Guest User

КОД

a guest
Mar 14th, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | Software | 0 0
  1. public class AccountDataServlet extends HttpServlet {
  2.  
  3. private AccountService accountService;
  4.  
  5. @Override
  6. public void init(ServletConfig config) throws ServletException {
  7. super.init(config);
  8. System.out.println("my servlet init() call");
  9. accountService = new AccountService();
  10. }
  11.  
  12. @Override
  13. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  14. HttpSession http = req.getSession(true);
  15. http.setMaxInactiveInterval(60);
  16. StringBuilder sb = new StringBuilder();
  17. resp.setContentType("text/html;charset=utf-8");
  18. PrintWriter pw = resp.getWriter();
  19. sb.append("<html><body>");
  20. sb.append("<h1>Serlvet</h1>").append("<br>");
  21. sb.append("<h1>Before database reading....</h1>");
  22. try {
  23. for (String s : accountService.getAccountsShortInfo()) {
  24. sb.append(s);
  25. }
  26. } catch (DAOException e) {
  27. throw new IOException();
  28. } finally {
  29. sb.append("<h1>After database reading...</h1>");
  30. sb.append("</body><html>");
  31. resp.setStatus(HttpServletResponse.SC_OK);
  32. pw.append(sb.toString());
  33. pw.close();
  34. }
  35. }
  36.  
  37. __________________________________________________________________________________
  38.  
  39. public class AccountService {
  40.  
  41. private final AccountDAO accountDAO;
  42.  
  43. public List<String> getAccountsShortInfo() throws DAOException {
  44. List<String> shortInfo = new ArrayList<>();
  45. for (Account a : accountDAO.getAll().values()) {
  46. String accountInfo = a.getName() + " " + a.getMiddleName() + " " + a.getEmail() + " " + a.getHomeAddress();
  47. shortInfo.add(accountInfo);
  48. }
  49. System.out.println(shortInfo.toString());
  50. return shortInfo;
  51. }
  52. }
  53.  
  54. __________________________________________________________________________________
  55.  
  56. public class AccountDAO extends DAO {
  57.  
  58. public Map<Long, Account> getAll() throws DAOException {
  59. return super.getAll("SELECT * FROM account");
  60. }
  61.  
  62. protected Account buildFromResult(ResultSet resultSet) throws SQLException {
  63. Account account;
  64. account = new Account(
  65. resultSet.getLong("account_id"),
  66. resultSet.getString("name"),
  67. resultSet.getString("middle_name"),
  68. resultSet.getString("last_name"),
  69. resultSet.getString("sex"),
  70. resultSet.getDate("birth_date").toLocalDate(),
  71. resultSet.getString("home_address"),
  72. resultSet.getString("work_address"),
  73. resultSet.getString("email"),
  74. resultSet.getString("skype"),
  75. resultSet.getString("additional_info")
  76. );
  77. return account;
  78. }
  79.  
  80. }
  81.  
  82. __________________________________________________________________________________
  83.  
  84. abstract class DAO {
  85.  
  86. public <E extends DataSet> Map<Long, E> getAll(String query) throws DAOException {
  87. Connection connection = manager.get();
  88. Map<Long, E> map = new HashMap<>();
  89. try (ResultSet resultSet = connection.createStatement().executeQuery(query)) {
  90. while (resultSet.next()) {
  91. E e = buildFromResult(resultSet);
  92. map.put(e.getId(), e);
  93. }
  94. } catch (SQLException e) {
  95. throw new DAOException(e);
  96. } finally {
  97. manager.closePool();
  98. }
  99. return map;
  100. }
  101. }
  102.  
  103. __________________________________________________________________________________
  104.  
  105. public class DAOException extends Exception {
  106.  
  107. public DAOException(String text) {
  108. super(text);
  109. }
  110.  
  111. public DAOException(Throwable cause) {
  112. super(cause);
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment