jsp實現的簡單分頁,適合新手用
阿新 • • 發佈:2018-12-03
<%@ page language="java" import="java.util.*,java.sql.*,com.ejb.pojo.User" pageEncoding="utf-8"%> <%--<jsp:useBean id="db" class="com.ejb.bean.DBUtil"></jsp:useBean> --%> <jsp:useBean id="dao" class="com.ejb.bean.dao.BaseDAO"></jsp:useBean> <jsp:useBean id="pg" class="com.ejb.pojo.Page"></jsp:useBean> <jsp:useBean id="user" class="com.ejb.pojo.User"></jsp:useBean> <html> <head> <script type="text/javascript"> function getPageData(){ var a = document.getElementById('page'); if(a.value == ''){ alert('不能為空!') return ; } window.location.href = 'showPage.jsp?index='+(a.value-1)*10;//頁碼乘以顯示的資料量 } </script> </head> <body> <% int a = Integer.parseInt( request.getParameter("index")==null?"0":request.getParameter("index"));//頁碼 pg.setPage(a); pg = dao.getPage("t_user",pg,user); out.print("<table border='1'><tr><td>ID</td><td>姓名</td><td>密碼</td></tr>"); for(User u :(List<User>)pg.getList()){ out.print("<tr><td>"+u.getId()+"</td><td>"+u.getName()+"</td><td>"+u.getPassword()+"" +"</td></tr>"); } out.print("</table>"); %> <a href="showPage.jsp?index=<%=(a-10)<=0?"0":a-10 %>">上一頁</a> 第<input style="width: 37px" type="text" id="page"></>頁 <input type="button" value="GO" onclick="getPageData();"/> <a href="showPage.jsp?index=<%=a+10 %>">下一頁</a> </body> </html>
package com.ejb.bean.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.ejb.bean.DBUtil; import com.ejb.pojo.Page; import com.ejb.pojo.User; public class BaseDAO <T> { private StringBuffer sqlPage = new StringBuffer(); /** * 通用分頁查詢 * */ @SuppressWarnings("unchecked") public Page getPage(String tableName,Page page, T t){ List list = new ArrayList(); User user = null; sqlPage.append("select * from"); sqlPage.append(" "+tableName+" "); sqlPage.append(" limit "+page.getPage()+","+page.getPageSize()); try{ ResultSet rs = DBUtil.getConn(). createStatement(). executeQuery(sqlPage.toString()); while( rs.next()){ if( t instanceof User){ user = new User(); user.setId(rs.getInt(1)); user.setName(rs.getString(2)); user.setPassword(rs.getString(3)); } list.add(user); } }catch(SQLException e) { }catch(InstantiationException e) { }catch(IllegalAccessException e) { } page.setList(list); return page; } } package com.ejb.pojo; import java.util.List; public class Page { private int page = 0;//預設頁碼 private int pageSize = 20;//每頁大小 private List list;//儲存實體 public List getList() { return list; } public void setList(List list) { this.list = list; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } } package com.ejb.pojo; public class User extends BasePO{ private int id = 0; private String name = ""; private String password = ""; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } package com.ejb.bean; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.List; import com.mysql.jdbc.Driver; public class DBUtil { private static Connection conn = null; public static Connection getConn() throws InstantiationException, IllegalAccessException, SQLException{ String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "root"; try { DriverManager.registerDriver((Driver)Class.forName("com.mysql.jdbc.Driver"). newInstance()); try { conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } return conn; } }