jsp/servlet分页

1:链接数据库

/*
 * 连接数据库
 */
public class BaseDao {


//编写驱动语句
private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//连接数据库语句


private String url = "jdbc:sqlserver://localhost:1433; DatabaseName=JDQN";
    private String name = "sa";
    private String passwod = "123456"; 
    /*
     * 打开数据库2
     */
   public Connection gerConnectionER(){
  Connection conn = null;
  try {
Class.forName(driver);
conn = DriverManager.getConnection(url, name, passwod);

} catch (ClassNotFoundException e) {
System.out.println("打开数据库失败");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("打开数据库失败");
e.printStackTrace();
}
return conn;
   }
   
   /*
    * 关闭数据库
    */
   
   public void clossAll(Connection conn,Statement stmt, ResultSet rs){
  if(rs!=null){
  try {
rs.close();
} catch (SQLException e) {
System.out.println("关闭rs失败");
e.printStackTrace();
}  
  }
  if(stmt!=null){
  try {
stmt.close();
} catch (SQLException e) {
System.out.println("stmt关闭失败");
e.printStackTrace();
}
  
  }
  
  if(conn!=null){
  try {
conn.close();
} catch (SQLException e) {
System.out.println("conn关闭失败");
e.printStackTrace();
}
  
  }
   }
   
   /**
    * 增删改通用方法  executeUpdate用于增删改,executeQuery()用于查询
    */
   
   public int executeUpdate(String sql, Object[] object){
PreparedStatement pstmt = null;
int num = 0;
Connection conn = gerConnectionER();

try {
pstmt = conn.prepareStatement(sql);
if(pstmt!=null){
for(int i =0; i < object.length; i++){
pstmt.setObject(i+1, object[i]);

}

}
num = pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println("增删改通用方法失败");
e.printStackTrace();
}finally{


clossAll(conn, pstmt, null);
}
return num;

  
   }
}


2:创建page类

public class page {
private int totalPageCount = 1;    //总数页数


private int pageSize = 0;  //页面大小  记录每显示的数据


private int totalCount = 0; //记录总数


private int currPageNo =1;   //当前页码


List<easybuy_product> lsitmsg;   //保存从数据库获取的数据




public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}




public int getPageSize() {
return pageSize;
}




public void setPageSize(int pageSize) {
if(pageSize>0){
this.pageSize = pageSize;
}
}




public int getTotalCount() {
return totalCount;
}




public void setTotalCount(int totalCount) {
if(totalCount>0){
this.totalCount = totalCount;
    //计算总页数
totalPageCount = this.totalCount%pageSize==0?(this.totalCount/pageSize):this.totalCount/pageSize+1;


}
}




public int getCurrPageNo() {
return currPageNo;
}

public void setCurrPageNo(int currPageNo) {
if(currPageNo>0){
this.currPageNo = currPageNo;
}
}




public List<easybuy_product> getLsitmsg() {
return lsitmsg;
}




public void setLsitmsg(List<easybuy_product> lsitmsg) {
this.lsitmsg = lsitmsg;
}










}



3:写查询的方法和SQL语句

1)方法省

2)SQL语句:String sql = "select top "+pageSize+" * from 表名 where msgid not in(select top "+(pageNo-1)*pageSize+" msgid from msg where sendto =? ) 


4:jsp页面

.<%
page pages = (page)request.getAttribute("pages");
if(pages==null){
response.sendRedirect("epcServlet?pageIndex=1");
}else{
int totalpages= pages.getTotalPageCount();
int pagesInex = pages.getCurrPageNo();

if(pagesInex> 1){
%>

<p align="center"><a href="epcServlet?pageIndex=1">首页</a>&nbsp;&nbsp;
                <a href="epcServlet?pageIndex=<%=pagesInex-1%>">上一页</a>&nbsp;&nbsp;
               
<%
}
if(pagesInex<totalpages){
%>
<a href="epcServlet?pageIndex=<%=pagesInex+1%>">下一页</a>&nbsp;&nbsp;
                <a href="epcServlet?pageIndex=<%=totalpages%>">未页</a></p>
<%
}
}
%>

5:servlet

  /*分页*/

String pageIndex = request.getParameter("pageIndex");    //获得当前页数
if(pageIndex==null){
pageIndex = "1";
}

int currPageNo = Integer.valueOf(pageIndex);


epDao endao = new epDao();
int zongyeshu = endao.zongTiaoShu();  //获取总记录数 
page pages = new page();
pages.setPageSize(8);   //每页显示的条数
pages.setTotalCount(zongyeshu);
int totalPage = pages.getTotalPageCount();

/**对首页和未页的控制*/
if(currPageNo<1){
currPageNo=1;
}else if(currPageNo > pages.getTotalPageCount()){
currPageNo = pages.getTotalPageCount();

}
pages.setCurrPageNo(currPageNo);
List<easybuy_product>  newep = endao.epGet(pages.getCurrPageNo(), pages.getPageSize());
pages.setLsitmsg(newep);
request.setAttribute("pages",pages);
        /**
         * 转发
         */
request.getRequestDispatcher("index.jsp").forward(request, response);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值