码迷,mamicode.com
首页 > 数据库 > 详细

数据库分页

时间:2016-06-08 01:43:35      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

@Test
    public void testArrayHandler() throws Exception {
        QueryRunner run = new QueryRunner(DataSourceUtils.getDataSource());
        Scanner sc = new Scanner(System.in);
        String page = sc.nextLine();
        int pg = Integer.parseInt(page);
        // 组成SQL语句
        int pageSize = 10;
        int start = (pg - 1) * pageSize;
        int end = start + pageSize;
        String sql = "";
        DatabaseMetaData db = run.getDataSource().getConnection().getMetaData();
        String dbName = db.getDatabaseProductName();
        if (dbName.toLowerCase().equals("oracle")) {
            // 以下是Oracle的
            sql = "select * from (select rownum nm,id,name,pages from books) b where nm>" + start + " and nm<=" + end;
        } else if (dbName.toLowerCase().equals("mysql")) {
            // 以下是Mysql的
            sql = "select * from books limit " + start + "," + pageSize;
        }
        // 执行查询
        List<Map<String, Object>> list = run.query(sql, new MapListHandler());
        for (Map map : list) {
            System.err.println(map);
        }
    }

 

数据库分页

标签:

原文地址:http://www.cnblogs.com/fujilong/p/5568756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!