Advertisement
haithienht

[JavaEE] MVC

Oct 7th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. 1. Tạo project DemoMVC
  2. 2. Add web.xml > mở lên vào tab Pages > điền vào Welcome files: ControllerServlet
  3. 3. Tạo Servlet ControllerServlet trong package vn.aptech.controller
  4. Thêm đoạn sau:
  5. String action = request.getParameter("aciton");
  6. BookDal dal = new BookDal();
  7. if (action == null) {
  8. request.setAttribute("books", dal.getBooks());
  9. request.getRequestDispatcher("index.jsp").forward(request, response);
  10. }
  11. 4. tạo Class Book trong vn.aptech.entity
  12. tạo hàm dựng không tham số
  13. Thêm: int bookID, price; String title;
  14. Và refactor
  15. 5. Add JAR vào libraries driver loại 4 MSSQL
  16. 6. Tạo Class BookDal trong vn.aptech.dal thêm hàm dựng kg tham số và thêm đoạn sau:
  17. private Connection getConnection() throws ClassNotFoundException, SQLException{
  18. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  19. String url = "jdbc:sqlserver://localhost:1433;databaseName=Demo";
  20. return DriverManager.getConnection(url,"sa","password123");
  21. }
  22.  
  23. public List<Book> getBooks() {
  24. List<Book> result = new ArrayList<>();
  25. try {
  26. Connection con = getConnection();
  27. String q = "SELECT * FROM Books";
  28. PreparedStatement stm = con.prepareStatement(q);
  29. ResultSet rs = stm.executeQuery();
  30. while (rs.next()) {
  31. Book b = new Book();
  32. b.setBookID(rs.getInt(1));
  33. b.setTitle(rs.getString(2));
  34. b.setPrice(rs.getInt(3));
  35. result.add(b);
  36. }
  37. rs.close();
  38. stm.close();
  39. con.close();
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. return result;
  44. }
  45.  
  46. public Book getBook(int id) {
  47. Book b = null;
  48. try {
  49. Connection con = getConnection();
  50. String q = "SELECT * FROM Books WHERE BookID=?";
  51. PreparedStatement stm = con.prepareStatement(q);
  52. stm.setInt(1, id);
  53. ResultSet rs = stm.executeQuery();
  54. if (rs.next()) {
  55. b.setBookID(rs.getInt(1));
  56. b.setTitle(rs.getString(2));
  57. b.setPrice(rs.getInt(3));
  58. }
  59.  
  60. rs.close();
  61. stm.close();
  62. con.close();
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. return b;
  67. }
  68.  
  69. public boolean add(Book b) {
  70. try {
  71. Connection con = getConnection();
  72. String q = "INSERT INTO Books (BookID, Title, Price) VALUES (?, ?, ?)";
  73. PreparedStatement stm = con.prepareStatement(q);
  74. stm.setInt(1, b.getBookID());
  75. stm.setString(2, b.getTitle());
  76. stm.setInt(3, b.getPrice());
  77. int rs = stm.executeUpdate();
  78. if (rs > 0) {
  79. stm.close();
  80. con.close();
  81. return true;
  82. } else {
  83. stm.close();
  84. con.close();
  85. return false;
  86. }
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }
  90. return false;
  91. }
  92.  
  93. public boolean update(Book b) {
  94. try {
  95. Connection con = getConnection();
  96. String q = "UPDATE Books SET Title = ?, Price = ? WHERE BookID = ?";
  97. PreparedStatement stm = con.prepareStatement(q);
  98. stm.setString(1, b.getTitle());
  99. stm.setInt(2, b.getPrice());
  100. stm.setInt(3, b.getBookID());
  101. int rs = stm.executeUpdate();
  102. if (rs > 0) {
  103. stm.close();
  104. con.close();
  105. return true;
  106. } else {
  107. stm.close();
  108. con.close();
  109. return false;
  110. }
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. }
  114. return false;
  115. }
  116.  
  117. public boolean delete(int id) {
  118. try {
  119. Connection con = getConnection();
  120. String q = "DELETE FROM Books WHERE BookID = ?";
  121. PreparedStatement stm = con.prepareStatement(q);
  122. stm.setInt(1, id);
  123. int rs = stm.executeUpdate();
  124. if (rs > 0) {
  125. stm.close();
  126. con.close();
  127. return true;
  128. } else {
  129. stm.close();
  130. con.close();
  131. return false;
  132. }
  133. } catch (Exception e) {
  134. e.printStackTrace();
  135. }
  136. return false;
  137. }
  138. 7. Tạo index.jsp và thêm đoạn sau:
  139. <h1>Book Index</h1>
  140. <div><a href="ControllerServlet?action=add">Add new Book</a></div>
  141. <br/>
  142. <table border="1" style="margin-top: 5px">
  143. <thead>
  144. <tr>
  145. <th>ID</th>
  146. <th>Tittle</th>
  147. <th>Price</th>
  148. <th colspan="2">Action</th>
  149. </tr>
  150. </thead>
  151. <tbody>
  152. <%
  153. List<Book> list = (List<Book>)request.getAttribute("books");
  154. for (Book b : list) {
  155. %>
  156. <tr>
  157. <td><%= b.getBookID() %></td>
  158. <td><%= b.getTitle()%></td>
  159. <td><%= b.getPrice()%></td>
  160. <td><a href="ControllerServlet?action=update&BookID=<%=b.getBookID()%>">Update</a></td>
  161. <td><a href="ControllerServlet?action=delete&BookID=<%=b.getBookID()%>" onclick="return confirm('Are you sure to delete this?')">Delete</a></td>
  162. </tr>
  163. <%
  164. }
  165. %>
  166. </tbody>
  167. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement