Javaweb Book Mall Design Library Module (4) _java

Source: Internet
Author: User

This article is followed by a book mall classification module for your reference, the specific content as follows

1. Create related class

Cn.itcast.bookstore.book
Domain:book
Dao:bookdao
Service:bookservice
Web.servle:BookServlet

Book

public class Book {
 private String bid;
 Private String bname;
 private double price;
 Private String author;
 Private String image;
 Private Category Category;
 Private Boolean del;


Bookdao

public class Bookdao {private Queryrunner QR = new Txqueryrunner (); /** * Query All Books * @return/public list<book> findall () {try {String sql = ' SELECT * from book where del=false
 ";
 return qr.query (SQL, New Beanlisthandler<book> (Book.class));
 catch (SQLException e) {throw new RuntimeException (e); /** * by Category * @param CID * @return/Public list<book> findbycategory (string cid) {try {String sql = "SELECT * from book where cid=?"
 and Del=false ";
 return qr.query (SQL, New beanlisthandler<book> (Book.class), CID);
 catch (SQLException e) {throw new RuntimeException (e); /** * Load Method * @param bid * @return/public book findbybid (String Bid) {try {* * * * We need to save the category in the book object
 Information */String sql = "SELECT * from book where bid=?";
 map<string,object> map = qr.query (sql, New Maphandler (), Bid);
 * * Use a map to map out two objects, and then create a relationship between the two objects!
 * * Category Category = Commonutils.tobean (map, Category.class); Book book = CommonuTils.tobean (map, Book.class);
 Book.setcategory (category);
 return book;
 catch (SQLException e) {throw new RuntimeException (e); /** * Query the number of books under the specified category * @param CID * @return/public int getcountbycid (string cid) {try {string sql = Sele
 CT Count (*) from the book where cid=? ";
 Number CNT = (number) qr.query (SQL, New Scalarhandler (), CID);
 return Cnt.intvalue ();
 catch (SQLException e) {throw new RuntimeException (e); }/** * Add Books * @param book * */public void Add (????
 ?,?)"; Object[] params = {book.getbid (), Book.getbname (), Book.getprice (), Book.getauthor (), Book.getimage (),
 Book.getcategory (). Getcid ()};
 Qr.update (sql, params);
 catch (SQLException e) {throw new RuntimeException (e); /** * Delete Books * @param bid/public void Delete (string bid) {try {string sql = Update Book set del=true wher
 E bid=? ";
 Qr.update (SQL, Bid); catch (SQLException e) {throw new RuntimeException (e);
 } public void edit (book book) {try {String sql = ' Update book set bname=?, price=?,author=?, image=?, cid=?
 where bid=? "; Object[] params = {book.getbname (), Book.getprice (), Book.getauthor (), Book.getimage (), Book.getcategory (). GetCid (),
 Book.getbid ()};
 Qr.update (sql, params);
 catch (SQLException e) {throw new RuntimeException (e);

 }
 }
}

Bookservice

public class Bookservice {
 private Bookdao Bookdao = new Bookdao ();

 /**
 * Query All Books
 * @return
 /Public list<book> FindAll () {return
 bookdao.findall ();
 }

 /**
 * Search Books by Category *
 @param CID
 * @return
 /Public list<book> findbycategory (String CID) { C17/>return bookdao.findbycategory (CID);
 }

 Public book load (String Bid) {return
 bookdao.findbybid (BID);
 }

 /**
 * Add Books * @param book * * */public
 void Add (title book) {
 bookdao.add);
 }

 public void Delete (String bid) {
 bookdao.delete (BID);
 }

 public void edit [book book] {
 bookdao.edit (book);
 }
}

Bookservlet

public class Bookservlet extends Baseservlet {private Bookservice bookservice = new Bookservice ();
 Public String Load (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {/* * 1. Get the parameter bid * 2. The query gets book * 3.
 Save, forward to desc.jsp/Request.setattribute ("book", Bookservice.load (Request.getparameter ("bid"));
 return "f:/jsps/book/desc.jsp";
 /** * Query All Books * @param request * @param response * @return * @throws servletexception * @throws IOException * * Public String FindAll (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {R
 Equest.setattribute ("Booklist", Bookservice.findall ());
 return "f:/jsps/book/list.jsp"; /** * Query by Category * @param request * @param response * @return * @throws servletexception * @throws ioexception * * p Ublic String findbycategory (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {String cid = REQUEST.GETPARameter ("CID");
 Request.setattribute ("Booklist", Bookservice.findbycategory (CID));
 return "f:/jsps/book/list.jsp";

 }
}

2. Search All Books

Process: left.jsp (All categories)-> Bookservlet#findall ()->/jsps/book/list.jsp

3. Search Books by Category

Process: left.jsp-> bookservlet#findbycategory ()-> list.jsp

4. Query details (load)

Process: list.jsp (click on a book)-> bookservlet#load ()-> desc.jsp

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.