2、在数据库中建立表格T_BOOK(BOOKID,BOOKNAME,BOOKPRICE),插入一些记录。制作一个查询页面,输入两个数字,显示价格在这两个数字之间的图书信息。使用PreparedStatment命令执行sql,要求使用Java Bean技术的DAO和VO模式实现。
时间: 2024-02-11 16:06:40 浏览: 89
好的,这是一个关于数据库的问题,我会尽力回答。首先,我们需要在数据库中建立表格T_BOOK,可以使用如下的SQL语句:
```
CREATE TABLE T_BOOK (
BOOKID INT NOT NULL AUTO_INCREMENT,
BOOKNAME VARCHAR(50) NOT NULL,
BOOKPRICE DECIMAL(10,2) NOT NULL,
PRIMARY KEY (BOOKID)
);
```
接着,我们需要插入一些记录:
```
INSERT INTO T_BOOK (BOOKNAME, BOOKPRICE) VALUES ('Java入门', 68.00);
INSERT INTO T_BOOK (BOOKNAME, BOOKPRICE) VALUES ('Spring实战', 98.00);
INSERT INTO T_BOOK (BOOKNAME, BOOKPRICE) VALUES ('MyBatis深入浅出', 88.00);
INSERT INTO T_BOOK (BOOKNAME, BOOKPRICE) VALUES ('Python编程', 78.00);
INSERT INTO T_BOOK (BOOKNAME, BOOKPRICE) VALUES ('数据结构与算法', 58.00);
```
然后,我们可以使用Java Bean技术的DAO和VO模式实现查询功能。首先,我们创建一个Book类作为VO(Value Object):
```
public class Book {
private int bookId;
private String bookName;
private double bookPrice;
// 省略getter和setter方法
}
```
然后,我们创建一个BookDao类作为DAO(Data Access Object):
```
public class BookDao {
public List<Book> queryBooks(double minPrice, double maxPrice) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Book> bookList = new ArrayList<Book>();
try {
conn = DbUtil.getConnection();
String sql = "SELECT BOOKID, BOOKNAME, BOOKPRICE FROM T_BOOK WHERE BOOKPRICE >= ? AND BOOKPRICE <= ?";
pstmt = conn.prepareStatement(sql);
pstmt.setDouble(1, minPrice);
pstmt.setDouble(2, maxPrice);
rs = pstmt.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setBookId(rs.getInt("BOOKID"));
book.setBookName(rs.getString("BOOKNAME"));
book.setBookPrice(rs.getDouble("BOOKPRICE"));
bookList.add(book);
}
} finally {
DbUtil.close(rs, pstmt, conn);
}
return bookList;
}
}
```
其中,DbUtil是一个数据库工具类,负责获取数据库连接、关闭连接等操作。可以参考如下的实现:
```
public class DbUtil {
// 数据库连接参数
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USER = "root";
private static final String PASSWORD = "root";
// 获取数据库连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
// 关闭数据库连接
public static void close(ResultSet rs, Statement stmt, Connection conn) {
try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
}
}
```
最后,我们可以在Servlet中调用BookDao的queryBooks方法,获取符合条件的图书信息,并将其展示在JSP页面上。
```
public class BookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
double minPrice = Double.parseDouble(request.getParameter("minPrice"));
double maxPrice = Double.parseDouble(request.getParameter("maxPrice"));
BookDao bookDao = new BookDao();
try {
List<Book> bookList = bookDao.queryBooks(minPrice, maxPrice);
request.setAttribute("bookList", bookList);
request.getRequestDispatcher("/book_list.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
在JSP页面上,我们可以使用JSTL标签库展示查询结果:
```
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
<tr>
<th>书名</th>
<th>价格</th>
</tr>
<c:forEach items="${bookList}" var="book">
<tr>
<td>${book.bookName}</td>
<td>${book.bookPrice}</td>
</tr>
</c:forEach>
</table>
```
阅读全文