JSP第七次作业

1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询

package entity;

 
public class Book {
   private   Integer BookId;
    private   String BookName;
   private   Integer BooKPrice;
 
    public Book() {
    }
 
    public Book(Integer bookId, String bookName, Integer booKPrice) {
        BookId = bookId;
        BookName = bookName;
        BooKPrice = booKPrice;
    }
 
    public Integer getBookId() {
        return BookId;
    }
 
    public void setBookId(Integer bookId) {
        BookId = bookId;
    }
 
    public String getBookName() {
        return BookName;
    }
 
    public void setBookName(String bookName) {
        BookName = bookName;
    }
 
    public Integer getBooKPrice() {
        return BooKPrice;
    }
 
    public void setBooKPrice(Integer booKPrice) {
        BooKPrice = booKPrice;
 
    }
 
    @Override
    public String toString() {
        return "entity.Book{" "BookId=" + BookId + ", BookName='" + BookName + '\'' ", BooKPrice=" + BooKPrice + '}';
    }
}
import entity.Book;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class BookTest {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
         
         for (;;){
                   System.out.println("1.添加,2.修改,3.删除,4.查询,5.退出");
                System.out.println("请输入指令:");<br>                   int a=input.nextInt();
                     switch (a) {
                         case 1:
                             add();
                             break;
                         case 2:
                             update();
                             break;
                         case 3:
                             del();
                             break;
                         case 4:
                             find();
                             break;
                         case 5:
                             return;
                         default:
                             System.out.println("输入错误");
                             break;
                     }
                 }
             }
 
    private static void del() {
    }
 
    private static void update() {
    }
 
    private static void add() {
    }
 
    private static void find() {
    }
 
 
    public List<Book> getBookList() {
        Book book1=new Book(1,"我的莽荒纪",47);
        Book book2=new Book(2,"吞噬星空",38);
        Book book3=new Book(3,"雪中悍刀行",41);
        List<Book> bookList=new ArrayList<Book>();
        bookList.add(book1);
        bookList.add(book2);
        bookList.add(book3);
        return bookList;
    }
}

 

2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。

import entity.Book;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
 
@WebServlet("/show.do")
public class BookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BookTest bookTest = new BookTest();
        List<Book> bookList = bookTest.getBookList();
        request.setAttribute("bookList",bookList);
        request.getRequestDispatcher("/show.jsp").forward(request,response);
    }
}
<body>
<h1>跳转成功</h1>
<table>
    <tr>
        <th>编号</th>
        <th>书名</th>
        <th>价格</th>
    </tr>
    <%
        for (int i = 0; i < bookList.size(); i++) {
 
    %>
    <tr>
        <th><%=bookList.get(i).getBookId()%></th>
        <th><%=bookList.get(i).getBookName()%></th>
        <th><%=bookList.get(i).getBooKPrice()%></th>
    </tr>
    <%
        }
    %>
 
</table>
</body>

 

 

3.在MySQL中创建Book表,里面id,name,price,

用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书

insert into book(name,price) values (1,'我的莽荒纪','47'),(1,'吞噬星空','37.5'),(1,'雪中悍刀行','41');

 

 

 

delete from book where name like'我%';

 

 

 

update book set price = 18.8 where price > 20;

 

 

 

select * from book;

select * from book where price > 10;

 

posted @ 2022-05-01 21:28  Vending  阅读(29)  评论(0编辑  收藏  举报