我有一个 Search JSP 页面,它将接受一些输入字段并将查询发送到具有相应给定字段的数据库。如果数据库中有 2000 条相关记录,则页面将显示前 30 条结果和一个显示后续结果的下一个按钮或显示上一个结果的上一个按钮,就像我们在 Google 中搜索时一样。现在我的问题是我不知道如何做这些事情。我了解 servlet、JSP、JDBC 的基本知识。有没有人解决这个问题?建议和想法最受赞赏。代码将是这样的
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import = "com.preva.vo.StoppageDetails"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="css/cal.css" rel="stylesheet" type="text/css" />
<link href="css/sty.css" rel="stylesheet" type="text/css" />
<link href="css/tabborder.css" rel="stylesheet" type="text/css" />
</head>
<body>
<jsp:include page="Header.jsp" />
<table align=center border=0 cellspacing=0 cellpadding=0>
<tr ><td colSpan=5 align=center><b>Overspeed Details</b></td></tr>
<tr >
<td colspan=5 align=center>
<b><%=request.getParameter("vehicleId") %></b></td></tr>
<tr><td>From
<%=session.getAttribute("fromdate") %>
to
<%=session.getAttribute("startdate") %></td></tr>
</table><br></br>
<table class='rptTbl_sortable' width='80%' align=center cellspacing='2' cellpadding='0' border='0'>
<thead>
<tr class="rptHdrRow">
<th id="index" class="rptHdrCol_sort" nowrap>DeviceID</th>
<th id="date" class="rptHdrCol_sort" nowrap>Date</th>
<th id="time" class="rptHdrCol_sort" nowrap>Speed</th>
<th id="statusdesc" class="rptHdrCol_sort" nowrap>Status</th>
<th id="address" class="rptHdrCol_sort" nowrap>Address</th>
</tr>
</thead>
<tbody>
<c:forEach items="${sessionScope.overspeeddetails}" var="overspeeddetailsvar">
<tr class="rptBodyRowOdd">
<td><c:out value="${overspeeddetailsvar.deviceID}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.TIMESTAMP}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.speed}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.statuscode}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.address}"></c:out></td>
</tr>
<tr class="rptBodyRowEven">
<td><c:out value="${overspeeddetailsvar.deviceID}"> </c:out> </td>
<td><c:out value="${overspeeddetailsvar.TIMESTAMP}"></c:out></td>
<td> <c:out value="${overspeeddetailsvar.speed}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.statuscode}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.address}"></c:out></td>
</tr> </c:forEach>
</tbody>
</table>
</body>
</html>
bean 和 Dao 类会是这样的
public class OverspeedDetails {
private String deviceID,timestamp,statuscode,address;
private double speed;
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public String getDeviceID() {
return deviceID;
}
public void setDeviceID(String deviceID) {
this.deviceID = deviceID;
}
public String getTIMESTAMP() {
return timestamp;
}
public void setTIMESTAMP(String TIMESTAMP) {
this.timestamp = TIMESTAMP;
}
public String getStatuscode() {
return statuscode;
}
public void setStatuscode(String statuscode) {
this.statuscode = statuscode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<OverspeedDetails> getosDetails(String accountID,String deviceID,String Timestamp1,String Timestamp2,double speed) {
Connection con=null;
List<OverspeedDetails> overspeeddetail = new ArrayList<OverspeedDetails>();
try{
con= DBConnectionFactory.getDBConnection();
String sql="SELECT deviceID,TIMESTAMP,speedKPH,statuscode,address FROM eventdata WHERE (TIMESTAMP BETWEEN '"+Timestamp1+"' AND '"+Timestamp2+"') AND accountID='"+accountID+"' AND deviceID='"+deviceID+"'and speedKPH >"+speed+";";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(sql);
System.out.println("Dao over"+rs.next());
while (rs.next()){
OverspeedDetails od=new OverspeedDetails();
od.setDeviceID(rs.getString(1));
String stringtimestamp=rs.getString(2);
long l=Long.parseLong(stringtimestamp);
long longtimestamp = l * 1000L;
String str = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date(longtimestamp));
od.setTIMESTAMP(str);
od.setSpeed(rs.getDouble(3));
od.setStatuscode(rs.getString(4));
String add=rs.getString(5);
String add1[]=add.split(" \\d");
//String add2=java.util.Arrays.toString(add.split(" "));
od.setAddress(add1[0]);
overspeeddetail.add(od);
}
}catch (Exception e) {
e.printStackTrace();
}
System.out.println(overspeeddetail);
return overspeeddetail;
}