package com.yulong.tag.dao;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class PageController extends BodyTagSupport {
private static String tableName;// 表名
private static String columnId;// 主键列
private static String pageIndex;// 每页要显示几行
private static Connection conn;
private static List colList;
public ResultSet findTablePage(String tableName,String columnId,String pageIndex,int next)
{
PreparedStatement pstmt=null;
try
{
HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
Object coun=request.getAttribute("count");//与上一页,下一页有关
String pageCon=(String)request.getAttribute("pageCon");//每页有几条数据
if(coun==null)
{
next=1;
}
else
{
if(Integer.parseInt(coun.toString())>=Integer.parseInt(this.findTableCount(tableName)))
{
next=Integer.parseInt(this.findTableCount(tableName));
}
else
{
next=Integer.parseInt(coun.toString());
}
}
if(pageCon==null||pageCon=="")
{
pageIndex="10";
}
else
{
pageIndex=pageCon;
}
int pIndex=Integer.parseInt(pageIndex);
String sql="select top "+pageIndex+"* from "+tableName+" where "+columnId+" not in(select top "+(next-1)*pIndex+" "+columnId+ " from "+tableName+" order by "+columnId+") order by "+columnId;
System.out.print(sql);
pstmt=this.getConn().prepareStatement(sql);
return pstmt.executeQuery();
} catch (Exception e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
// finally
// {
// this.closeAll(pstmt, con, null);
// }
//
return null;
}
//就算出数据库中共有多少条记录
public String findTableCount(String tableName)
{
ResultSet res=null;
PreparedStatement pstmt=null;
Connection con=null;
try
{
String sql="select count(*) as co from "+tableName;
con=this.getConn();
pstmt=conn.prepareStatement(sql);
res=pstmt.executeQuery();
if(res.next())
{
return res.getString("co");
}
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
finally
{
// this.closeAll(pstmt, con, res);
}
return "0";
}
//co为0时,默认10条进行分页时,共有几页
public int total(int co)
{
int to=Integer.parseInt(this.findTableCount(tableName));
int rto=0;
if(co==0)//默认分页
{
if(to%10==0)
{
rto=(to/10);
}
else
{
rto=(to/10)+1;
}
}
else//传参分页
{
if(to%co==0)
{
rto=(to/co);
}
else
{
rto=(to/co)+1;
}
}
return rto;
}
@Override
// 输出jsp页面要显示的内容
public int doStartTag() throws JspException
{
//String col = "";
ResultSet res=this.findTablePage(tableName, columnId, pageIndex, 1);//注意,11111111111111111111111111
JspWriter out=this.pageContext.getOut();
//HttpServletResponse response=
//PrintWriter pw=this.bodyContent.
StringBuffer sb=new StringBuffer();
sb.append("<script>function ok(type)" +
"{ document.forms[0].action='page.do';" +
"document.getElementById('typed').value=type;" +
"document.forms[0].submit();" +
"}</script>");
sb.append("<table class='data_table' width='960' height='50' border='1'>");
try {
while(res.next())
{
sb.append("<tr>");
for (int i = 0; i < colList.size(); i++)
{
////*********************/////////另外添加的内容 /////////************************************
//注:if中的内容可全部删除,没有影响 (为了适应我自己的项目,所以才添加的)
if(i==2)
{
//为了获取总分
int cute=Integer.parseInt(res.getString(colList.get(4)+""));
int love=Integer.parseInt(res.getString(colList.get(5)+""));
int sumco=cute+love;
if(Integer.parseInt(res.getString(colList.get(2)+""))==1)//千嘻猪
{
sb.append("<td width='120px' align='center'>千嘻猪</td>");
}
if(Integer.parseInt(res.getString(colList.get(2)+""))==2)//喵咪
{
sb.append("<td width='120px' align='center'>喵咪</td>");
}
if(Integer.parseInt(res.getString(colList.get(2)+""))==3)//哥斯拉
{
sb.append("<td width='120px' align='center'>哥斯拉</td>");
}
sb.append("<td width='120px' align='center'>"+sumco+"</td>");
}
//////**********************/////////////////////////////////////////////////***************************
else
{
sb.append("<td width='120px' align='center'>"+res.getObject(colList.get(i)+"")+"</td>");
}
//col += colList.get(i)+",";
// select id ,name,sex
}
sb.append("</tr>");
}
sb.append("</table>");
} catch (Exception e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
String pageCon=(String)request.getAttribute("pageCon");
String wangPage=(String)request.getAttribute("wangPage");
try {
sb.append("<br>");
sb.append("<form action='page.do' method='get'>");
sb.append("共"+this.findTableCount(tableName)+"条记录 每页显示");
if(pageCon==null||pageCon.equals(""))
{
sb.append("<input name='pageCon' value='10' size='3' />");
}
if(pageCon!=null)
{
sb.append("<input name='pageCon' value='"+pageCon+"' size='3' />");
}
sb.append(" 条");
sb.append("第");
if(wangPage==null||wangPage.equals(""))
{
sb.append("<input name='wangPage' value='1' size='3' />");
}
if(wangPage!=null)
{
sb.append("<input name='wangPage' value='"+wangPage+"' size='3' />");
}
sb.append("页");
sb.append("/ 共");
if(pageCon==null||pageCon.equals(""))
{
sb.append(this.total(0));
}
if(pageCon!=null)
{
sb.append(this.total(Integer.parseInt(pageCon)));
}
sb.append("页 ");
sb.append("<a href="+"javascript:ok('first')"+">第一页</a> ");
sb.append("<a href="+"javascript:ok('up')"+">上一页</a> ");
sb.append("<a href="+"javascript:ok('down')"+">下一页</a> ");
sb.append("<a href="+"javascript:ok('end')"+">最后一页 </a><input type='hidden' id='typed' value='' name='typedf'/> ");
sb.append("<input type='button' value='Go' onclick="+"ok('ok')"+">");
//sb.append("<a href="+"javascript:ok('ok')"+">go</a> ");
sb.append("<br>");
sb.append("</form>");
out.print(sb);
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println("ddddd");
return super.doStartTag();
}
public String getColumnId() {
return columnId;
}
public void setColumnId(String columnId) {
this.columnId = columnId;
}
public String getPageIndex() {
return pageIndex;
}
public void setPageIndex(String pageIndex) {
this.pageIndex = pageIndex;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public List getColList() {
return colList;
}
public void setColList(List colList) {
this.colList = colList;
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
//*********************************************
// for (int i = 0; i < colList.size(); i++)
// {
// "private objece"+co