您的位置:首页 > Web前端 > JavaScript

jsp的分页技术

2015-01-10 22:32 183 查看
在jsp开发中,很多时候要用到分页技术,另外纯粹的分页实现就占了很多篇幅,所以使用javabean就十分有必要。

此文章依据实际代码,想大家展示jsp中利用javabean实现分页

目录结构如下:



javabean的代码如下:

package lytjb;

import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;

public class fenye {
ResultSet CountTopicrs = null; // 初始化总记录数Rs变量
ResultSet Pagirs = null; // 初始化分页时Rs变量
public int intCountTopic = 0; // 主题总数
public int intPageSize;// 每页显示主题数
public int intPageCount;// 总页数
public int intPage = 1; // 当前页数
public String nowPage; // int i;
public String HttpFile;// 初始化当前页intPage变量,以准确便获取当前页。 //当前的地址栏的文件
DB db; // 定义Linkdb类的一个对象。

public fenye() {
intPageSize = 40; // 每页显示的记录数目
db = new DB();
}

// Countsql:总记录的Query字符串。[形式为select count(*) from tablename]
// Pagisql :要分页的Query字符串。[形式为select * from tablename where ...]
// request :参数传递过程中的变量。[用来控制翻页时的pages变量]
public ResultSet querySql(String Countsql, String Pagisql,
HttpServletRequest request) throws Exception {
HttpFile = request.getRequestURI(); // 获取当前文件名。
nowPage = request.getParameter("pages"); // 获取当前页,将数值赋予intPage变量。[分页栏中必须要有pages参数]
if (nowPage == null) {
intPage = 1;
} else {
intPage = Integer.parseInt(nowPage);
if (intPage < 1)
intPage = 1;
}
CountTopicrs = db.executeQuery(Countsql); // @@@@@@@@@@@@获取总记录数的结果集。
if (CountTopicrs.next()) {
intCountTopic = CountTopicrs.getInt(1);
}
intPageCount = (intCountTopic + intPageSize - 1) / intPageSize; // 获取总页数。
if (intPage > intPageCount)// 如果当前页大于总页数,则当前页等于总页数。
{
intPage = intPageCount;
}
CountTopicrs.close(); // 关闭总主题数的数据集。
db.close_all();
Pagirs = db.executeQuery(Pagisql); // @@@@@@@@@@@@@@@获取执行分页的结果集。
return Pagirs;
}// end querySql function.

public int getCountTopic()// 获取记录总数。
{
return intCountTopic;
}

public int getPageCount() // 获取总页数。
{
return intPageCount;
}

public int getIntPage() // 获取当前页数。
{
return intPage;
}

public String PageFooter() {
String str = "";
int next, prev;
prev = intPage - 1;
next = intPage + 1;
str += "查询到<font color=red>" + getCountTopic() + "</font>条记录"
+ "    共<font color=red>" + getPageCount() + "</font>页";
str += " 第<font color=red>" + getIntPage() + "</font>页 ";
if (intPage > 1)
str += " <A href=" + HttpFile + "?pages=1" + ">首页</A> ";
else
str += " 首页 ";
if (intPage > 1)
str += " <A href=" + HttpFile + "?pages=" + prev + ">上一页</A> ";
else
str += " 上一页 ";
if (intPage < intPageCount)
str += " <A href=" + HttpFile + "?pages=" + next + ">下一页</A> ";
else
str += " 下一页 ";
if (intPageCount > 1 && intPage != intPageCount)
str += " <A href=" + HttpFile + "?pages=" + intPageCount
+ ">尾页</A>";
else
str += " 尾页 ";
return str;
}

public void close_all() {
db.close_all();
}

}

DB类文件:

package lytjb;

import java.sql.*;

//一个用于查找数据源的工具类。

public class DB {
private Connection con = null;
private Statement stmt = null;
ResultSet rs = null;

public ResultSet executeQuery(String sql) throws Exception {

try{  
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=SHB","sa","1234567");  
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
//con=DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest");  
stmt=con.createStatement();  
rs=stmt.executeQuery(sql);
}catch(Exception e){}
return rs;
}

// 执行Insert,Update语句
public void executeUpdate(String sql) throws Exception {
try{  
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=SHB","sa","1234567");  
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
//con=DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest");  
stmt=con.createStatement();  
int rs=stmt.executeUpdate(sql);

}catch(Exception e){}
}

// 关闭stmt和关闭连接
public void close_all() {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

}

jsp显示代码如下:

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GBK"%>

<%@ page import="lytjb.fenye"%>

<html>

