这是BookSaleImpl里getBookSaleByPriceRange方法的代码
@Override
public ArrayList<BookSale> getBookSaleByPriceRange(int minPrice, int maxPrice, String priceRange) {
ArrayList<BookSale> bookSales = new ArrayList<>();
Connection conn = Connect.getConn();
Statement stat = null;
try {
stat = conn.createStatement();
String range = "";
if (priceRange != null && !priceRange.isEmpty()) {
// 若传入参数,根据参数设置查询价格区间
String[] rangeStr = priceRange.split("-");
if (rangeStr.length == 2) {
int min = Integer.parseInt(rangeStr[0]);
int max = Integer.parseInt(rangeStr[1]);
range = " AND price >= " + min + " AND price < " + max;
}
}
String sqlstr = "SELECT * FROM bookSales WHERE price >= " + minPrice + " AND price < " + maxPrice + range;
ResultSet rs = stat.executeQuery(sqlstr);
while (rs.next()) {
int id = rs.getInt("id");
int bookId = rs.getInt("bookId");
int price = rs.getInt("price");
int quantity = rs.getInt("quantity");
Date saleDate = rs.getDate("saleDate");
String dateStr = saleDate.toString();
BookSale temp = new BookSale(id, bookId, price, quantity, dateStr);
bookSales.add(temp);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
Connect.getClose(conn, stat);
return bookSales;
}
Jsp页面的代码:
<%--
Document : bookModInfo
Created on : 2023-6-12, 22:33:29
Author : 人间惊鸿客
--%>
<%@page import="impl.BookSaleImpl"%>
<%@page import="dao.BookSaleDao"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@page import="java.util.ArrayList"%>
<%@page import="vo.BookSale"%>
<html>
<head>
<meta charset="UTF-8">
<title>图书价格区间查询</title>
</head>
<body>
<h1>查询图书价格区间</h1>
<form method="post" action="query">
<p>价格区间:(0,50)<input type="radio" name="priceRange" value="0,50"> (50,100)<input type="radio" name="priceRange" value="50,100"> (100,∞)<input type="radio" name="priceRange" value="100,"> <input type="submit" value="查询"></p>
</form>
<%
// 获取查询结果
String priceRange = request.getParameter("priceRange");
BookSaleDao bookSaleDao = new BookSaleImpl();
int minPrice = 0;
int maxPrice = 0;
ArrayList<BookSale> bookSales = new ArrayList<BookSale>();
if (priceRange != null) {
String[] priceArr = priceRange.split(",");
if (priceArr.length == 2) {
try {
minPrice = Integer.parseInt(priceArr[0]);
maxPrice = Integer.parseInt(priceArr[1]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
} else if (priceArr.length == 1) {
try {
minPrice = Integer.parseInt(priceArr[0]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
bookSales = bookSaleDao.getBookSaleByPriceRange(minPrice, maxPrice,priceRange);
}
%>
<%
// 调试信息
out.println("priceRange: " + priceRange);
out.println("bookSales size: " + bookSales.size());
%>
<h2>查询结果:</h2>
<table>
<tr>
<th>ID</th>
<th>图书ID</th>
<th>价格</th>
<th>数量</th>
<th>销售日期</th>
</tr>
<% if (!bookSales.isEmpty()) {
for(BookSale bookSale: bookSales) { %>
<tr>
<td><%=bookSale.getId()%></td>
<td><%=bookSale.getBookId()%></td>
<td><%=bookSale.getPrice()%></td>
<td><%=bookSale.getQuantity()%></td>
<td><%=bookSale.getSaleDate()%></td>
</tr>
<% }
} else { %>
<tr>
<td colspan="5">未查询到符合条件的图书销售记录</td>
</tr>
<% } %>
</table>
</body>
</html>
这不是没查到数据么 ,你把你的sql复职出来去数据库带进你的参数去查询,查一下,肯定没符合条件的数据
SELECT * FROM bookSales WHERE price >= " + minPrice + " AND price < " + maxPrice + range;
priceRange为null,没有勾选priceRange,minPrice、maxPrice也是null,可以定义一个默认选中;或者定义默认minPrice、maxPrice