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");
		}
	}
	
}

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

发布了17 篇原创文章 · 获赞 4 · 访问量 4278
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览