Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Tạo project DemoMVC
- 2. Add web.xml > mở lên vào tab Pages > điền vào Welcome files: ControllerServlet
- 3. Tạo Servlet ControllerServlet trong package vn.aptech.controller
- Thêm đoạn sau:
- String action = request.getParameter("aciton");
- BookDal dal = new BookDal();
- if (action == null) {
- request.setAttribute("books", dal.getBooks());
- request.getRequestDispatcher("index.jsp").forward(request, response);
- }
- 4. tạo Class Book trong vn.aptech.entity
- tạo hàm dựng không tham số
- Thêm: int bookID, price; String title;
- Và refactor
- 5. Add JAR vào libraries driver loại 4 MSSQL
- 6. Tạo Class BookDal trong vn.aptech.dal thêm hàm dựng kg tham số và thêm đoạn sau:
- private Connection getConnection() throws ClassNotFoundException, SQLException{
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- String url = "jdbc:sqlserver://localhost:1433;databaseName=Demo";
- return DriverManager.getConnection(url,"sa","password123");
- }
- public List<Book> getBooks() {
- List<Book> result = new ArrayList<>();
- try {
- Connection con = getConnection();
- String q = "SELECT * FROM Books";
- PreparedStatement stm = con.prepareStatement(q);
- ResultSet rs = stm.executeQuery();
- while (rs.next()) {
- Book b = new Book();
- b.setBookID(rs.getInt(1));
- b.setTitle(rs.getString(2));
- b.setPrice(rs.getInt(3));
- result.add(b);
- }
- rs.close();
- stm.close();
- con.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- public Book getBook(int id) {
- Book b = null;
- try {
- Connection con = getConnection();
- String q = "SELECT * FROM Books WHERE BookID=?";
- PreparedStatement stm = con.prepareStatement(q);
- stm.setInt(1, id);
- ResultSet rs = stm.executeQuery();
- if (rs.next()) {
- b.setBookID(rs.getInt(1));
- b.setTitle(rs.getString(2));
- b.setPrice(rs.getInt(3));
- }
- rs.close();
- stm.close();
- con.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return b;
- }
- public boolean add(Book b) {
- try {
- Connection con = getConnection();
- String q = "INSERT INTO Books (BookID, Title, Price) VALUES (?, ?, ?)";
- PreparedStatement stm = con.prepareStatement(q);
- stm.setInt(1, b.getBookID());
- stm.setString(2, b.getTitle());
- stm.setInt(3, b.getPrice());
- int rs = stm.executeUpdate();
- if (rs > 0) {
- stm.close();
- con.close();
- return true;
- } else {
- stm.close();
- con.close();
- return false;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- public boolean update(Book b) {
- try {
- Connection con = getConnection();
- String q = "UPDATE Books SET Title = ?, Price = ? WHERE BookID = ?";
- PreparedStatement stm = con.prepareStatement(q);
- stm.setString(1, b.getTitle());
- stm.setInt(2, b.getPrice());
- stm.setInt(3, b.getBookID());
- int rs = stm.executeUpdate();
- if (rs > 0) {
- stm.close();
- con.close();
- return true;
- } else {
- stm.close();
- con.close();
- return false;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- public boolean delete(int id) {
- try {
- Connection con = getConnection();
- String q = "DELETE FROM Books WHERE BookID = ?";
- PreparedStatement stm = con.prepareStatement(q);
- stm.setInt(1, id);
- int rs = stm.executeUpdate();
- if (rs > 0) {
- stm.close();
- con.close();
- return true;
- } else {
- stm.close();
- con.close();
- return false;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- 7. Tạo index.jsp và thêm đoạn sau:
- <h1>Book Index</h1>
- <div><a href="ControllerServlet?action=add">Add new Book</a></div>
- <br/>
- <table border="1" style="margin-top: 5px">
- <thead>
- <tr>
- <th>ID</th>
- <th>Tittle</th>
- <th>Price</th>
- <th colspan="2">Action</th>
- </tr>
- </thead>
- <tbody>
- <%
- List<Book> list = (List<Book>)request.getAttribute("books");
- for (Book b : list) {
- %>
- <tr>
- <td><%= b.getBookID() %></td>
- <td><%= b.getTitle()%></td>
- <td><%= b.getPrice()%></td>
- <td><a href="ControllerServlet?action=update&BookID=<%=b.getBookID()%>">Update</a></td>
- <td><a href="ControllerServlet?action=delete&BookID=<%=b.getBookID()%>" onclick="return confirm('Are you sure to delete this?')">Delete</a></td>
- </tr>
- <%
- }
- %>
- </tbody>
- </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement