Java_web分页代码

package com.fengjing.webphonebook.db;

import Java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class DB {
         public static Connection getConnection() throws Exception{
              Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
              Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=address","sa","");
              return conn;
         }
 
         public static PreparedStatement getPstmt(Connection conn, String sql) throws Exception{
              PreparedStatement pstmt = conn.prepareStatement(sql);
              return pstmt;
         }
 
         public static Statement getStmt(Connection conn) throws Exception{
              Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
              return stmt;
         }
 
         public static ResultSet getRs(Statement stmt,String sql) throws Exception{
              ResultSet rs = stmt.executeQuery(sql);
              return rs;
         }
 
         public static void close(Connection conn, Statement stmt, ResultSet rs) throws Exception{
              if(rs != null){
                   rs.close();
              }
              if(stmt != null){
                   stmt.close();
              }
              if(conn != null){
                   conn.close();
              }
         }
         public static void close(Connection conn, Statement stmt) throws Exception{
              if(stmt != null){
                   stmt.close();
              }
              if(conn != null){
                   conn.close();
              }
         }
}

PageBean类代码:

package com.fengjing.webphonebook.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.fengjing.webphonebook.db.DB;
import com.fengjing.webphonebook.vo.Address;

public class PageBean {
 
         //每页显示多少行
         private int pageSize = 4;
         //总共多少页
         private int pageNum;
         //当前是第几页
         private int pageId;
         //数据库中总共多少条记录
         private int size;
 
         @SuppressWarnings("unchecked")
         public List queryAll(int pageId) throws Exception {
                  this.pageId = pageId;
                  Connection conn = null;
                  Statement stmt = null;
                  ResultSet rs = null;
  
                  if(this.pageId<1) {
                   this.pageId=1;
          }
          conn = DB.getConnection();
          stmt = DB.getStmt(conn);
          String sql = "select * from address";
          rs = DB.getRs(stmt, sql);
          //使游标指向最后一条记录
          rs.last();
          //获取当前结果集合中所有数据的行数
          this.size = rs.getRow();
          this.pageNum = (size%pageSize==0? size/pageSize:size/pageSize+1);
          if(this.pageId>this.pageNum) {
                   this.pageId = this.pageNum;
          }
          //将游标重新移动到第一条记录的前面
          rs.beforeFirst();
  
          //步长
          for (int i = 0; i < (this.pageId-1)*pageSize; i++) {
                   rs.next();
          }
          List list = new ArrayList();
          //控制每页显示的条数
          for(int j=0;j
                   //判断是否有下一条记录
                   if(!rs.next()) {
                           break;
                   }
                   Address address = new Address();
                   address.setId(rs.getInt(1));
                   address.setUid(rs.getInt(2));
                   address.setPname(rs.getString(3));
                   address.setPhone(rs.getString(4));
                   address.setAge(rs.getInt(5));
                   address.setEmail(rs.getString(6));
                   address.setAddress(rs.getString(7));
                   list.add(address);
                  }
  
              return list;
         }

         public int getPageNum() {
              return pageNum;
         }

         public void setPageNum(int pageNum) {
              this.pageNum = pageNum;
         }

         public int getPageId() {
              return pageId;
         }

         public int getSize() {
              return size;
         }
}

猜你喜欢

转载自blog.csdn.net/lxlong89940101/article/details/83933659
今日推荐