默认状态 | 客户ID | 客户名 | //这里的这个复选框也就是表格里最左上角那个单元格里的复选框,没有用for循环控制输出.将来用JS脚本控制全选/不选
---|
for(int i=1;i <=pageSize;i++)
{
if(rowSet.next()){
str.append(""); ");}
str.append(""); ");
str.append("
for(int k=1;k <=fieldnum;k++)
{
str.append(""+rowSet.getString(k)+" ");
}
str.append("
}
package database.operation;
import java.sql.*;
import com.sun.rowset.CachedRowSetImpl;
public class ShowRecordByPage
{ int pageSize=0; //每页显示的记录数
int pageAllCount=0; //分页后的总页数
int showPage=1 ; //当前显示页
StringBuffer presentPageResult; //显示当前页内容
CachedRowSetImpl rowSet; //用于存储ResultSet对象
String databaseName=""; //数据库名称
String tableName=""; //表的名字
String user="" ; //用户
String password="" ; //密码
String field[]=new String[100] ; //field字段
int fieldnum=0;
public ShowRecordByPage()
{ presentPageResult=new StringBuffer();
try{ Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(Exception e){
System.out.println("加载驱动失败!~"+e);}
}
public void setPageSize(int size)
{ pageSize=size;
fieldnum=0;
String uri="jdbc:mysql://localhost:3306/deltest?useUnicode=true&characterEncoding=EUC_CN";
try{ Connection con=DriverManager.getConnection(uri,user,password);
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null,null,tableName,null);
int k=0;
while(rs1.next())
{ fieldnum++;
field[k]=rs1.getString(4); //获取字段的名字
k++;
}
Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs=sql.executeQuery("SELECT * FROM test");
rowSet=new CachedRowSetImpl(); //创建行集对象
rowSet.populate(rs);
con.close(); //关闭连接
rowSet.last();
int m=rowSet.getRow(); //总行数
int n=pageSize;
pageAllCount=((m%n)==0)?(m/n):(m/n+1);
}
catch(Exception exp){}
}
public int getPageSize()
{ return pageSize;
}
public int getPageAllCount()
{ return pageAllCount;
}
public void setShowPage(int n)
{ showPage=n;
}
public int getShowPage()
{ return showPage;
}
public StringBuffer getPresentPageResult()
{ if(showPage>pageAllCount)
showPage=1;
if(showPage<=0)
showPage=pageAllCount;
presentPageResult=show(showPage);
return presentPageResult;
}
public StringBuffer show(int page)
{ StringBuffer str=new StringBuffer();
try{ rowSet.absolute((page-1)*pageSize+1);
for(int i=1;i<=pageSize;i++)
{ str.append(""); ");}
if(rowSet.next()){
str.append(""); ");
str.append("
for(int k=1;k<=fieldnum;k++)
{
str.append(""+rowSet.getString(k)+" ");
}
str.append("
}
}
catch(SQLException exp){}
return str;
}
public void setDatabaseName(String s)
{ databaseName=s.trim();
}
public String getDatabaseName()
{ return databaseName;
}
public void setTableName(String s)
{ tableName=s.trim();
}
public String getTableName()
{ return tableName;
}
public void setPassword(String s)
{ password=s.trim();;
}
public void setUser(String s)
{ user=s.trim();
}
public String getUser()
{ return user;
}
}
public StringBuffer show(int page)
{ StringBuffer str=new StringBuffer();
try{ rowSet.absolute((page-1)*pageSize+1);
for(int i=1;i<=pageSize;i++)
{ str.append(""); ");}
if(!rowSet.isAfterLast()){
str.append(""); ");
str.append("
for(int k=1;k<=fieldnum;k++)
{
str.append(""+rowSet.getString(k)+" ");
}
str.append("
rowSet.next();
}
}
catch(SQLException exp){}
return str;
}
public StringBuffer show(int page)
{ StringBuffer str=new StringBuffer();
try{ rowSet.absolute((page-1)*pageSize+1);
for(int i=1;i<=pageSize;i++)
{ str.append(""); ");
if(!rowSet.isAfterLast()){
str.append(""); ");
str.append("
for(int k=1;k<=fieldnum;k++)
{
str.append(""+rowSet.getString(k)+" ");
}
str.append("
rowSet.next();
}
}
}
catch(SQLException exp){}
return str;
}