JSP第九周练习

1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参

 1 package com.study.book;
 2 
 3 public class Book {
 4     private Integer id;
 5     private String name;
 6     private Double price;
 7     
 8     public Book(Integer id, String name, Double price) {
 9         super();
10         this.id = id;
11         this.name = name;
12         this.price = price;
13     }
14     public Book() {
15         super();
16         // TODO Auto-generated constructor stub
17     }
18     @Override
19     public String toString() {
20         return "Book [id=" + id + ", name=" + name + ", price=" + price + "]";
21     }
22     public Integer getId() {
23         return id;
24     }
25     public void setId(Integer id) {
26         this.id = id;
27     }
28     public String getName() {
29         return name;
30     }
31     public void setName(String name) {
32         this.name = name;
33     }
34     public Double getPrice() {
35         return price;
36     }
37     public void setPrice(Double price) {
38         this.price = price;
39     }
40     
41     
42 
43 }
Book

 

1.1做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询

  1 package com.study.book;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 import java.util.Scanner;
  6 
  7 public class BookTest {
  8     private static List<Book> books = new ArrayList<Book>();
  9     private static Book book = new Book();
 10     static {
 11         books.add(new Book(1001, "《童话故事》", 23.55));
 12         books.add(new Book(1002, "《三国演义》", 87.87));
 13         books.add(new Book(1003, "《人世间》", 200.00));
 14     }
 15     private static Scanner sc = new Scanner(System.in);
 16 
 17     public static void main(String[] args) {
 18         menu();
 19 
 20     }
 21 
 22     private static void menu() {
 23         while (true) {
 24             System.out.println("1.增加图书");
 25             System.out.println("2.删除图书");
 26             System.out.println("3.修改图书");
 27             System.out.println("4.查询图书");
 28             System.out.println("5.全部图书");
 29             System.out.println("0.退出系统");
 30             System.out.println("请输入您要执行的操作序号:");
 31             int sw = sc.nextInt();
 32             switch (sw) {
 33             case 1:
 34                 addBook();
 35                 break;
 36             case 2:
 37                 deleteBook();
 38                 break;
 39             case 3:
 40                 updataBook();
 41                 break;
 42             case 4:
 43                 selectBook();
 44                 break;
 45             case 5:
 46                 selectBooks();
 47                 break;
 48             case 0:
 49                 return;
 50             default:
 51                 break;
 52             }
 53 
 54         }
 55 
 56     }
 57 
 58     private static void selectBooks() {
 59         tittleShow("查询所有图书");
 60         for (Book book : books) {
 61             System.out.println(book);
 62         }
 63     }
 64 
 65     private static void selectBook() {
 66         tittleShow("查询图书");
 67         System.out.println("请输入图书id:");
 68         int bId = sc.nextInt();
 69         book = haveId(bId);
 70         if (book != null) {
 71             System.out.println(book);
 72         }else{
 73             System.out.println("未找到该id!");
 74         }
 75     }
 76 
 77     private static void updataBook() {
 78         tittleShow("修改图书");
 79         System.out.println("请输入图书id:");
 80         int bId = sc.nextInt();
 81         book = haveId(bId);
 82         if (book != null) {
 83             System.out.println("请输入新的图书id:");
 84             bId = sc.nextInt();
 85             System.out.println("请输入新的图书名称:");
 86             String name = "《" + sc.next() + "》";
 87             System.out.println("请输入新的图书价格:");
 88             double price = sc.nextDouble();
 89             System.out.println(book);
 90             System.out.println("↓");
 91             book.setId(bId);
 92             book.setName(name);
 93             book.setPrice(price);
 94             System.out.println(book);
 95             System.out.println("修改成功!");
 96         } else {
 97             System.out.println("未找到该id!");
 98         }
 99     }
100 
101     private static void deleteBook() {
102         tittleShow("删除图书");
103         System.out.println("请输入图书id:");
104         int bId = sc.nextInt();
105         book = haveId(bId);
106         if (book != null) {
107             books.remove(book);
108             System.out.println("删除成功!");
109         } else {
110             System.out.println("未找到该id!");
111         }
112     }
113 
114     private static void addBook() {
115         tittleShow("添加图书");
116         int bId;
117         System.out.println("请输入图书id:");
118         bId = sc.nextInt();
119         while (haveId(bId) != null){
120             System.out.println("图书id重复,请重新输入id:");
121             bId = sc.nextInt();
122         }
123         System.out.println("请输入图书名称:");
124         String bName = "《" + sc.next() + "》";
125         System.out.println("请输入图书价格:");
126         double bPrice = sc.nextDouble();
127         books.add(new Book(bId, bName, bPrice));
128         System.out.println("添加成功,图书编号: " + bId);
129     }
130 
131     private static Book haveId(int bId) {
132         for (Book b : books) {
133             if (bId == b.getId()) {
134                 return b;
135             }
136         }
137         return null;
138     }
139 
140     private static void tittleShow(String tittle) {
141         System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
142         System.out.println("☆☆☆☆☆☆☆" + tittle + "☆☆☆☆☆☆☆");
143     }
144 
145 }
BookTest

 

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

 1 <%@page import="com.study.book.Book"%>
 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 3 <%
 4     String path = request.getContextPath();
 5     String basePath = request.getScheme() + "://"
 6             + request.getServerName() + ":" + request.getServerPort()
 7             + path + "/";
 8 %>
 9 
