1.做一个图书类Book id,name,price 。 get,set访问器,构造方法2个,1个无参,1个有参
package homework;
public class lcx {
private String id;
private String name;
private double price;
public String getId() {
return id;
}
public void setId(String 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 lcx(String string, String name, double price) {
super();
this.id = string;
this.name = name;
this.price = price;
}
public lcx() {
super();
}
}
做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
package homework;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class lcxtext {
static List<lcx> listbooks = new ArrayList<lcx>();
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
creatBookList();
menu();
}
public static void creatBookList() {
// TODO Auto-generated method stub
lcx b1 = new lcx("001", "葵花宝典", 9.99);
listbooks.add(b1);
lcx b2 = new lcx("002", "十二路弹腿", 19.99);
listbooks.add(b2);
lcx b3 = new lcx("003", "玉女心经", 29.9);
listbooks.add(b3);
}
public static void menu() {
for (;;) {
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("************************");
int a = inputInt("请选择你的操作:");
switch (a) {
case 1:
addBook();
break;
case 2:
updateBook();
break;
case 3:
delBook();
break;
case 4:
findBook();
break;
case 5:
return;
default:
System.out.println("输入错误,请重新输入");
break;
}
}
}
private static void findBook() {
// TODO Auto-generated method stub
System.out.println("id 图书名称 图书价格");
for (lcx book : listbooks) {
System.out.println(book.getId() + " " + book.getName() + " "
+ book.getPrice());
}
}
private static void delBook() {
// TODO Auto-generated method stub
String id = inputStr("请输入要删除图书的id");
for (int i = 0; i < listbooks.size(); i++) {
lcx book = listbooks.get(i);
if (id.equals(book.getId())) {
listbooks.remove(i);
}
}
}
private static void updateBook() {
// TODO Auto-generated method stub
String id = inputStr("请输入要修改的图书id");
for (int i = 0; i < listbooks.size(); i++) {
lcx book = listbooks.get(i);
if (id.equals(book.getId())) {
book.setName(inputStr("请输入修改后的图书名称:"));
book.setPrice(inputDouble("请输入修改后的图书价格"));
listbooks.remove(i);
listbooks.add(i, book);
}
}
}
private static void addBook() {
// TODO Auto-generated method stub
String id = inputStr("请输入要添加的图书id");
String name = inputStr("请输入要添加的图书名称");
double price = inputDouble("请输入要添加的图书价格");
lcx book = new lcx(id, name, price);
listbooks.add(book);
}
public static String inputStr(String msg) {
System.out.print(msg);
return new Scanner(System.in).nextLine();
}
public static int inputInt(String msg) {
System.out.print(msg);
return new Scanner(System.in).nextInt();
}
public static double inputDouble(String msg) {
System.out.print(msg);
return new Scanner(System.in).nextDouble();
}
}

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="homework.lcx"%>
<%
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>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<%
List<lcx> listbooks = new ArrayList<lcx>();
Book b1 = new Book("1", "网络安全技术", 79.0);
listbooks.add(b1);
Book b2 = new Book("2", "JSP实用教程", 59.8);
listbooks.add(b2);
Book b3 = new Book("3", "JavaScript", 49.8);
listbooks.add(b3);
listbooks.add(b3); for (int i = 0; listbooks != null && i < listbooks.size(); i++) { out.print(listbooks.get(i).getId() + ": " + listbooks.get(i).getName() + " " + listbooks.get(i).getPrice() + "<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,"小王子",9.5);
insert into Book(id,name,price)values(2,"web",20);
insert into Book(id,name,price)values(3,"HTML",30);
insert into Book(id,name,price)values(4,"水浒传",28);
insert into Book(id,name,price)values(5,"梦想家",17);
把20元以上的图书价格都修改为18.8,
update 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;
浙公网安备 33010602011771号