  <head>

    <link rel="stylesheet" type="text/css" href="style.css">

  </head>

  

  <body bgcolor="c0ffc0">

  <jsp:useBean id="pagi" scope="page" class="lytjb.fenye"></jsp:useBean>

 

  

  <%

  String college="政法 ";

  String user="";  

  user = (String)session.getAttribute("username_session");  

  if(user==null || user.equals("")){ 

  user="游客"; 

  } 

  else 

  ; 

  //out.println(user); 

   %>

      <table  align="center"  border='0'>

      <tr><td><img width="800" height="200" src="source1/1.jpg"/> </td></tr>

      </table> 

      <table  align="center"  border='1' bordercolor="c0ff00" bgcolor="" rules=rows>

      <tr>

      <td>您好<a href="modi_person_info.jsp"><%=user %></a>,欢迎光临!请</td>

      <td><table><tr><td><input type="button" value="登陆"  onclick="window.location.href='denglu.html'"></td></tr></table></td>

      <td><table><tr><td><input type="button" onclick="window.location.href='zhuceqian.jsp'" value="注册"/></td></tr></table></td>

      <td><input type="button" value="退出" name="logout" onclick="window.location.href='tuichu.jsp'"/></td>

      <td><table><tr><td width="360"><input type="button" value="管理员登陆" onclick="window.location.href='admin_denglu.html'"></td></tr></table></td>

      <td>

      <table align="right"><tr><td><input type="button" onclick="window.location.href='shopCar.jsp'" value="我的购物车" id="shopCar"/></td></tr></table>

      </td>

      </tr>

    </table>

   <table width="800"  align="center" border=1 height="40" bordercolor="c0ff00" bgcolor="ffffff">

    <tr><td width="200"  align="center"><a href="index.jsp">首页</a></td><td width="200" align="center"><a href="er_shou_shu.jsp">二手书</a></td><td width="200"  align="center"><a href="liu_yan_ban.jsp">留言板</a></td><td width="200" align="center"><a href="lian_xi_wo_men.jsp">联系我们</a></td></tr> 

   </table> 

    

   <p></p>

   <table align="center"  >

   <tr><td align="center" ><form name="serach_college_book" action="sousuoshuji.jsp?college=<%=college %>" method="get">

    <input type="text" name="college_book_search" id="college_book_text"/>

      <input type="submit" name="college_book_search" id="college_book_submit" value="搜索"/>

   </form></td></tr>

   </table>

   <!--table开始显示图书-->

   

   <table align="center">

  <tr>

    <%
String CountQuery = "select count(*) from college_info where college='政法 '";//查询总记录数
String sql="select book_picture,book_name,book_out_price,book_stock from college_info where college='政法 '";//查询结果集
ResultSet rs = pagi.querySql(CountQuery, sql, request);//request为jsp内置变量
String footer = pagi.PageFooter();//这句话一定要写在while之前,否则页面获取到的记录数为0
if (pagi.intPageCount > 0) {
int i = 0;
while (rs.next()) {
i++;
if (i > ((pagi.intPage - 1) * pagi.intPageSize)
&& (i <= pagi.intPage * pagi.intPageSize)) {
String picture=rs.getString("book_picture");

    String bookname=rs.getString("book_name");

    String price=rs.getString("book_out_price");

    String no=rs.getString("book_stock");

    int no_int=Integer.parseInt(no);

    if(no_int<=0)

    no="缺货";

   

    %>
<td><table  align="center" ><tr ><td align="center" width="163"><img width="100" height="120" src="source/<%=picture %>.jpg"/></td></tr><tr width="100"><td align="center" width="100"><a href="detail.jsp?bookid=<%=picture
%>"><%=bookname %></a></td></tr><tr><td align="center">价格:<%=price %></td></tr><tr><td align="center">剩余量:<%=no %></td></tr></table></td>

   

    <% 

    i=i+1; 

    if(i%5==0){

    out.println("</tr><tr>");

    }

    %>

<%
}
}
}
out.println("<table width='700' align=" + "right"
+ "><tr><td colspan=2>" + footer + "</td></tr></table>");//翻页显示一定要写在数据库关闭之前
rs.close();
//pagi.close_all();
%>
</tr>

  </table>

  

  </body>

</html>

效果图如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息