10 — 网上书城(2)— 添加图书

不问归期 提交于 2020-01-23 23:23:56

1 添加图书

form 表单 ——》提交到 Servlet ——》Service ——》 DAO ——》保存数据到数据库 ——》回到列表界面


  • list.jsp
    在这里插入图片描述
    在这里插入图片描述

add.jsp

  • action
enctype="application/x-www-form-urlencoded" 用于普通表单提交
enctype="multipart/form-data" 用于有文件的表单提交

在这里插入图片描述

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.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);


    }
}

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 {
    public List<Book> findAllBooks() {
        // 从创建 DAO 对象
        BookDaoImpl bookDao = new BookDaoImpl();

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

        return null;
    }

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

1.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.lang.reflect.InvocationTargetException;
import java.util.List;

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

        req.setCharacterEncoding("utf-8");

        // 1 表单数据封装成模型
       Book book = new Book();
        try {
            BeanUtils.populate(book,req.getParameterMap());
            System.out.println(book);

            // 2 调用 Service
            BookServiceImpl bookService = new BookServiceImpl();
            bookService.addBook(book);

            // 3 返回 list 列表页面
            // 重新获取 book 数据
            List<Book> books = bookService.findAllBooks();
            req.setAttribute("books",books);
            req.getRequestDispatcher("/admin/products/list.jsp").forward(req, resp);

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


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

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