在实际Web开发中,一般将访问数据库的操作放到特定的类,JSP作为表示层,
可以在表示层中调用这些类提供的操作数据库的方法。
这种Java类叫做DAO(Data Access Object)专门进行数据库的访问,可以使用VO(Value Object)配合DAO来使用。
之前的JavaBean对象就可以当作VO。
p 103 第5章 JSP和JavaBean 第3题 bookquery 使用DAO和VO实现 MySQL

制作一个查询页面,输入两个数字,显示价格在两个数字之间的图书信息。使用DAO和VO实现

books.jsp
<%@ page language="java" import="java.util.*,entity.*,DAO.*"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
BookDao bookdao = new BookDao();
ArrayList books = bookdao.queryBooks();
%>
<center>
<form action="books.jsp" method="post">
价格:<input type="text" name="p1" />-<input type="text" name="p2" />
<input type="submit" value="提交" />
</form>
</center>
<table align="center" width="80%" border="1" cellspacing="0"
cellpadding="5">
<tr>
<td>书号</td>
<td>书名</td>
<td>价格</td>
</tr>
<%
String p1 = request.getParameter("p1");
String p2 = request.getParameter("p2");
if (books != null & p1 != null && p2 != null) {
int a = 0, b = 0;
try {
a = Integer.parseInt(p1);
b = Integer.parseInt(p2);
} catch (Exception e) {
}
for (int i = 0; i < books.size(); i++) {
Book b1 = (Book) books.get(i);
if (b1.getPrice() > a && b1.getPrice() < b) {
out.println("<tr><td>" + b1.getId() + "</td><td>"
+ b1.getName() + "</td><td>" + b1.getPrice()
+ "</td></tr>");
}
}
} else {
out.println("<tr><td colspan='3'>没有符合要求的图书</td></tr>");
}
%>
</table>
</body>
</html>
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
BookDao bookdao = new BookDao();
ArrayList books = bookdao.queryBooks();
%>
<center>
<form action="books.jsp" method="post">
价格:<input type="text" name="p1" />-<input type="text" name="p2" />
<input type="submit" value="提交" />
</form>
</center>
<table align="center" width="80%" border="1" cellspacing="0"
cellpadding="5">
<tr>
<td>书号</td>
<td>书名</td>
<td>价格</td>
</tr>
<%
String p1 = request.getParameter("p1");
String p2 = request.getParameter("p2");
if (books != null & p1 != null && p2 != null) {
int a = 0, b = 0;
try {
a = Integer.parseInt(p1);
b = Integer.parseInt(p2);
} catch (Exception e) {
}
for (int i = 0; i < books.size(); i++) {
Book b1 = (Book) books.get(i);
if (b1.getPrice() > a && b1.getPrice() < b) {
out.println("<tr><td>" + b1.getId() + "</td><td>"
+ b1.getName() + "</td><td>" + b1.getPrice()
+ "</td></tr>");
}
}
} else {
out.println("<tr><td colspan='3'>没有符合要求的图书</td></tr>");
}
%>
</table>
</body>
</html>

Book.java
package entity;
public class Book {
private int id;
private String name;
private double price;
public Book() {
}
public Book(int id, String name, double price) {
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;
}
}

BookDao.java
package DAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import entity.Book;
public class BookDao {
public ArrayList queryBooks() throws SQLException {
ArrayList books = new ArrayList();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/product", "root", "root");
Statement stat = conn.createStatement();
String sql = "select * from Book where 1=1 ";
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setPrice(rs.getDouble("price"));
books.add(book);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return books;
}
}







=====================================================================================


