1.创建数据库
2使用JDBC连接数据库
3. 创建图书实体类BookBean,封装图书信息属性
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String publish;
private Double price;
private String author;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(Integer id, String name, String publish, Double price,
String author) {
super();
this.id = id;
this.name = name;
this.publish = publish;
this.price = price;
this.author = author;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
4 创建数据库访问类,bookDao,在该类中封装了各种类型的对数据库访问的方法
public class BookDao {
// type 查询类型 1-5分别全部查询 按编码查询。按名字查询。出版社 作者
// name 查询的值
public List<Book> findBookByBook(int type, Book book) {
List<Book> books = new ArrayList<Book>();
// 声明sql
String sql = "select * from book ";
// 构建
Connection conn = DBcon.getCon();
try {
PreparedStatement pt = null;
if (type == 1) {
pt = conn.prepareStatement(sql);
} else if (type == 2) {
sql = sql + "where id =?";
pt = conn.prepareStatement(sql);
// 占位符
pt.setInt(1, book.getId());
} else if (type == 3) {
sql = sql + "where name like ?";
pt = conn.prepareStatement(sql);
// 占位符
pt.setString(1, book.getName() + "%");
} else if (type == 4) {
sql = sql + "where publish like ?";
pt = conn.prepareStatement(sql);
// 占位符
pt.setString(1, book.getPublish()+"%");
} else if (type == 5) {
sql = sql + "where author like ?";
pt = conn.prepareStatement(sql);
// 占位符
pt.setString(1, book.getAuthor()+"%");
}
// 生成结果
ResultSet rs = pt.executeQuery();
// 填充对象
while (rs.next()) {
Book b = new Book();
b.setId(rs.getInt(1));
b.setName(rs.getString(2));
b.setPublish(rs.getString(3));
b.setPrice(rs.getDouble(4));
b.setAuthor(rs.getString(5));
books.add(b);
}
rs.close();
pt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return books;
}
}
5
67. 在图书结果显示页面,先判断是否有图书信息。如果有,则使用列表显示;没有则显示提示信息。
public class BookServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//页面接受参数
int type =1;
if(request.getParameter("type")!=null){
type= Integer.parseInt(request.getParameter("type"));
}
//填充一个book
Book book = new Book();
if(type==2){
if(request.getParameter("name")!=null){
book.setId(Integer.parseInt(request.getParameter("name")));
}
}else if( type==3){
book.setName(request.getParameter("name"));
}else if(type==4){
book.setPublish(request.getParameter("name"));
}else if(type==5){
book.setAuthor(request.getParameter("name"));
}
//实例化dao 调用方法
BookDao dao = new BookDao();
List<Book> books= dao.findBookByBook(type, book);
//传递list到下一个页面
request.setAttribute("books", books);
request.getRequestDispatcher("result.jsp").forward(request, response);
}
7
传递到图书结果显示页面(result.jsp 用EL表达式来实现和JSTL来实现)
完成: