数据库分页

@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);
        }
    }

 

posted @ 2016-06-08 00:04  追梦人丶  阅读(212)  评论(0编辑  收藏  举报