生活随笔
收集整理的這篇文章主要介紹了
图书分类查询 Demo
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
視頻演示:
JavaWeb實現
源碼下載
資源下載 https://download.csdn.net/download/u014565127/12130742
目錄結構s
核心代碼展示
link.jsp<body><h1>鏈接頁面</h1><a href="<c:url value='/BookServlet?method=findAll'/>">查詢所有分類</a><br><a href="<c:url value='/BookServlet?method=findByCategory&category=1'/>">查詢SE分類</a><br><a href="<c:url value='/BookServlet?method=findByCategory&category=2'/>">查詢EE分類</a><br><a href="<c:url value='/BookServlet?method=findByCategory&category=3'/>">查詢框架分類</a><br></body>
show.jsp<body><h1 align="center">圖書列表</h1><table align="center" border="1" width="50%"><tr><th>書名</th><th>單價</th><th>分類</th></tr><c:forEach items="${bookList }" var="book"><tr><td>${book.bname }</td><td>${book.price }</td><c:choose><c:when test="${book.category eq 1 }"><td style="color:red">JavaSE</td></c:when><c:when test="${book.category eq 2 }"><td style="color:blue">JavaEE</td></c:when><c:when test="${book.category eq 3 }"><td style="color:green">JavaFramework</td></c:when></c:choose></tr></c:forEach></table>
</body>
BookServletpublic class BookServlet extends BaseServlet {private BookDao bookDao = new BookDao();public String findAll(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {request.setAttribute("bookList", bookDao.findAll());return "/show.jsp";}public String findByCategory(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {String value =request.getParameter("category");int category = Integer.parseInt(value);request.setAttribute("bookList", bookDao.findByCategory(category));return "/show.jsp";}
}
BookDaopublic class BookDao {private QueryRunner qr = new TxQueryRunner();public List<Book> findAll() {try {String sql = "select * from t_book";return qr.query(sql, new BeanListHandler<Book>(Book.class));} catch (SQLException e) {throw new RuntimeException(e);}}public List<Book> findByCategory(int category) {try {String sql = "select * from t_book where category=?";return qr.query(sql, new BeanListHandler<Book>(Book.class),category);} catch (SQLException e) {throw new RuntimeException(e);}}
}
Book.javapackage com.veeja.book.domain;public class Book {private String bid;private String bname;private double price;private int category;public String getBid() {return bid;}public void setBid(String bid) {this.bid = bid;}public String getBname() {return bname;}public void setBname(String bname) {this.bname = bname;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getCategory() {return category;}public void setCategory(int category) {this.category = category;}public String toString() {return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price+ ", category=" + category + "]";}}
END.
總結
以上是生活随笔為你收集整理的图书分类查询 Demo的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。