10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
11 <html>
12 <head>
13 <base href="<%=basePath%>">
14 
15 <title>My JSP 'Show.jsp' starting page</title>
16 
17 </head>
18 
19 <body>
20     <%
21         List<Book> books = new ArrayList<Book>();
22         books.add(new Book(1001, "《童话故事》", 23.55));
23         books.add(new Book(1002, "《三国演义》", 87.87));
24         books.add(new Book(1003, "《人世间》", 200.00));
25         for (int i = 0; i < books.size(); i++) {
26             out.println(books.get(i));
27     %><br />
28     <%
29         out.println("\n");
30         }
31     %>
32 </body>
33 </html>
Show

 

 

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

 1 -- 有则删除表
 2 DROP TABLE IF EXISTS t_book;
 3 -- 创建表
 4 CREATE TABLE t_book(
 5 id BIGINT PRIMARY KEY,
 6 name VARCHAR(20) NOT NULL,
 7 price DOUBLE(5,2) NOT NULL
 8 );
 9 -- 查看表结构
10 DESC t_book;
11 -- 添加数据
12 INSERT INTO t_book(id,name,price) 
13 VALUES(1001,'我的童话',22.3),(1002,'我的酒馆',12.3),(1003,'鬼吹灯',32.3),(1011,'你好',22.3),(1012,'活着',19.9),(1013,'人世间',2.9);
14 -- 查询所有数据
15 SELECT id,name,price FROM t_book;
16 -- 添加一个图书,
17 INSERT INTO t_book(id,name,price) VALUES(1101,'谁的童话',9.3);
18 -- 根据名称删除图书,
19 DELETE FROM t_book WHERE name = '谁的童话';
20 -- 把所有名称是“我”开头的图书删除,
21 DELETE FROM t_book WHERE name LIKE '我%';
22 -- 删除全部图书,
23 DELETE FROM t_book;
24 -- 把20元以上的图书价格都修改为18.8,
25 UPDATE t_book SET price = 18.8 WHERE price > 20;
26 -- 查看全部图书,
27 SELECT id,name,price FROM t_book;
28 -- 查看价格高于10块钱的全部图书
29 SELECT id,name,price FROM t_book WHERE price >10;
t_book


用命令实现,
添加一个图书,

 

 


根据名称删除图书,

 

 


把所有名称是“我”开头的图书删除,

 

 


删除全部图书,

 

 


把20元以上的图书价格都修改为18.8,

 

 


查看全部图书,

 

 


查看价格高于10块钱的全部图书

 

posted @ 2022-04-30 22:40  L'童话故事  阅读(31)  评论(0编辑  收藏  举报