NO IMAGE

//操作的資料庫中的表名為:UserInfo,有三個屬性:userId,userName,userPassword(顧名思義)

首先為UserInfo表構造一個相對應的JavaBean,UserInfo.java(因為要貼的程式碼過多,這個就省略了)。

 現在正式開始:新建名為:LendDao.java 的java類,其中有兩個方法,一個是用來獲得,分頁後,每一頁應該顯示的內容( selectLend(int *,  int *) );另一個是查詢資料表中共有多少條資料,用來確定要分多少頁的( selectLendSize() )。

package com.zhou.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zhou.util.DButil;
import com.zhou.vo.UserInfo;
public class LendDao {
Connection conn = DButil.getConn();
public List<UserInfo> selectLend(int pageNow, int pageSize){
List<UserInfo> list = new ArrayList<UserInfo>();
try {
PreparedStatement pst = conn.prepareStatement("select top " pageSize " userId,userName,userPassword from UserInfo where userId not in(select top " (pageSize*(pageNow-1)) " userId from UserInfo)");
ResultSet rs = pst.executeQuery();
while(rs.next()){
UserInfo us = new UserInfo();
us.setUserId(rs.getInt("userId"));
us.setUserName(rs.getString("userName"));
us.setUserPassword(rs.getString("userPassword"));
list.add(us);
}
return list;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}finally{
DButil.CloseConn();
}
}
public int selectLendSize(){
try {
Connection conn = DButil.getConn();
PreparedStatement pst = conn.prepareStatement("select count(*) from userInfo");
ResultSet rs = pst.executeQuery();
if(rs.next()){
int pagecount = rs.getInt(1);
return pagecount;
}
return 0;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}finally{
DButil.CloseConn();
}
}
}

來解釋下兩個方法裡的sql語句:

“select top ” pageSize “userId,userName,userPassword from UserInfo where userId not in(select top ” (pageSize*(pageNow-1)) “userId from UserInfo)”

第一個要注意的地方就是 pageSize 和 (pageSize*(pageNow-1)) 都是方法傳過來的引數值,所以要注意不能直接寫在sql語句的“”中,在sql中能夠識別的是傳過來的數字0,1,….等。第二個特別要注意的地方就是 top後面一定要接空格,否則就會把top1userId看成一個列屬性,可以去看下sql語句中和top有關語句的用法。

“select count(*) from userInfo”

這句相對來說就簡單多了,就是一個count(*):統計元組個數函式,通俗點說就是算下,表中有多少行資料。

然後就是實現分頁功能的Pager.java:

package com.zhou.tool;
public class Pager {
private int pageNow;      //當前頁
private int pageSize=4;   //每頁顯示多少條記錄
private int totalPage;	  //共有多少頁
private int totalSize;		//共有多少條記錄
private boolean hasFirst;	//是否有首頁
private boolean hasPre;		//是否有前一頁
private boolean hasNext;	//是否有下一頁
private boolean hasLast;	//是否有最後一頁
public Pager(int pageNow, int totalSize){
this.pageNow = pageNow;
this.totalSize = totalSize;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
totalPage = getTotalSize()/getPageSize();
if(totalSize%pageSize!=0)
totalPage  ;
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public boolean isHasFirst() {
if(pageNow==1)
return false;
else return true;
}
public void setHasFirst(boolean hasFirst) {
this.hasFirst = hasFirst;
}
public boolean isHasPre() {
if(pageNow==1)
return false;
else return true;
}
public void setHasPre(boolean hasPre) {
this.hasPre = hasPre;
}
public boolean isHasNext() {
if(pageNow==this.getTotalPage())
return false;
else return true;
}
public void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
public boolean isHasLast() {
if(pageNow==this.getTotalPage())
return false;
else return true;
}
public void setHasLast(boolean hasLast) {
this.hasLast = hasLast;
}
}

接下來寫呼叫dao的Action,新建名為:LendAction.java的java類:

package com.zhou.action;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zhou.dao.LendDao;
import com.zhou.form.Pager;
public class LendAction extends ActionSupport{
private int pageNow=1;           //初始頁為第一頁
private int pageSize=4;			 //每頁資料為4條,可調節
public String execute() throws Exception{
LendDao dao = new LendDao();
List list = dao.selectLend(pageNow, pageSize);
Pager page = new Pager(pageNow, dao.selectLendSize());
Map session = ActionContext.getContext().getSession();
session.put("userinfo", list);
session.put("pageinfo", page);
return SUCCESS;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}

這個主要是把要儲存的資訊存到session中,表資料key:userinfo,頁碼:pageinfo。

配置Struts.xml,加頁面顯示了:

<package name=”Lendfront” extends=”struts-default”>
<action name=”lendaction” class=”com.zhou.action.LendAction”>
<result name=”success”>/lendresult.jsp</result>
</action>
</package>

這裡就說明下,配置的Action名為:lendaction,等下頁面顯示中要用到:

lendresult.jsp程式碼(body中關鍵程式碼):記得在頁面開頭加上Struts header:<%@taglib prefix=”s” uri=”/struts-tags” %>

<body>
<table border="1" width="599">
<tr>
<td valign="top"><jsp:include page="lenduserinfo.jsp"></jsp:include></td>
</tr>
<tr bgcolor="#E9EDF5" class="font1">
<td align="right">
<s:set name="page" value="#session.pageinfo"/>
<a href="lendaction?pageNow=1">首頁</a>
<s:if test="#page.hasPre">
<a href="lendaction?pageNow=<s:property value="#page.pageNow-1"/>">上一頁</a>
</s:if>
<s:else>
<a href="lendaction?pageNow=1">上一頁</a>
</s:else>
<s:if test="#page.hasNext">
<a href="lendaction?pageNow=<s:property value="#page.pageNow 1"/>">下一頁</a>
</s:if>
<s:else>
<a href="lendaction?pageNow=<s:property value="#page.totalPage"/>">下一頁</a>
</s:else>
<a href="lendaction?pageNow=<s:property value="#page.totalPage"/>">尾頁</a>
</td>
</tr>
</table>
</body>

<jsp:include page=”lenduserinfo.jsp”></jsp:include>:這句插入的頁面就是用來顯示資料庫UserInfo表中內容的

lenduserinfo.jsp中程式碼(同樣只包含body,記得加上Struts header):

<body>
<table border="2">
<tr>
<td>userId</td>   <td>userName</td>		<td>userPassword</td>
</tr>
<s:iterator value="#session.userinfo" id="lend">
<tr>
<td><s:property value="#lend.userId" /></td>
<td><s:property value="#lend.userName" /></td>
<td><s:property value="#lend.userPassword" /></td>
</tr>
</s:iterator>
</table>
</body>

呼,好長的一篇啊