JSP第九次作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
package entity;
public class Book {
private Integer BookId;
private String BookName;
private Integer BooKPrice;
public Book() {
}
public Book(Integer bookId, String bookName, Integer booKPrice) {
BookId = bookId;
BookName = bookName;
BooKPrice = booKPrice;
}
public Integer getBookId() {
return BookId;
}
public void setBookId(Integer bookId) {
BookId = bookId;
}
public String getBookName() {
return BookName;
}
public void setBookName(String bookName) {
BookName = bookName;
}
public Integer getBooKPrice() {
return BooKPrice;
}
public void setBooKPrice(Integer booKPrice) {
BooKPrice = booKPrice;
}
@Override
public String toString() {
return "entity.Book{" + "BookId=" + BookId + ", BookName='" + BookName + '\'' + ", BooKPrice=" + BooKPrice + '}';
}
}
import entity.Book;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
for (;;){
System.out.println("1.添加,2.修改,3.删除,4.查询,5.退出");
System.out.println("请输入指令:");
int a=input.nextInt();
switch (a) {
case 1:
add();
break;
case 2:
update();
break;
case 3:
del();
break;
case 4:
find();
break;
case 5:
return;
default:
System.out.println("输入错误");
break;
}
}
}
private static void del() {
}
private static void update() {
}
private static void add() {
}
private static void find() {
}
public List<Book> getBookList() {
Book book1=new Book(1,"图书1",20);
Book book2=new Book(2,"图书2",10);
Book book3=new Book(3,"图书3",20);
List<Book> bookList=new ArrayList<Book>();
bookList.add(book1);
bookList.add(book2);
bookList.add(book3);
return bookList;
}
}

2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
import entity.Book;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/show.do")
public class BookServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BookTest bookTest = new BookTest();
List<Book> bookList = bookTest.getBookList();
HttpSession session = request.getSession();
session.setAttribute("book",bookList);
session.setAttribute("test1",new String("Session成功"));
request.setAttribute("bookList",bookList);
request.setAttribute("test",new String("传值成功"));
request.getRequestDispatcher("/show.jsp").forward(request,response);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="<%=request.getContextPath()%>/show.do">展示所有图书</a>
</body>
</html>
<%@ page import="java.util.List" %>
<%@page import="entity.Book" %>
<%--
Created by IntelliJ IDEA.
User: 86183
Date: 2022/5/1
Time: 17:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String test = (String) request.getAttribute("test");
List<entity.Book> bookList = (List<entity.Book>) request.getAttribute("bookList");
%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>跳转成功</h1>
<table>
<tr>
<th>编号</th>
<th>书名</th>
<th>价格</th>
</tr>
<%
for (int i = 0; i < bookList.size(); i++) {
%>
<tr>
<th><%=bookList.get(i).getBookId()%></th>
<th><%=bookList.get(i).getBookName()%></th>
<th><%=bookList.get(i).getBooKPrice()%></th>
</tr>
<%
}
%>
</table>
</body>
</html>

3.在MySQL中创建Book表,里面id,name,price,
用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书

浙公网安备 33010602011771号