A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ゞ心痛-τ._煩離 中级黑马   /  2013-10-17 13:08  /  1266 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 ゞ心痛-τ._煩離 于 2013-10-18 17:58 编辑

求一段Java实现的分页代码……

3 个回复

倒序浏览
网上有。我以前有,但是删掉了{:soso_e143:}
回复 使用道具 举报
楼上说的是,网上有很多这类的代码,修改一下就好,但是自己要看明白哦
回复 使用道具 举报
public class TestPage {

  /**
   * @param args
   */
  public static void main(String[] args) {
    //getResult(10, 20);//[10,20)
    //getPage(10, 5);
    //getPageSimple(10, 5);
    Scanner sc = new Scanner(System.in);
    int page = sc.nextInt();
    getPageSimple(10, page);//每页10条,第page页
  }
  //获得数据表一共多少条记录
  public static int getTotalNumber(){
    int n = -1;
    String sql = "select count(*) num from mytemp";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      conn = ConnectionUtils.openConnection();
      stmt = conn.createStatement();
      rs = stmt.executeQuery(sql);
      rs.next();
      n = rs.getInt("num");
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return n;
  }
  
  
  public static void getPageSimple(int pageSize, int page){
    //totalPage:总页数; totalNumber:总记录条数
    int totalPage = 0;
    int totalNumber = getTotalNumber();
    if (totalNumber % pageSize == 0){
      totalPage = totalNumber/pageSize;
    }else{
      totalPage = totalNumber/pageSize + 1;
    }
    if (page > totalPage)
        page = totalPage;
    if (page < 1){
        page = 1;
    }
    int from = (page - 1) * pageSize + 1;
    int to = from + pageSize;
    getResult(from, to);//[from, to)
  }
  
  
  
  public static void getPage(int pageSize, int page){
    int from = (page - 1) * pageSize + 1;
    int to = from + pageSize;
    String sql = "select id, user_id, " +
    "login_time, logout_time, rn " +
    "from " +
    "(select id, user_id, login_time, " +
    "logout_time, rownum rn " +
    "from mytemp where rownum < ? ) " +
    "where rn >= ?";
    Connection conn = null;
    PreparedStatement pStmt = null;
    ResultSet rs = null;
    try {
      conn = ConnectionUtils.openConnection();
      pStmt = conn.prepareStatement(sql);
      pStmt.setInt(1, to);
      pStmt.setInt(2, from);
      rs = pStmt.executeQuery();
      while(rs.next()){
        String line = rs.getString("id")
          + "-" + rs.getString("user_id")
          + "-" + rs.getString("rn");
        System.out.println(line);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally{
      ConnectionUtils.closeResultSet(rs);
      ConnectionUtils.closeStatement(pStmt);
      ConnectionUtils.closeConnection(conn);
    }
   
   
  }

  public static void getResult(int m, int n){
    String sql = "select id, user_id, " +
        "login_time, logout_time, rn " +
        "from " +
        "(select id, user_id, login_time, " +
        "logout_time, rownum rn " +
        "from mytemp where rownum < ? ) " +
        "where rn >= ?";
    Connection conn = null;
    PreparedStatement pStmt = null;
    try {
      conn = ConnectionUtils.openConnection();
      pStmt = conn.prepareStatement(sql);
      pStmt.setInt(1, n);
      pStmt.setInt(2, m);
      ResultSet rs = pStmt.executeQuery();
      while(rs.next()){
        String line = rs.getString("id")
          + "-" + rs.getString("user_id")
          + "-" + rs.getString("rn");
        System.out.println(line);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
   
  }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马