/**
* 生成取中间部分数据的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);
}
}
分页语句(特殊字符替换)
最新推荐文章于 2018-11-16 23:41:00 发布