java分页

public class PageTag implements Tag {

private PageContext pageContext;

private int pageNum;
private String tableName;
public PageTag(){
System.out.println("PageTag...............");
}
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return 0;
}

public int doStartTag() throws JspException {
// TODO Auto-generated method stub
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        String url =  request.getServletPath();
        url = url.substring(1);
Connection con = DBConnection.getConnection();
int totalRows = 0;
int pages = 0;
System.out.println("---------tableName--------->" + tableName);
/* if (tableName == null || tableName.equals("")) {
this.tableName = pageContext.getSession().getAttribute("tn") + "";
} else {
System.out.println(request.getSession().getId() + "-----------");
request.getSession().setAttribute("tn", tableName);
}*/
JspWriter out =  pageContext.getOut();

String sql = "select table_name from tabs";
try {
Statement st = con.createStatement();
///查询所有的表名
ResultSet rs = st.executeQuery(sql);
out.println("<script language='javascript'>" +
" function stn(){" +
" document.myf.submit(); " +

" }" +
"</script>");
out.println("<form name='myf' action='" + url +"'>" +
"<select id='sn' name='tablename' onchange='stn()'>");
while (rs.next()) {
String t = rs.getString(1);
out.println("<option value='" + t + "'>" + t + "</option>");
}
out.println("</select></form><br>");

tableName = request.getParameter("tablename");
if (tableName == null || tableName.equals("null") || tableName.equals("")) {
this.tableName = request.getSession().getAttribute("tn") + "";
System.out.println("sessinoid:" + request.getSession().getId() + "-- tableName:" + tableName);
if (tableName.equals("null")) {
out.println("请选择表名");
return  this.SKIP_BODY;
}
} else {
request.getSession().setAttribute("tn", tableName);
System.out.println("sessinoid:" + request.getSession().getId() );
}
out.println("tableName:" + tableName + "<br>" );
//查询表中行数
sql = "select count(1) from " + tableName;
rs = st.executeQuery(sql);
rs.next();
totalRows = rs.getInt(1);
pages = (int)Math.ceil((1.0 * totalRows)/pageNum);
String page = pageContext.getRequest().getParameter("page");
if (page == null) {
page = 1 + "";
}
int currentPage = Integer.parseInt(page);
int startRow = (currentPage - 1 ) * pageNum + 1;
int endRow = currentPage * pageNum;

sql = "select * from  ( select rownum num, tn.*  from " + tableName + " tn ) mt where mt.num >= "
+ startRow + " and mt.num <= " + endRow;


rs = st.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int colNum = rsmd.getColumnCount();

out.println("<table border=1>");
while (rs.next()) {
out.println("<tr>");
for (int i = 1; i <= colNum ; i++) {
out.print("<td>" + rs.getObject(i) + "</td>");
}
out.println("</tr>");

}
out.println("</table>");

out.println("<a href=" + url +  "?page=1>首页</a>");

if (currentPage > 1) {
out.println("<a href=" + url +  "?page=" + (currentPage - 1) + ">上一页</a>");
}

if (currentPage < pages) {
out.println("<a href=" + url +  "?page=" + (currentPage + 1) + ">下一页</a>");
}
out.println("<a href=" + url +  "?page=" + pages + ">尾页</a>");



} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBConnection.closeConnection(con);
}

this.tableName = null;
this.pageNum = 0;
return 0;
}

public Tag getParent() {
// TODO Auto-generated method stub
return null;
}

public void release() {
// TODO Auto-generated method stub

}

public void setPageContext(PageContext arg0) {
// TODO Auto-generated method stub
pageContext = arg0;

}

public void setParent(Tag arg0) {
// TODO Auto-generated method stub

}

public int getPageNum() {
return pageNum;
}

public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}

public String getTableName() {
return tableName;
}

public void setTableName(String tableName) {

this.tableName = tableName;
}

}

你可能感兴趣的:(java,sql)