JSP第九周作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
package BOOK; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Lib { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); List<Book> blist = new ArrayList<>(); Book b1 = new Book(); b1.setBid(1); b1.setBname("Java设计"); b1.setBprice(45); blist.add(b1); Book b2 = new Book(); b2.setBid(2); b2.setBname("Android设计"); b2.setBprice(35); blist.add(b2); Book b3 = new Book(); b3.setBid(3); b3.setBname("JavaWeb设计"); b3.setBprice(55); blist.add(b3); w: while (true) { System.out.println("1.增加2.删除3.修改4.查询"); System.out.println("输入您的选择:"); int x = sc.nextInt(); int i; String n; double p; switch (x) { case 1: System.out.println("输入Book的id:"); i = sc.nextInt(); System.out.println("输入Book的name:"); n = sc.next(); System.out.println("输入Book的price:"); p = sc.nextDouble(); add(i, n, p, blist); break; case 2: System.out.println("输入Book的id:"); i = sc.nextInt(); delete(i, blist); break; case 3: System.out.println("输入Book的id:"); i = sc.nextInt(); System.out.println("输入Book的price:"); p = sc.nextDouble(); update(i, p, blist); break; case 4: System.out.println("是否查询所有:(y/n)"); String pd = sc.next(); if (pd.equals("y")) { for (Book book : blist) { System.out.println(book.toString()); } } else { System.out.println("输入Book的name:"); n = sc.next(); List<Book> blist1 = select(n, blist); for (Book book : blist1) { System.out.println(book.toString()); } } break; default: break w; } } } public static void add(int i, String n, double p, List<Book> blist) { Book b1 = new Book(); b1.setBid(i); b1.setBname(n); b1.setBprice(p); blist.add(b1); } public static void delete(int i, List<Book> blist) { for (Book b : blist) { if (b.getBid() == i) { blist.remove(b); } } } public static void update(int i, double p, List<Book> blist) { for (Book b : blist) { if (b.getBid() == i) { b.setBprice(p); } } } public static List<Book> select(String n, List<Book> blist) { List<Book> blist1 = new ArrayList<>(); for (Book b : blist) { if (b.getBname().equals(n)) { blist1.add(b); } } return blist1; } }
package BOOK; public class Book { private int bid; private String bname; private double bprice; public Book() { super(); // TODO Auto-generated constructor stub } public Book(int bid, String bname, double bprice) { super(); this.bid = bid; this.bname = bname; this.bprice = bprice; } public int getBid() { return bid; } public void setBid(int bid) { this.bid = bid; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public double getBprice() { return bprice; } public void setBprice(double bprice) { this.bprice = bprice; } @Override public String toString() { return "Book [bid=" + bid + ", bname=" + bname + ", bprice=" + bprice + "]"; } }
2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
<%-- Librsry.jsp --%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="BOOK.Book"%> <%@ page import="java.util.List"%> <%@ page import="java.util.ArrayList"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% List<Book> blist = new ArrayList<>(); Book b1 = new Book(); b1.setBid(1); b1.setBname("Java设计"); b1.setBprice(45); blist.add(b1); Book b2 = new Book(); b2.setBid(2); b2.setBname("Android设计"); b2.setBprice(35); blist.add(b2); Book b3 = new Book(); b3.setBid(3); b3.setBname("JavaWeb设计"); b3.setBprice(55); blist.add(b3); for (Book b : blist) { int bid = b.getBid(); String bname = b.getBname(); double bprice = b.getBprice(); out.print("<li>商品id " + bid + " 商品名:" + bname + " 价格:" + bprice + " </li>"); } %> </body> </html>
3.在MySQL中创建Book表,里面id,name,price,
用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书
CREATE TABLE Book ( bid INT auto_increment PRIMARY KEY, bname VARCHAR (10), bprice DECIMAL (3, 1) );
添加一个图书,
INSERT INTO Book (bid,bname, bprice) VALUES (1,'微机原理', 50);
根据名称删除图书,
DELETE FROM Book WHERE bname = '微机原理';
把所有名称是“我”开头的图书删除,
FROM Book WHERE bname LIKE '我%';
删除全部图书
DELETE FROM Book;
把20元以上的图书价格都修改为18.8,
UPDATE Book SET bprice = 18.8 WHERE bprice > 20;
查看全部图书,
SELECT * FROM Book;
查看价格高于10块钱的全部图书
SELECT * FROM Book WHERE bprice > 10;