书本信息的增删改查案例
书城的全部书本查询
基本思路即为findAllBook(),很简单
主要代码如下:
dao:
public List<Book> getAllBook() throws SQLException {
QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource());
return qr.query("select * from book",new BeanListHandler<Book>(Book.class));
}
service:
BookDao bd = new BookDaoImpl();
public List<Book> getAllBook() {
try {
return bd.getAllBook();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
controller:
package controller;
import pojo.Book;
import service.BookService;
import service.impl.BookServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class BookListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取表单数据
resp.setContentType("text/html;charset=UTF-8");
//调用业务逻辑
BookService bs = new BookServiceImpl();
List<Book> bookList = bs.getAllBook();
if (bookList!=null){
req.setAttribute("books",bookList);
//分发转向
req.getRequestDispatcher("/admin/products/list.jsp").forward(req,resp);
}
}
}
jsp:
<c:forEach items="${books}" var="book">
<tr onmouseover="this.style.backgroundColor = 'white'"
onmouseout="this.style.backgroundColor = '#F5FAFE';">
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="23">${book.id}</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="18%">${book.name}</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="8%">${book.price}</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="8%">${book.pnum}</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">${book.description}</td>
<td align="center" style="HEIGHT: 22px" width="7%"><a
href="/servlet/findBookByIdServlet?id=${book.id}">
<img
src="${pageContext.request.contextPath}/admin/images/i_edit.gif"
border="0" style="CURSOR: hand"> </a>
</td>
<td align="center" style="HEIGHT: 22px" width="7%"><a
href="#">
<img
src="${pageContext.request.contextPath}/admin/images/i_del.gif"
width="16" height="16" border="0" style="CURSOR: hand">
</a>
</td>
</tr>
</c:forEach>
书本的添加
图示思路:
注意:添加成功的servlet里转发的是查询的servlet,数据才能更新
servlet:
package controller;
import pojo.Book;
import service.BookService;
import service.impl.BookServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class AddBookServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取表单数据
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
String price = req.getParameter("price");
String number = req.getParameter("pnum");
String category = req.getParameter("category");
String description = req.getParameter("description");
Book book =