分页语句(特殊字符替换)

本文介绍了在Oracle和MySQL数据库中实现特定范围数据查询的方法。针对不同数据库特性,提供了生成带有分页限制的SQL语句的具体实现,包括如何通过ROWNUM在Oracle中获取指定区间的记录,以及MySQL中LIMIT关键字的应用。

/**
* 生成取中间部分数据的sql语句, Oracle专用
* @param sql String 带排序的原始sql语句
* @param lowLimit int 起始行
* @param highLimit int 截止行
* @return String
* @author Zhang zhongguang
*/
public static String getOraLimitString(String sql, int lowLimit,
int highLimit)
{
StringBuffer pagingSelect = new StringBuffer(sql.length() + 64);
pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
pagingSelect.append(sql);
pagingSelect.append(" ) row_ where rownum <= ");
pagingSelect.append(highLimit);
pagingSelect.append(" ) where rownum_ >= ");
pagingSelect.append(lowLimit);
return pagingSelect.toString();
}

//select * from (select row_.*, rownum rownum_ from ( select * from 表名) row_ where rownum<=10) where rownum_>=1

/**
* 生成取中间部分数据的sql语句, MySQL专用
* @param sql String 带排序的原始sql语句
* @param lowLimit int 起始行
* @param highLimit int 截止行
* @return String
* @author Zhang zhongguang
*/
public static String getMySQLLimitString(String sql, int lowLimit,
int highLimit)
{
StringBuffer pagingSelect = new StringBuffer(sql.length() + 16);
pagingSelect.append(sql);
pagingSelect.append(" limit ");
pagingSelect.append(lowLimit);
pagingSelect.append(", ");
pagingSelect.append(highLimit);
return pagingSelect.toString();
}

public static void main(String[] args)
{
String miscId = "AAAs'%";
miscId=miscId.replaceAll("';", "';';");
miscId=miscId.replaceAll("_","[_]");
miscId=miscId.replaceAll("%","[%]");
miscId=miscId.replaceAll("'","''");
System.out.println("#####"+miscId);
}

public class Page
{
/**
* @param args
* @throws IOException
*/
public static void pager(HttpServletRequest request, JspWriter out)
{
String reqParam = request.getQueryString() == null ? "" : request.getQueryString();
String url = request.getRequestURL() + "?";
int pagesize = -1;
int pageindex = -1;
int totalcount = (Integer) request.getAttribute("totalcount");

if (reqParam.indexOf("pagesize") == -1)
{
pagesize = 10;
}
else
{
try
{
pagesize = Integer.parseInt(request.getParameter("pagesize"));
}
catch (NumberFormatException e)
{
pagesize = 10;
}
}

if (reqParam.indexOf("pageindex") == -1)
{
pageindex = 1;
}
else
{
try
{
pageindex = Integer.parseInt(request.getParameter("pageindex"));
}
catch (NumberFormatException e)
{
pageindex = 1;
}
}

String result = "";
if (totalcount > pagesize)
{
if (pageindex == 1)
{
result = "1 ";
for (int page = 2; (page <= (totalcount+pagesize-1)/pagesize) && (page <= 10); page++)
{
result = result + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=" + page + "\">[" + page + "]</a> ";
}
result = result + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=2" + "\">下一页</a> " + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=" + (totalcount+pagesize-1)/pagesize + "\">尾页</a> ";
}
else if (pageindex == (totalcount+pagesize-1)/pagesize)
{
result = result + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=1\">首页</a> " + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=" + (pageindex - 1) + "\">上一页</a> ";
for (int page = Math.max((totalcount+pagesize-1)/pagesize - 10, 1); page <= ((totalcount+pagesize-1)/pagesize - 1); page++)
{
result = result + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=" + page + "\">[" + page + "]</a> ";
}
result = result + (totalcount+pagesize-1)/pagesize;
}
else
{
result = result + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=1\">首页</a> " + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=" + (pageindex - 1) + "\">上一页</a> ";
int i = 0;
for (int page = (int) Math.max(pageindex - 5, 1); page <= (pageindex - 1); page++)
{
result = result + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=" + page + "\">[" + page + "]</a> ";
i = i + 1;
}
result = result + pageindex + " ";
for (int page = pageindex + 1; (page <= (totalcount+pagesize-1)/pagesize) && (page <= (pageindex + 9 - i)); page++)
{
result = result + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=" + page + "\">[" + page + "]</a> ";
}
result = result + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=" + (pageindex + 1) + "\">下一页</a> " + "<a href=\"" + url + "pagesize=" + pagesize + "&pageindex=" + (totalcount+pagesize-1)/pagesize + "\">尾页</a> ";
}
result = result + "    第 " + pageindex + "/ " + (totalcount+pagesize-1)/pagesize + "页 ";
}
try
{
out.print(result + "一共 " + totalcount + " 条数据  ");
}
catch (IOException e)
{
}
}

public static void main(String[] args)
{
System.out.println((14+10-1)/10);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值