JSP第七次作业

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

 

package glg;
//做一个图书类Book id,name,price 
//get,set访问器,构造方法2个,1个无参(Super),1个有参(Fields)
public class book {
    int id;
    String name;
    double price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public book(int id, String name, double price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }


    public book() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
        return "book [id=" + id + ", name=" + name + ", price=" + price + "]";
    }

}
package glg;

//做一个测试类,在main中创建3个图书对象,放到list集合中
//做一个菜单,可以添加,删除,修改,查询
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class main {
    static java.util.List<book> list = new ArrayList<book>();
    static {//static是静态修饰符,修饰的成员可以通过类名加'.'进直接访问
        book b1 = new book(1, "book1", 11.11);
        book b2 = new book(2, "book2", 22.22);
        book b3 = new book(3, "book3", 33.33);
        list.add(b1);
        list.add(b2);
        list.add(b3);
    }

    /**
     * @param args
     * @return
     */

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        while (true) {
            System.out.println("主页面");
            System.out.println("1.添加");
            System.out.println("2.删除");
            System.out.println("3.修改");
            System.out.println("4.查询");
            System.out.println("5.退出程序");
            System.out.println("请选择1-5");
            Scanner scanner = new Scanner(System.in);
            int i = scanner.nextInt();
            switch (i) {
            case 1:
                addbook();
                System.out.println("是否返回主界面Y/N?");
                String s = scanner.next();
                if (s.equals("Y")) {
                    break;
                } else {
                    return;
                }
            case 2:
                delBoook();
                System.out.println("是否返回主界面Y/N?");
                s = scanner.next();
                if (s.equals("Y")) {
                    break;
                } else {
                    return;
                }
            case 3:
                update();
                System.out.println("是否返回主界面Y/N?");
                s = scanner.next();
                if (s.equals("Y")) {
                    break;
                } else {
                    return;
                }
            case 4:
                search();
                System.out.println("是否返回主界面Y/N?");
                s = scanner.next();
                if (s.equals("Y")) {
                    break;
                } else {
                    return;
                }
            case 5:
                System.out.println("退出成功");
                return;
            }

        }
    }

    private static void search() {
        // TODO Auto-generated method stub
        for (book book4 : list) {
            System.out.println(book4);
        }
    }

    private static void update() {
        // TODO Auto-generated method stub
        System.out.println("请输入修改的图书id");
        int update = new Scanner(System.in).nextInt();
        for (book book : list) {
            if (update == book.getId()) {
                System.err.println("请输入名称");
                String b1 = new Scanner(System.in).next();
                System.err.println("请输入价格");
                double c1 = new Scanner(System.in).nextDouble();
                book.setName(b1);
                book.setPrice(c1);
                System.out.println("修改成功");
            }
        }

    }

    private static void delBoook() {
        System.out.println("请输入你要删除的图书id");
        int id = new Scanner(System.in).nextInt();
        try {
            for (book book : list) {
                if (list.size() != 0) {
                    if (id == book.getId()) {
                        list.remove(book);
                        System.out.println("删除成功");
                    }
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    private static void addbook() {
        // TODO Auto-generated method stub
    //构造一个Scanner类的对象来获取用户的输入
        Scanner scanner = new Scanner(System.in);
        System.err.println("请输入id");
        int id = scanner.nextInt();
        System.err.println("请输入名称");
        String name = scanner.next();
        System.err.println("请输入价格");
        double price = scanner.nextDouble();

        book book = new book(id, name, price);
        list.add(book);
        System.out.println("添加成功");

    }

}

 

 

 

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

<%@page import="glg.book"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
  </head>
  <body>
   <% 
      List<book> list=new ArrayList<book>();
        book b1 = new Book(1, "book1", 11.11);
        book b2 = new Book(2, "book2", 22.22);
        book b3 = new Book(3, "book3", 33.33);
        list.add(b1);
        list.add(b2);
        list.add(b3);
        for(book book4:list){
        out.println(book4+"<br>");
        }
   %>
  </body>
</html>

 

 

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

用命令实现,
添加5个图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书。
insert into 表名(列,列,列)values(值,值,值)
delete from 表名 where 删除条件
update 表名 set列名=更新值 where 更新条件
select 列,列,列 …… from 表 where 查询条件

 

在MySQL中创建Book表,里面id,name,price        create table book(
    -> id int(10) auto_increment primary key,
    -> name varchar(50),
    -> price double(5,1));
添加5个图书
insert into Book(id,name,price)values(1,"我们仨",20);
insert into Book(id,name,price)values(2,"西游记",30);
insert into Book(id,name,price)values(3,"水浒传",30);
insert into Book(id,name,price)values(4,"三国演义",30);
insert into Book(id,name,price)values(5,"红楼梦",30);
把20元以上的图书价格都修改为18.8update book set price=18.8 where price>20;
查看全部图书
 select * from book;
查看价格高于10块的全部图书
 select * from book where price>10;
把所有名称是“我”开头的图书删除
 delete from book where name like '我%';
删除全部图书
 delete from book;

 

posted @ 2022-05-01 10:38  Song、  阅读(6)  评论(0编辑  收藏  举报