Strust2 Mysql數據庫,sql語句分頁,JSP顯示

1.

常見分頁實現方式

(1) Java程序分頁的實現主要是用List 接口中的subList(int startIndex,int endIndex)方法,這種方式也稱爲程序內存分頁。
(2) 使用數據庫的SQL語句實現數據分頁。適用於數據量較大,訪問頻度較低的操作。

最近在用Strust2寫項目,涉及到從數據庫裏取數據時使用分頁功能,網上分頁插件代碼很多,特別是js的內存分頁。
我在大學期末作品中也用過js分頁,它的一個缺點是:當數據多的時候,可能會出現錯誤。我當時出錯可能是因爲服務器緩存問題,只能取出來30多條數據。這個問題比較難打理,所以這次我特意使用Sql語句進行分頁,一次從數據庫中就只取10條數據來展示。這個就比較簡單。

2. 先上效果圖
在這裏插入圖片描述
DAO方法:

public List<Userbean> selAlluser(){   //管理員查詢全部用戶
			int pageSize=10;//取幾條數據
			int showPage=1;//當前頁
			int pageCount = 0;//總頁數
			int recordCount=0;//總記錄數
			int ushow=1;
			String show;
			ResultSet rs1;
			Statement stmt1;
			HttpSession session= ServletActionContext.getRequest().getSession();
			Dbase ab=new Dbase();
			con=(Connection) ab.getCon();
	try {	stmt1=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			String sql="select * from tb_user";
			rs1=stmt1.executeQuery(sql);
			rs1.last();//記錄最後一行
			recordCount=rs1.getRow();//根據行數得到總記錄數
			pageCount=(recordCount%pageSize==0)?(recordCount/pageSize):(recordCount/pageSize+1);//計算頁數
			show=ServletActionContext.getRequest().getParameter("showPage");//得到顯示頁面傳來的想要跳轉的頁號
			//這裏的show類型要設爲String,因爲int類型無法判空
			if(show==null){
				ushow=1;
				showPage=1;}
			else{
			ushow=Integer.parseInt(show);//轉換成int
			if(ushow<=0){
				showPage=1;
			}
			if(ushow>=pageCount){
				showPage=pageCount;
			}else{
				showPage=ushow;
				}
			if(showPage<=1){//當用戶輸入負數或0時,設置顯示第一頁
				showPage=1;
			}
			}
			  con.close();
			  rs1.close();
			  stmt1.close();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		showPage=1;
		e.printStackTrace();
	} 
			//下面是查詢操作
			List<Userbean> ulist=new ArrayList<Userbean>();
			con=(Connection) ab.getCon();
			int position=(showPage-1)*pageSize;//計算當前頁的第一條數據在整個數據庫中位於第幾條
			if(position<=0){position=0;}//0就是第一條
			String sql1="select * from tb_user limit "+position+",10";//這裏的10就是pagesize
			try {
			 stmt=con.createStatement();
			 ResultSet rs=stmt.executeQuery(sql1);
			 while(rs.next()){
				    u=new Userbean();
				    u.setUid(rs.getInt("uid"));
				    u.setUname(rs.getString("uname"));
				    u.setUpwd(rs.getString("upwd"));
				    u.setUsex(rs.getString("usex"));
				    u.setUage(rs.getInt("uage"));
				    u.setUaddr(rs.getString("uaddr"));
				    u.setUdesc(rs.getString("udesc"));
				  ulist.add(u);
			 }		 
			  con.close();
			  rs.close();
			  stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//用session保存頁號,總頁數,總數據數,便於在前臺取出
			  session.setAttribute("showpage", showPage);
	          session.setAttribute("pagecount", pageCount);
	          session.setAttribute("recordcount", recordCount);
			  return ulist;
		}

jsp頁面

<body>
<table width="90%"  border="0">
      <tr align="center" height="10">
        <td bgcolor="#33FFCC" width="15%">ID</td>
        <td width="30%" bgcolor="#99FF99">用戶名</td>
        <td width="25%" bgcolor="#99FF99">密碼</td>
        <td colspan="10" width="30%" bgcolor="#33FFCC">操作</td>
      </tr>
  
     <s:iterator value="ulist" status="status">
     <tr bgcolor="white" height="10">
       <td><s:property value="uid"/></td>
       <td><s:property value="uname"/></td>
       <td><s:property value="upwd"/></td>
       <td width="10%">
       <a href="lookuser.action?uid=<s:property value="uid"/>">查看</a></td>
       <td width="10%">
       <a href="deluser.action?uid=<s:property value="uid"/>">刪除</a>
       </td>
       <td width="10%">
       <a href="deluser.action?uid=<s:property value="uid"/>">封號</a>
       </td>
    </tr>
    </s:iterator>
      <tr>
    <td align="center" height="10%" colspan="10">  
 
  <ul class="pagination">
  <li><a href="useroper.action?showPage=<% 
  String s=session.getAttribute("showpage").toString();
  String s1=session.getAttribute("pagecount").toString();
  int pin=Integer.parseInt(s);
  int pin1=Integer.parseInt(s1);
  int pin2=Integer.parseInt(s1);
  int be=Integer.parseInt(s)-1;
  out.print(be); %>">«</a></li>
  <li><a class="<%if(pin<=1){out.print("active");}else{} %>" href="useroper.action?showPage=1">1</a></li>
  <li><a class="<%if(pin==2){out.print("active");}else{} %>" href="useroper.action?showPage=2">2</a></li>
  <li><a class="<%if(pin==3){out.print("active");}else{} %>" href="useroper.action?showPage=3">3</a></li>
  <li><a class="<%if(pin==4){out.print("active");}else{} %>" href="useroper.action?showPage=4">4</a></li>
  <li><a class="<%if(pin==5){out.print("active");}else{} %>" href="useroper.action?showPage=5">5</a></li>
  <li><a href="useroper.action?showPage=<% 
  int af=Integer.parseInt(s)+1;
  out.print(af); %>">»</a></li>
  <li><a href="useroper.action?showPage=<% out.print(session.getAttribute("pagecount")); %>">尾頁</a></li>
  </ul>
</td>
  </tr>
  </table>
  <table width="90%"  border="0">
  <tr>
    <td align="right" height="20%" style="color: white;">
    <form action="useroper.action">
    <input placeholder="輸入頁數" style="border-radius: 5px;border: none;height:30; width: 60" name="showPage" type="text" onkeyup="value=value.replace(/[^0-9]/g,'')" onpaste="value=value.replace(/[^0-9]/g,'')" oncontextmenu ="value=value.replace(/[^0-9]/g,'')"/>
    <input type="submit" name="tz" style="border-radius: 5px;height:30; width: 50;
    border:none; background-color: yellow; " value="跳轉">
    </form>
    <br>
 數據庫中有<% out.print(session.getAttribute("recordcount")); %>條數據,共<% out.print(session.getAttribute("pagecount")); %>頁,當前第<% out.print(session.getAttribute("showpage")); %>頁。<br><br>
</td>
  </tr>
</table>
</body>

css代碼:

ul.pagination {
    display: inline-block;
    padding: 2px 0px;
    margin: 0;
    width: 100%;
}

ul.pagination li {display: inline;}

ul.pagination li a {
    color: black;
    float: left;
    padding: 8px 16px;
    text-decoration: none;
    border-radius: 5px;
}

ul.pagination li a.active {
    background-color: #4CAF50;
    color: white;
    border-radius: 5px;
}

ul.pagination li a:hover:not(.active) {
	background-color: #ddd;
	}

完事!有問題歡迎留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章