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();
}
}
}
来源:CSDN
作者:一角残叶
链接:https://blog.csdn.net/u012292754/article/details/103791029