package real.tags;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.sql.rowset.CachedRowSet;
import com.sun.rowset.CachedRowSetImpl;
public class Pagination extends BodyTagSupport {
private CachedRowSet _crs;
private CachedRowSet _rs;
private String _sql;
private String _method;
private int _curpage;
private int _totalpage;
private int _count;
private String _url;
private String _driver;
// private String _searthText="where 'a='a'";
// 表名
private String tableName;
private String databaseName;
private String var;
private String curPageCountName="curPageCount";
private String totalPageCountName="totalPageCount";
private String username;
private String password;
private String dataBaseAddressName="localhost";
private String dataBaseAddressPort="1433";
private String sql="select * from "+tableName;
// 要显示的页数
private String count="5";
private void load()
{
HttpServletRequest req=(HttpServletRequest)this.pageContext.getRequest();
this._method=req.getParameter("method");
// this._searthText=req.getParameter("searthText");
this._crs=(CachedRowSet)this.pageContext.getSession().getAttribute("crs");
if(this._crs==null)
{
init();
}
// if(this._searthText!=null){
// this._sql="select * from "+tableName+_searthText;
// try {
// this._crs.setCommand(this._sql);
// this._crs.execute();
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
if(_method==null)
{
_method="first";
}
System.out.println("-------------------------"+this.pageContext.getSession().getAttribute(curPageCountName).toString());
this._curpage=Integer.valueOf(this.pageContext.getSession().getAttribute(curPageCountName).toString());
this._totalpage=Integer.valueOf(this.pageContext.getSession().getAttribute(totalPageCountName).toString());
try{
this._crs.afterLast();
if(_method.equals("first")){
while(this._crs.previousPage()){}
this._crs.beforeFirst();
_curpage=1;
}
else if(_method.equals("priv"))
{
if(this._crs.previousPage()) _curpage--;
else{
this._crs.beforeFirst();
}
}
else if(_method.equals("next")){
if(this._crs.nextPage())_curpage++;
else{
this._crs.previousPage();
}
}
else{
while(this._crs.nextPage()){}
_curpage=_totalpage;
this._crs.afterLast();
this._crs.previousPage();
}
if(this._crs.next()){
ResultSetMetaData me=_crs.getMetaData();
HashMap hm = new HashMap();
for(int i=1;i<_crs.getMetaData().getColumnCount()+1;i++){
hm.put(me.getColumnName(i),_crs.getObject(i));
}
// this.pageContext.getRequest().setAttribute(var, _crs.getObject(1));
this.pageContext.getRequest().setAttribute(var, hm);
// System.out.println();
}
}catch(SQLException ex){
ex.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
public int doAfterBody() throws JspException {
try {
while(_crs.next()){
ResultSetMetaData me=_crs.getMetaData();
HashMap hm = new HashMap();
for(int i=1;i<=_crs.getMetaData().getColumnCount();i++){
hm.put(me.getColumnName(i),_crs.getObject(i));
}
this.pageContext.getRequest().setAttribute(var, hm);
return EVAL_BODY_AGAIN;
}
} catch (SQLException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
@Override
public int doStartTag() throws JspException {
load();
return EVAL_BODY_INCLUDE;
}
@Override
public void doInitBody() throws JspException {
};
public void update(){
this.pageContext.getSession().setAttribute("crs", _crs);
//this.pageContext.getSession().setAttribute("searthText" ,_searthText);
this.pageContext.getSession().setAttribute(curPageCountName, _curpage);
this.pageContext.getSession().setAttribute(totalPageCountName, _totalpage);
}
public void init(){
try {
this._curpage=1;
this._method="first";
this._sql=sql;
this._count=Integer.valueOf(count);
this._crs=new CachedRowSetImpl();
this._driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
this._url="jdbc:sqlserver://"+this.dataBaseAddressName+":"+this.dataBaseAddressPort+";databasename="+databaseName;
this._crs.setUsername(username!=null?username:"sa");
this._crs.setPassword(password!=null?password:"sa");
this._crs.setUrl(_url);
this._crs.setCommand(_sql);
this._rs=this._crs.createCopy();
this._crs.setPageSize(Integer.valueOf(count));
Class.forName(this._driver);
this._crs.execute();
while(this._crs.nextPage()){this._totalpage++;}
while(this._crs.previousPage()){}
update();
}catch(ClassNotFoundException ex){
System.out.println("请导入SUN公司连接数据库的JAR包");
}catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
// @Override
public int doEndTag() throws JspException {
update();
return EVAL_PAGE;
}
public String getCurPageCountName() {
return curPageCountName;
}
public void setCurPageCountName(String curPageCountName) {
this.curPageCountName = curPageCountName;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getTotalPageCountName() {
return totalPageCountName;
}
public void setTotalPageCountName(String totalPageCountName) {
this.totalPageCountName = totalPageCountName;
}
public String getDatabaseName() {
return databaseName;
}
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getDataBaseAddressName() {
return dataBaseAddressName;
}
public void setDataBaseAddressName(String dataBaseAddressName) {
this.dataBaseAddressName = dataBaseAddressName;
}
public String getDataBaseAddressPort() {
return dataBaseAddressPort;
}
public void setDataBaseAddressPort(String dataBaseAddressPort) {
this.dataBaseAddressPort = dataBaseAddressPort;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
}