jsp第九次作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
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,"三国演义",20); Book book2=new Book(200,"红楼梦",30); Book book3=new Book(300,"水浒传",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.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
<%@page import="book.Book"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head></head> <body> 图书:<br> <% ArrayList<Book> list=new ArrayList<Book>(); Book book1=new Book(1,"骆驼祥子",55); Book book2=new Book(2,"我的爱人",18); Book book3=new Book(3,"老人与海",35); list.add(book1); list.add(book2); list.add(book3); for (int i = 0; i < list.size(); i++) { Book b=list.get(i); int id=b.getId(); String name=b.getName(); double price=b.getPrice(); out.print("id:"+id+" , 书名为:"+name+" ,价格为:"+price); %><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 查询条件