mysql和oracle相似
oracle分页语句
sql语句
select * from (select t.*,rownum rn from food t where t.foodname like '%"+name+"%') where rn>=开始位置 and
rn<=结束位置;
例如每页显示5条记录
第一页
开始位置 (页数-1)x每页显示的条数+1 (1-1)x5+1
结束位置 页数x每页显示的条数 1x5
mysql分页语句(第一条从0索引开始)
sql语句
select * from food t where t.foodName like '%"+name+"%' limit开始位置,每页显示的条数
例如每页显示5条记录
第一页
开始位置 (页数-1)x每页显示的条数 (1-1)x5
包装工具类不用改 通用的
获取oracle包装工具了中的开始索引减1就可以了
1、Oracle分页的工具类
package cn.et.model;
import java.util.List;
public class PageTools_1 {
/**
*
*
* @param curPage 页面传入的当前页
* @param totalCount 数据库查询的总记录数
* @param pageCount 每页显示的条数
*/
public PageTools_1(Integer curPage,Integer totalCount,Integer pageCount){
this.curPage=curPage;
this.totalCount=totalCount;
this.pageCount=pageCount==null?this.pageCount:pageCount;
//上一页
this.prePage=(curPage==1?1:curPage-1);
//总页数
this.totalPage=totalCount%this.pageCount==0?totalCount/this.pageCount:(totalCount/this.pageCount+1);
//下一页 ==不行的话 可以使用equals
this.nextPage=(curPage==totalPage)?totalPage:(curPage+1);
//数据库第几页每页显示的数据 开始索引和结束索引
this.startIndex=(curPage-1)*this.pageCount+1;
this.endIndex=curPage*this.pageCount;
}
/**
* 当前页(动态 由页面传递)
*/
private Integer curPage;
/**
* 上一页
* prePage=(curPage==1?1:curPage-1)
*
*/
private Integer prePage;
/**
* 下一页
* nextPage=(nextPage==totalPage)?totalPage:(curPage+1)
*
*
*/
private Integer nextPage;
//每页显示的条数 默认显示10条
private Integer pageCount=10;
/**
* 总页数
* totalPage=(totalCount%pageCount==0?totalCount/pageCount:(totalCount/pageCount+1)
*
*/
private Integer totalPage;
//数据库的总记录数(数据库查询)
private Integer totalCount;
//每页的数据放入该集合中
private List date;
/**
* 数据库开始索引和结束索引
*
* startIndex=(curPage-1)*PageCount+1
* endIndex=curPage*pageCount
*
*/
private int startIndex;
private int endIndex;
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public int getEndIndex() {
return endIndex;
}
public void setEndIndex(int endIndex) {
this.endIndex = endIndex;
}
public List getDate() {
return date;
}
public void setDate(List date) {
this.date = date;
}
public Integer getCurPage() {
return curPage;
}
public void setCurPage(Integer curPage) {
this.curPage = curPage;
}
public Integer getPrePage() {
return prePage;
}
public void setPrePage(Integer prePage) {
this.prePage = prePage;
}
public Integer getNextPage() {
return nextPage;
}
public void setNextPage(Integer nextPage) {
this.nextPage = nextPage;
}
public Integer getPageCount() {
return pageCount;
}
public void setPageCount(Integer pageCount) {
this.pageCount = pageCount;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public static void main(String[] args) {
int curPage=6;
int totalCount=27;
int pageCount=5;
PageTools_1 pt=new PageTools_1(curPage, totalCount, pageCount);
System.out.println(pt.getNextPage());
System.out.println(pt.getPrePage());
System.out.println(pt.getTotalPage());
System.out.println(pt.getStartIndex());
System.out.println(pt.getEndIndex());
}
}
2、此类连接数据库并插叙总记录数
package cn.et.model;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class OracleDbUtils_1 {
static Properties p=new Properties();
static{
//在bin目录去读jdbcmysql.properties文件
InputStream is=OracleDbUtils_1.class.getResourceAsStream("/jdbcoracle.properties");
try {
p.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
String url=p.getProperty("url"); //获取连接服务器的ip地址 端口 和数据库
String driverClass=p.getProperty("driverClass");//告诉jdbc使用的是什么数据库
String uname=p.getProperty("username");//使用哪个账号登录
String paw=p.getProperty("password");//登录密码
Class.forName(driverClass);//加载该类
Connection conn=DriverManager.getConnection(url,uname,paw);//登录
return conn;
}
//例如sql语句:select count(rowid) as cr from food
public static List<Map> query(String sql) throws Exception{
Connection conn=getConnection();
PreparedStatement pst= conn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
// 获取包含有关 ResultSet 对象列信息的 ResultSetMetaData 对象
ResultSetMetaData rsmd=rs.getMetaData();
//获取列的总数
int columnCount=rsmd.getColumnCount();
List list=new ArrayList();
while(rs.next()){
Map map=new HashMap();
for(int i=1;i<=columnCount;i++){
//获取列名
String colName=rsmd.getColumnName(i);
//获取对应的值
String colValue=rs.getString(i);
//以键值对的方式存入map集合中
map.put(colName, colValue);
}
list.add(map);
}
rs.close();
pst.close();
conn.close();
return list;
}
}
3、调用上面的方法 query() 返回总记录数
public Integer getFoodListCount(String name){
if(name==null){
name="";
}
int i=0;
//该sql语句按条件查询的总记录数
String sql="select count(rowid) as cr from food where foodname like '%"+name+"%'";
try {
//调用OracleDbUtils_1类中的query()方法 查询sql语句
List<Map> result=OracleDbUtils_1.query(sql);
//result.get(0):返回此列表中指定位置上的元素。 返回的是一个map集合 get("CR"): 返回指定键所映射的值
i= Integer.parseInt(result.get(0).get("CR").toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
4.调用上面的getFoodListCount()方法 接收总记录数 并调用PageTools_1
public PageTools_1 getFoodAll(String name,Integer curPage) throws Exception{
if(name==null){
name="";
}
//按条件查询返回记录的总数
Integer totalCount=getFoodListCount(name);
//创建PageTools_1对象 传入当前页 总记录数 每页显示的数量
PageTools_1 pt=new PageTools_1(curPage, totalCount, 6);
//sql语句查询每页显示的具体数据
List<Map> result=OracleDbUtils_1.query("select * from
(select t.*,rownum rn from food t where t.foodname like '%"+name+"%')
where rn>="+pt.getStartIndex()+" and rn<="+pt.getEndIndex());
pt.setDate(result);
return pt;
}
如果是MySQL数据库 查询分页的sql语句
pt.getStartIndex()-1:开始下标(mysql从0开始) pt.getPageCount():每页显示的条数
"select * from food where foodname like '%"+name+"%' limit" pt.getStartIndex()-1,pt.getPageCount();