JSP第9次作业
1.
package BookPackage; public class Book { int id; String name; double price; public Book() { super(); } public Book(int id, String name, double price) { super(); this.id = id; this.name = name; this.price = 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; } }
package BookPackage; import java.util.ArrayList; import java.util.Scanner; public class main { static ArrayList<Book> booklist=new ArrayList<Book>(); static Scanner sc=new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub Book book1=new Book(100,"1",20); Book book2=new Book(200,"2",30); Book book3=new Book(300,"3",10); booklist.add(book1); booklist.add(book2); booklist.add(book3); for (int i = 0; i <booklist.size(); i++) { Book b= booklist.get(i); System.out.println("图书编号:"+b.getId()+",图书名称:"+b.getName()+",图书价格"+b.getPrice()); } System.out.println("==========================="); shouye(); } public static void shouye() { System.out.println("请输入你需要的选项:"); System.out.println("1.添加图书"); System.out.println("2.删除图书"); System.out.println("3.修改图书"); System.out.println("4.查询图书"); System.out.println("==========================="); caidan(); } public static void caidan() { int num=sc.nextInt(); switch (num) { case 1: tianjia(); break; case 2: shanchu(); break; case 3: xiugai(); break; case 4: chaxun(); break; default: break; } } private static void chaxun() { // TODO Auto-generated method stub System.out.println("请输入要查询图书名称:"); String bookname=sc.next(); for (int i = 0; i < booklist.size(); i++) { if(booklist.get(i).getName().equals(bookname)){ System.out.println("==========================="); Book b= booklist.get(i); System.out.println("图书编号:"+b.getId()+",图书名称:"+b.getName()+",图书价格"+b.getPrice()); } } } private static void xiugai() { // 修改图书信息 System.out.println("请输入要修改的图书id:"); int bookid=sc.nextInt(); for (int i = 0; i < booklist.size(); i++) { if(booklist.get(i).getId()==bookid){ booklist.remove(i); System.out.println("请输入新的图书编号:"); int id=sc.nextInt(); System.out.println("请输入新的图书名称:"); String name=sc.next(); System.out.println("请输入新的图书价格:"); double price=sc.nextDouble(); Book newbook=new Book(id,name,price); booklist.add(newbook); } } System.out.println("图书信息修改完成!"); System.out.println("==========================="); mulu(); shouye(); } private static void shanchu() { // 删除图书 System.out.println("请输入要删除的图书名称:"); String bookname=sc.next(); for (int i = 0; i < booklist.size(); i++) { if(booklist.get(i).getName().equals(bookname)){ booklist.remove(i); } } System.out.println("删除成功!"); System.out.println("==========================="); mulu(); shouye(); } private static void tianjia() { //添加图书 System.out.println("请输入图书编号:"); int id=sc.nextInt(); System.out.println("请输入图书名称:"); String name=sc.next(); System.out.println("请输入图书价格:"); double price=sc.nextDouble(); Book newbook=new Book(id,name,price); booklist.add(newbook); System.out.println("新图书添加完成!"); System.out.println("==========================="); mulu(); shouye(); } private static void mulu() { // 更新后图书目录 for (int i = 0; i <booklist.size(); i++) { Book b= booklist.get(i); System.out.println("图书编号:"+b.getId()+",图书名称:"+b.getName()+",图书价格"+b.getPrice()); } System.out.println("==========================="); } }
2.
<%@ page import="one.Book" %> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% ArrayList<Book> list=new ArrayList<Book>(); Book b1=new Book("1","1",73); Book b2=new Book("2","2",40); Book b3=new Book("3","3",39); list.add(b1); list.add(b2); list.add(b3); for (int i = 0; i<list.size(); i++) { out.print("编号为:"+list.get(i).getId()+" ,名称为:" +list.get(i).getName()+" ,价格为:"+list.get(i).getPrice()+"<br>"); } %> </body> </html>
3.
在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,"一",10); insert into Book(id,name,price)values(2,"二",20); insert into Book(id,name,price)values(3,"三",30); insert into Book(id,name,price)values(4,"四",40); insert into Book(id,name,price)values(5,"五",30); 把20元以上的图书价格都修改为15, update book set price=15 where price>20; 查看全部图书 select * from book; 查看价格高于10块的全部图书 select * from book where price>10; 把所有名称是“二”开头的图书删除 delete from book where name like '二%'; 删除全部图书 delete from book;