10 — 网上书城(3)— 修改图书

心不动则不痛 提交于 2020-01-01 13:31:14

1 修改图书信息

1.1 DAO

package com.zbt.dao;

import com.zbt.model.Book;
import com.zbt.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

public class BookDaoImpl {

    public List<Book> findAllBook() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        return qr.query("select * from books", new BeanListHandler<Book>(Book.class));

    }

    public void addBook(Book book) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "insert into books (id,name,price,pnum,category,description) values (?,?,?,?,?,?)";

        // 方式1
        //qr.update(sql, book.getName(), book.getPrice(), book.getPnum(), book.getCategory(), book.getDescription());

        // 方式2
        Object[] parms = new Object[6];

        // id必须是唯一的
        parms[0] = UUID.randomUUID().toString();
        parms[1] = book.getName();
        parms[2] = book.getPrice();
        parms[3] = book.getPnum();
        parms[4] = book.getCategory();
        parms[5] = book.getDescription();

        qr.update(sql, parms);


    }

    public Book findBookById(String id) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from books where id = ?";

        return qr.query(sql, new BeanHandler<Book>(Book.class), id);

    }

}

1.2 Service

package com.zbt.service;

import com.zbt.dao.BookDaoImpl;
import com.zbt.model.Book;

import java.sql.SQLException;
import java.util.List;

public class BookServiceImpl {
    BookDaoImpl bookDao = new BookDaoImpl();
    public List<Book> findAllBooks() {
        // 从创建 DAO 对象


        // 执行查询
        try {
            return bookDao.findAllBook();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public void addBook(Book book) {
        try {
            bookDao.addBook(book);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Book findBookById(String id) {
        try {
            return bookDao.findBookById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

}

1.3 Servlet

@WebServlet("/FindBookByIdServlet")
public class FindBookByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        String id = req.getParameter("id");

        BookServiceImpl bookService = new BookServiceImpl();

        Book book = bookService.findBookById(id);

        req.setAttribute("book", book);

        //转发
        req.getRequestDispatcher("/admin/products/edit.jsp").forward(req, resp);
    }
}

1.4 list.jsp

在这里插入图片描述

1.5 edit.jsp

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2 更新书的信息

2.1 DAO

package com.zbt.dao;

import com.zbt.model.Book;
import com.zbt.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

public class BookDaoImpl {

    public List<Book> findAllBook() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        return qr.query("select * from books", new BeanListHandler<Book>(Book.class));

    }

    public void addBook(Book book) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "insert into books (id,name,price,pnum,category,description) values (?,?,?,?,?,?)";

        // 方式1
        //qr.update(sql, book.getName(), book.getPrice(), book.getPnum(), book.getCategory(), book.getDescription());

        // 方式2
        Object[] parms = new Object[6];

        // id必须是唯一的
        parms[0] = UUID.randomUUID().toString();
        parms[1] = book.getName();
        parms[2] = book.getPrice();
        parms[3] = book.getPnum();
        parms[4] = book.getCategory();
        parms[5] = book.getDescription();

        qr.update(sql, parms);
    }

    public Book findBookById(String id) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from books where id = ?";

        return qr.query(sql, new BeanHandler<Book>(Book.class), id);

    }


    public void updateBook(Book book) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "update books set name = ?, price = ?, pnum = ?, category = ?, description = ?" +
                "where id = ?";

        qr.update(sql,book.getName(),book.getPrice(),
                book.getPnum(),book.getCategory(),
                book.getDescription(),book.getId());

    }

}

2.2 Service

package com.zbt.service;

import com.zbt.dao.BookDaoImpl;
import com.zbt.model.Book;

import java.sql.SQLException;
import java.util.List;

public class BookServiceImpl {
    BookDaoImpl bookDao = new BookDaoImpl();
    public List<Book> findAllBooks() {
        // 从创建 DAO 对象


        // 执行查询
        try {
            return bookDao.findAllBook();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public void addBook(Book book) {
        try {
            bookDao.addBook(book);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Book findBookById(String id) {
        try {
            return bookDao.findBookById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public void updateBook(Book book){
        try {
            bookDao.updateBook(book);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
}

2.3 Servlet

package com.zbt.web.servlet;

import com.zbt.model.Book;
import com.zbt.service.BookServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/UpdateBookServlet")
public class UpdateBookServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        Book book = new Book();
        try {
            BeanUtils.populate(book, req.getParameterMap());

            BookServiceImpl bookService = new BookServiceImpl();
            bookService.updateBook(book);

            // 回到 list  页面
            List<Book> books = bookService.findAllBooks();
            req.setAttribute("books", books);
            req.getRequestDispatcher("/admin/products/list.jsp").forward(req, resp);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}


在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!