private boolean setCounts(){
ResultSet rs = null;
Connection conn = null;
PreparedStatement stt = null;
boolean flag = false;
this.itemCount = 3;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdemo", "gac", "");
String sql = "select count(*) from users";
stt = conn.prepareStatement(sql);
rs = stt.executeQuery();
if(rs.next()){
this.counts = rs.getInt(1);
System.out.println("总页数"+ this.counts);
if(0==this.counts % this.itemCount){
this.pagesize = this.counts/this.itemCount;
}else{
this.pagesize = this.counts/this.itemCount + 1;
}
flag = true;
}
}catch(Exception e){
if (null != rs) {
try {
rs.close();
if (null != stt) {
stt.close();
}
if (null != conn) {
conn.close();
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}
}
return flag;
}
//获取当前页的指定数量itemCounts 的用户数
private ResultSet getUsers(int pageCurrent,int itemCounts){
ResultSet rs = null;
Connection conn = null;
PreparedStatement stt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdemo", "gac", "");
String sql = "SELECT * FROM users LIMIT ?,?";
stt = conn.prepareStatement(sql);
stt.setInt(1, (pageCurrent-1)*itemCounts);
stt.setInt(2, itemCounts);
rs = stt.executeQuery();
System.out.println("*************getUsers*******************************");
}catch(Exception e){
try {
if(null != rs){
rs.close();
}
if (null != stt) {
stt.close();
}
if (null != conn) {
conn.close();
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if( null!=rs)
return rs;
else
return null;
}
doPost方法中的主要内容:
pw.println("<html>");
pw.println("<body><center>");
pw.println("<br><img src=images/1.gif><br>");//给浏览器输出图片
pw.println("<a href=login>返回重新登陆<a/>");
pw.println("<table border=1;width:980px; height:200px>");//设置表格
pw.println("<tr><th>id</th><th>name</th> <th>pwd</th><th>email</th></tr>");//表格的头部
if(setCounts()){//获取当前页面的总数 和总行数 获取成功返回true
rs = getUsers(pageCurrent,this.itemCount);//得到当前也需要显示的数据的结果集
//将结果集打印出来
while(rs.next()){
pw.println("<tr>");
pw.println("<th>" +rs.getString("id")+"</th>");
pw.println("<th>" +rs.getString("firstname")+"</th>");
pw.println("<th>" +rs.getString("pwd")+"</th>");
pw.println("<th>" +rs.getString("email")+"</th>");
pw.println("</tr>");
}
}
pw.println("</table>");
//总页数小于三的时候
if(this.pagesize <= 3){
for(int i = 1; i <= this.pagesize; i++)
pw.println("<a href=wel?current="+i+">"+"第"+i+"页</a>");
}else{
//直接选页数 会钞超过原有页数 因为pageCurrent+3可能大于pageSize
if(this.pageCurrent + 2 >= this.pagesize){
if(this.pageCurrent !=1)
pw.println("<a href=wel?current="+(this.pageCurrent-1)+">上一页</a>");//页面内容跳转是通过 链接给本页面发送请求并传递参数
for(int i = this.pagesize-2; i <= this.pagesize;i++){
pw.println("<a href=wel?current="+i+">"+"第"+i+"页</a>");
}
}else{
if(this.pageCurrent !=1)
pw.println("<a href=wel?current="+(this.pageCurrent-1)+">上一页</a>");
for(int i = this.pageCurrent; i < this.pageCurrent + 3; i++)
pw.println("<a href=wel?current="+i+">"+"第"+i+"页</a>");
if(this.pageCurrent != this.pagesize-2)
pw.println("<a href=wel?current="+(this.pageCurrent+1)+">下一页</a>");
}
}
pw.println("</html>");
pw.println("</center></body>");
}catch(Exception e){
e.printStackTrace();
}