黑马程序员技术交流社区

标题: 感谢各位帮帮忙啊 [打印本页]

作者: ゞ心痛-τ._煩離    时间: 2013-10-17 13:08
标题: 感谢各位帮帮忙啊
本帖最后由 ゞ心痛-τ._煩離 于 2013-10-18 17:58 编辑

求一段Java实现的分页代码……
作者: 潘才新    时间: 2013-10-17 14:01
网上有。我以前有,但是删掉了{:soso_e143:}
作者: 王飚    时间: 2013-10-17 14:55
楼上说的是,网上有很多这类的代码,修改一下就好,但是自己要看明白哦
作者: 漫步人    时间: 2013-10-18 02:19
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();
    }
   
  }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2