mysql模糊查询后分页_jsp模糊查询后的数据进行分页,但点击下一页后就查询全部的了...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

String path = request.getContextPath();

String basePath = request.getScheme() + "://"

+ request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%>

信息表

#container {

width: 500px;

}

#header {

background-color: #99bbbb;

height: 60px;

width: 150px;

}

#menu {

background-color: yellow;

height: 809px;

width: 209px;

float: left;

}

#content {

background-color: #F0F8FF;

height: 809px;

width: 1000px;

float: left;

}

#footer {

background-color: #99bbbb;

height: 60px;

text-align: center;

}

.divcss5 img {

width: 300px;

height: 200px

}

.out{

}

.over{

border:solid 3px red;

font-weight:bold;

cursor:pointer;

}

.aa{

list-style-type:none;

display:none;

}

.box {

width: 300px;

height: 74px;

float: left;

}

.box ul {

list-style-type: none;

margin: 0px;

padding: 0px;

}

.box li {

border:solid 2px red;

margin: 7px;

padding: 5px;

float: left;

}

request.setCharacterEncoding("GBK");

Class.forName("com.mysql.jdbc.Driver");

Connection conn = DriverManager.getConnection(

"jdbc:mysql://localhost:3306/Student", "root", "1234");

Statement stmt = conn.createStatement();

//每页显示记录数

int PageSize = 6; //每页显示记录数

int StartRow = 0; //开始显示记录的编号

int PageNo = 0;//需要显示的页数

int CounterStart = 0;//每页页码的初始值

int CounterEnd = 0;//显示页码的最大值

int RecordCount = 0;//总记录数;

int MaxPage = 0;//总页数

int PrevStart = 0;//前一页

int NextPage = 0;//下一页

int LastRec = 0;

int LastStartRecord = 0;//最后一页开始显示记录的编号

//获取需要显示的页数,由用户提交

if (request.getParameter("PageNo") == null) { //如果为空,则表示第1页

if (StartRow == 0) {

PageNo = StartRow + 1; //设定为1

}

} else {

PageNo = Integer.parseInt(request.getParameter("PageNo")); //获得用户提交的页数

StartRow = (PageNo - 1) * PageSize; //获得开始显示的记录编号

}

//因为显示页码的数量是动态变化的,假如总共有一百页,则不可能同时显示100个链接。而是根据当前的页数显示

//一定数量的页面链接

//设置显示页码的初始值!!

if (PageNo % PageSize == 0) {

CounterStart = PageNo - (PageSize - 1);

} else {

CounterStart = PageNo - (PageNo % PageSize) + 1;

}

CounterEnd = CounterStart + (PageSize - 1);

//获取总记录数

ResultSet rs = stmt.executeQuery("select count(product.id) from product ");

rs.next();

RecordCount = rs.getInt(1);

String q = request.getParameter("q") == null ? "" : request

.getParameter("q");

rs = stmt

.executeQuery("SELECT * FROM product INNER JOIN type on type.id=product.t_id where (product.t_id like*%"

+ q

+ "%*)or (type.name like*%"

+ q

+ "%*) order by type.name,product.t_id limit "

+ StartRow + ", " + PageSize);

//获取总页数

MaxPage = RecordCount % PageSize;

if (RecordCount % PageSize == 0) {

MaxPage = RecordCount / PageSize;

} else {

MaxPage = RecordCount / PageSize + 1;

}

%>

int i = 1;

while (rs.next()) {

int bil = i + (PageNo-1)*PageSize;

%>

i++;

}

%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于JSP模糊查询分页的实现,可以采用以下简单的代码实现: 首先,在jsp页面中,定义相应的查询表单,包括输入查询关键字和选择页码等必要的组件。 接着,在后台的servlet中,获取相应的查询关键字,并结合查询语句实现模糊查询,同时根据选择的页码计算出需要查询数据起始位置和结束位置,从而实现数据分页。 最后,将查询结果及分页信息传送到jsp页面中,结合相应的页面模板实现数据展示和分页导航。 具体的代码实现可参考以下示例: 1. JSP页面: <form method="post" action="query.jsp"> <input type="text" name="keyword" placeholder="请输入查询关键字" required> <button type="submit">查询</button> </form> <%-- 数据展示 --%> <table> <thead> <tr> <th>序号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <% for(int i=0; i<list.size(); i++){ %> <tr> <td><%=i+1 %></td> <td><%=list.get(i).getName() %></td> <td><%=list.get(i).getAge() %></td> <td><%=list.get(i).getGender() %></td> </tr> <% } %> </tbody> </table> <%-- 分页导航 --%> <% if(totalPage>0){ %> <div class="pagination"> <% if(curPage>1){ %> <a href="?keyword=<%=keyword %>&page=<%=curPage-1 %>" class="prev">«</a> <% } %> <% for(int i=1; i<=totalPage; i++){ %> <% if(i==curPage){ %> <span class="current"><%=i %></span> <% }else{ %> <a href="?keyword=<%=keyword %>&page=<%=i %>" class="page"><%=i %></a> <% } %> <% } %> <% if(curPage<totalPage){ %> <a href="?keyword=<%=keyword %>&page=<%=curPage+1 %>" class="next">»</a> <% } %> </div> <% } %> 2. Servlet代码: String keyword = request.getParameter("keyword"); int curPage = request.getParameter("page")==null ? 1 : Integer.valueOf(request.getParameter("page")); int pageSize = 10; int startIndex = (curPage-1)*pageSize; int endIndex = curPage*pageSize-1; List<Data> list = dao.query(keyword, startIndex, endIndex); int totalCount = dao.count(keyword); int totalPage = (totalCount-1)/pageSize+1; request.setAttribute("list", list); request.setAttribute("keyword", keyword); request.setAttribute("curPage", curPage); request.setAttribute("totalPage", totalPage); request.getRequestDispatcher("query.jsp").forward(request, response); 3.DAO代码: public List<Data> query(String keyword, int startIndex, int endIndex){ String sql = "select * from data where name like ? limit ?,?"; Object[] params = new Object[]{ '%'+keyword+'%', startIndex, endIndex }; return template.query(sql, new BeanPropertyRowMapper<Data>(Data.class), params); } public int count(String keyword){ String sql = "select count(*) from data where name like ?"; Object[] params = new Object[]{ '%'+keyword+'%' }; return template.queryForObject(sql, Integer.class, params); } 需要注意的是,该示例代码使用了JavaWeb的常用组件,如servlet、JDBC和JSP等,同时也使用了一些常用的第三方jar包,如spring-jdbc和mysql-connector-java等,因此在使用前需确保相关环境和依赖都已配置完毕。同时,由于该示例只是展示了一种可能的实现方式,如果需要满足更多的实际需求,还需根据具体情况进行相应的调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值