项目说明

本案例采用 servlet 实现，包括数据文件存储，也采用 java 对象来模拟数据库，而无需真正的数据库系统。以下是有关数据存储对象的说明。

通过类模拟后台数据库系统，可以使用 sql server 2000 或者 mySQL 的真实数据库。并通过改类进行接口处理。

/*

* 该类用来模拟虚拟数据库。用一个哈希表存储书本信息,向量模拟数据库视图

* */

package database;

import java.util.*;

public class BookDB {

/* 这个数据库哈希表和dbByTitle向量包含10个BookDetails对象

* 这个哈希表被假设为一个数据库，向量假设为这个数据库的一个视图

*/

private HashMap<Integer , BookDetails>database ;

private ArrayList al;

private static BookDB onlyInstance = null;

public BookDB () {

BookDetails book;

database = new HashMap<Integer,BookDetails>();

book = new BookDetails("201", "Duke", "",

"My Early Years: Growing up on *7",

(float)10.75, 1995, "What a cool book.");

database.put(new Integer(201), book);

book = new BookDetails("202", "Jeeves", "",

"Web Servers for Fun and Profit", (float)10.75,

1996, "What a cool book.");

database.put(new Integer(202), book);

book = new BookDetails("203", "Masterson", "Webster",

"Servlets: A Web Developers Dream Come True",

(float)17.75, 1995, "What a cool book.");

database.put(new Integer(203), book);

book = new BookDetails("204", "RealGood", "Coad",

"Moving from C++ to the Java(tm) Programming Language",

(float)10.75, 1996, "What a cool book.");

database.put(new Integer(204), book);

book = new BookDetails("205", "Novation", "Kevin",

"From Oak to Java: The Revolution of a Language",

(float)10.75, 1998, "What a cool book.");

database.put(new Integer(205), book);

book = new BookDetails("206", "Gosling", "James",

"Oak Intermediate Bytecodes", (float)10.75,

1998, "What a cool book.");

database.put(new Integer(206), book);

book = new BookDetails("207", "Thrilled", "Ben",

"The Green Project: Programming for Consumer Devices",

(float)10.75, 1998, "What a cool book");

database.put(new Integer(207), book);

book = new BookDetails("208", "Duke", "",

"100% Pure: Making Cross Platform Deployment a Reality",

(float)10.75, 1998, "What a cool book.");

database.put(new Integer(208), book);

book = new BookDetails("209", "Tru", "Itzal",

"Duke: A Biography of the Java Evangelist",

(float)10.75, 1998, "What a cool book.");

database.put(new Integer(209), book);

book = new BookDetails("210", "Sidence", "Cohen",

"I Think Not: Duke's Likeness to the Federation Insignia",

(float)10.75, 1998, "What a cool book.");

database.put(new Integer(210), book);

al = new ArrayList(database.values());

Collections.sort(al);

}

//获得该类的唯一实例（因为模拟数据库只有一个）

public static BookDB instance () {

if (onlyInstance == null)

onlyInstance = new BookDB();

return onlyInstance;

}

//根据书籍id取得书籍详细信息

public BookDetails getBookDetails(String bookId) {

Integer key = new Integer(bookId);

return (BookDetails)database.get(key);

}

public Collection getBooks() {

return al;

}

//获得书籍总类

public int getNumberOfBooks() {

return database.size();

}

}

--------------------------------------------------

/*

* 书籍类

* */

package database;

public class BookDetails implements Comparable {

//书籍在数据库中的 id

private String bookId = null;

//书标题

private String title = null;

//书名

private String firstName = null;

private String surname = null;

//价格

private float price = 0.0F;

//出版年

private int year = 0;

//书本描叙

private String description = null;

public BookDetails(String bookId, String surname, String firstName,

String title, float price, int year,

String description) {

this.bookId = bookId;

this.title = title;

this.firstName = firstName;

this.surname = surname;

this.price = price;

this.year = year;

this.description = description;

}

public String getTitle() {

return title;

}

public float getPrice() {

return price;

}

public int getYear() {

return year;

}

public String getDescription() {

return description;

}

public String getBookId() {

return this.bookId;

}

public String getFirstName() {

return this.firstName;

}

public String getSurname() {

return this.surname;

}

public int compareTo(Object o) {

BookDetails n = (BookDetails)o;

int lastCmp = title.compareTo(n.title);

return (lastCmp);

}

}