java web 简单版网上书城-图书增删查改

java web 简单版网上书城-图书增删查改
继续前一篇
https://blog.csdn.net/weixin_44889138/article/details/103908251
项目GitHub地址
https://github.com/LGH-cmd/booksystem

添加图书

  1. 在BookDao中添加方法
// 添加一本书
	public void addBook(Book book) throws Exception {
		Connection conn = JdbcUtil.getConnection();
		String sql = "insert into t_book(id,name,price,bnum,category)"
				+ "values(?,?,?,?,?)";
		PreparedStatement prps = conn.prepareStatement(sql);
		prps.setString(1, book.getId());
		prps.setString(2, book.getName());
		prps.setDouble(3, book.getPrice());
		prps.setInt(4, book.getBnum());
		prps.setString(5, book.getCategory());
		
		prps.execute();
		conn.close();
	}
  1. 添加AddBookServlet类,映射写为addBookServlet
@WebServlet("/addBookServlet")
public class AddBookServlet extends HttpServlet {
	
	private BookDao bookDao = new BookDao();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String price = req.getParameter("price");
		String bnum = req.getParameter("bnum");
		String category = req.getParameter("category");
		
		Book book = new Book();
		book.setId(id);
		book.setName(name);
		book.setPrice(Double.parseDouble(price));
		book.setBnum(Integer.parseInt(bnum));
		book.setCategory(category);
		try {
			bookDao.addBook(book);
			resp.sendRedirect(req.getContextPath() + "/bookServlet");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resp.setContentType("text/html;charset=utf-8");
			resp.getWriter().write("系统异常,请联系管理员 10086");
		}
	}
	
}

在这里插入图片描述

删除图书

  1. 在BookDao中添加方法
//删除图书
	public void deleteBookById(String id) throws Exception {
		Connection conn = JdbcUtil.getConnection();
		String sql = "delete from t_book where id=?";
		PreparedStatement prps = conn.prepareStatement(sql );
		prps.setString(1, id);
		prps.execute();
		conn.close();
		
	}
  1. 添加DeleteBookServlet类
@WebServlet("/deleteBookServlet")
public class DeleteBookServlet extends HttpServlet {
	
	private BookDao bookDao = new BookDao();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String id = req.getParameter("id");
		try {
			bookDao.deleteBookById(id);
			resp.sendRedirect(req.getContextPath() + "/bookServlet");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resp.setContentType("text/html;charset=utf-8");
			resp.getWriter().write("系统异常,请联系管理员 10086");
		}
	}
	
}

在这里插入图片描述

编辑图书

编辑图书时需要回显图书信息

  1. 先查询编辑的图书的信息

在BookDao中添加方法

// 找点击的书
	public Book findBookById(String id) throws Exception {
		Connection conn = JdbcUtil.getConnection();
		String sql = "select * from t_book where id=?";
		
		PreparedStatement prps = conn.prepareStatement(sql);
		prps.setString(1, id);
		ResultSet set = prps.executeQuery();
		Book book = new Book();
		if(set.next()) {
			//获取每一个字段的值
			String name = set.getString("name");
			double price = set.getDouble("price");
			int bnum = set.getInt("bnum");
			String category = set.getString("category");
			
			book.setId(id);
			book.setName(name);
			book.setPrice(price);
			book.setBnum(bnum);
			book.setCategory(category);
			
		}
		conn.close();
		return book;
		
	}
  1. 获得信息后回显

创建FindBookServlet

@WebServlet("/findBookServlet")
public class FindBookServlet extends HttpServlet {

	private BookDao bookDao = new BookDao();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		try {
			Book book = bookDao.findBookById(id);
			req.setAttribute("BOOK", book);
			req.getRequestDispatcher("/editBook.jsp").forward(req, resp);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resp.setContentType("text/html;charset=utf-8");
			resp.getWriter().write("系统异常,请联系管理员 10086");
		}
	}
	
}

利用EL表达式回显
id进行input隐藏,更新时需要使用
在这里插入图片描述3. 更新信息后保存

在BookDao中添加方法

// 更新 一本书
	public void updateBook(Book book) throws Exception {
		Connection conn = JdbcUtil.getConnection();
		String sql = "update t_book set name=?,price=?,bnum=?,category=? where id=?";
		PreparedStatement prps = conn.prepareStatement(sql);
		prps.setString(1, book.getName());
		prps.setDouble(2, book.getPrice());
		prps.setInt(3, book.getBnum());
		prps.setString(4, book.getCategory());
		prps.setString(5, book.getId());
		
		prps.executeUpdate();
		conn.close();
	}

创建UpdateBookServlet

@WebServlet("/updateBookServlet")
public class UpdateBookServlet extends HttpServlet {
	
	private BookDao bookDao = new BookDao();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String price = req.getParameter("price");
		String bnum = req.getParameter("bnum");
		String category = req.getParameter("category");
		
		Book book = new Book();
		book.setId(id);
		book.setName(name);
		book.setPrice(Double.parseDouble(price));
		book.setBnum(Integer.parseInt(bnum));
		book.setCategory(category);
		
		try {
			bookDao.updateBook(book);
			resp.sendRedirect(req.getContextPath() + "/bookServlet");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resp.setContentType("text/html;charset=utf-8");
			resp.getWriter().write("系统异常,请联系管理员 10086");
		}
	}
	
}

在这里插入图片描述
完成,不更新了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值