创建分页存储过程
1 | CREATE OR REPLACE PROCEDURE split_page ( |
qsql: 查询语句
page_index: 查询第几页
page_size: 每页大小
total: 总条数
res: 结果集游标
注意:qsql结果集中列名不能与rownum别名rownumber
重复
java 调用
1 | db.dirver=oracle.jdbc.driver.OracleDriver |
Connection conn = null;
CallableStatement cstmt = null;
ResultSet res = null;
try {
conn = DBUtil.getConnection();
cstmt = conn.prepareCall("{call split_page(?,?,?,?,?)}");
cstmt.setString(1, "select * from student order by age asc");
cstmt.setInt(2, 2);
cstmt.setInt(3, 2);
cstmt.registerOutParameter(4, OracleTypes.INTEGER);
cstmt.registerOutParameter(5, OracleTypes.CURSOR);
cstmt.execute();
System.out.println(cstmt.getObject(4));
res = (ResultSet) cstmt.getObject(5);
while (res.next()) {
System.out.println(res.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(res, cstmt, conn);
}
rownum 和 group by 的关系,当group by 中存在非主键,则oracle会先给列加rownum然后再group by.
如果主键类型是char、varchar2而非int,number等,则先rownum再group by.