java分页查询

  1     /** BaseDao中
  2      * 查询单个列值
  3      * @param sql
  4      * @param objects
  5      * @return
  6      * @throws SQLException 
  7      * @throws ClassNotFoundException 
  8      */
  9     public Object excuteQueryObject(String sql,Object...objects) throws ClassNotFoundException, SQLException {
 10         this.createStatement(sql, objects);
 11         rs=ps.executeQuery();
 12         Object ob=null;
 13         if(rs.next()) {
 14             ob=rs.getObject(1);
 15         }
 16         this.close();
 17         return ob;
 18     }
 19 
 20 
 21 /**      studentdao中
 22      * 分页查询
 23      * 
 24      * @param page
 25      * @return
 26      * @throws DAOException
 27      */
 28     public List<Students> queryPageAll(int page) throws DAOException {
 29         page = (page - 1) * 3;// 通过页码,计算出当前页的开始行号;
 30         String sql = "select * from students limit ?,3";
 31         try {
 32             return this.excuteQuery(sql, rm, page);
 33         } catch (Exception e) {
 34             e.printStackTrace();
 35             throw new DAOException("查询全部学生有误!" + e.getMessage());
 36         }
 37     }
 38 
 39     /**
 40      * 统计总页数
 41      * 
 42      * @return
 43      * @throws SQLException
 44      * @throws ClassNotFoundException
 45      */
 46     public int countPages() throws ClassNotFoundException, SQLException {
 47         String sql = "select count(*) as sl from students";
 48         // 调用basedao的查询单个列值方法
 49         Object ob = this.excuteQueryObject(sql, null);
 50         // 将object转为int:count是总记录条数
 51         int count = Integer.parseInt(String.valueOf(ob));
 52         // 根据总记录条数计算出总页数
 53         int pages = (count % 3) == 0 ? (count / 3) : (count / 3) + 1;
 54         return pages;
 55 
 56     }
 57 
 58 
 59     /**    BIZ调用DAO
 60      * 分页查询
 61      * @param page
 62      * @return
 63      * @throws DAOException
 64      * @throws SQLException 
 65      * @throws ClassNotFoundException 
 66      */
 67     public List<Students> queryPageStuAll(int page) throws DAOException, ClassNotFoundException, SQLException{
 68         List<Students> list=null;
 69         //调用查询总页数据方法
 70         int pages=sd.countPages();
 71         //如果页码在指定范围内容
 72         if(page>0 && page<=pages) {
 73             //调用分页查询方法
 74             list=sd.queryPageAll(page);
 75         }        
 76         return list;
 77     }
 78 
 79     /**
 80      * 查询总页数
 81      * @return
 82      * @throws ClassNotFoundException
 83      * @throws SQLException
 84      */
 85     public int queryPages() throws ClassNotFoundException, SQLException {
 86         return sd.countPages();
 87     }
 88 
 89     /**   View调用,层层调用
 90      * 查询全部学生
 91      * 
 92      * @throws DAOException
 93      */
 94     public void displayStuAllInfo() throws DAOException {
 95         List<Students> list = sb.queryStuAll();
 96         System.out.println("学号\t姓名\t年龄\t性别");
 97         for (Students s : list) {
 98             System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getSex());
 99         }
100     }
101 
102     int page=1;//设置分页的当前页码[默认为第1页]
103     public void displayPageStuAllInfo() throws DAOException, ClassNotFoundException, SQLException {
104         // 调用方法获取查询结果
105         List<Students> list = sb.queryPageStuAll(page);
106         System.out.println("编号\t姓名\t年龄\t性别");
107         for (Students s : list) {
108             System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getSex());
109         }
110         System.out.println("========================================");
111         // 查询总页数
112         int pages = sb.queryPages();
113         //显示页码信息
114         System.out.print("总页数为:" + pages);
115         System.out.println("\t当前页码为:" + page);
116         
117         //上一页和下一页
118         System.out.println("1:上一页     2:下一页     3:指定页码     4:返回上一级");
119         String op=input.next();
120         if(op.equals("1")) {
121             page=    page==1?1:page-1;//如果当前页为第1页,则还是第1页
122         }else if(op.equals("2")) {
123             page=   page==pages?pages:page+1;//如果当前页为最后一页,则还是最后1页
124         }else if(op.equals("3")) {
125             System.out.print("请输入页码:");
126             page=input.nextInt();
127         }else if(op.equals("4")) {
128             MenuView.students();
129         }
130         //自调
131         this.displayPageStuAllInfo();
132         
133     }

 

posted @ 2018-07-08 10:32  某某55464  阅读(89)  评论(0)    收藏  举报