百度分页效果之纯jsp版
程序员文章站
2022-05-03 21:15:33
数据库连接工具类">数据库连接工具类
package com.gao.page.utils;
import java.sql.connection;
import java.sql.dr...
数据库连接工具类">数据库连接工具类
package com.gao.page.utils; import java.sql.connection; import java.sql.drivermanager; public class databaseconnection { /** *一个静态方法,返回一个数据库的连接。 *这样达到了对数据库连接统一控制的目的。 */ public static connection getconnection() { connection con=null; string classforname=com.mysql.jdbc.driver; string servanddb=jdbc:mysql://localhost/gaodb; string user=root; string pwd=123; try { class.forname(classforname); con = drivermanager.getconnection(servanddb,user,pwd); } catch(exception e) { e.printstacktrace(); } return con; } }
person实体类
package com.gao.page; /** * @作者 relieved * @创建日期 2015年6月14日 * @描述 (person类) * @版本 v 1.0 */ public class person { private integer id; private string name; private integer gender; private string phone; private integer age; private string address; /** * @return the id */ public integer getid() { return id; } /** * @param id the id to set */ public void setid(integer id) { this.id = id; } /** * @return the name */ public string getname() { return name; } /** * @param name the name to set */ public void setname(string name) { this.name = name; } /** * @return the gender */ public integer getgender() { return gender; } /** * @param gender the gender to set */ public void setgender(integer gender) { this.gender = gender; } /** * @return the age */ public integer getage() { return age; } /** * @param age the age to set */ public void setage(integer age) { this.age = age; } /** * @return the phone */ public string getphone() { return phone; } /** * @param phone the phone to set */ public void setphone(string phone) { this.phone = phone; } /** * @return the address */ public string getaddress() { return address; } /** * @param address the address to set */ public void setaddress(string address) { this.address = address; } }
数据库连接操作类
package com.gao.page; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.statement; import java.util.arraylist; import java.util.list; import com.gao.page.utils.databaseconnection; public class personbean { private static connection con; //构造方法,获得数据库的连接。 static { con=databaseconnection.getconnection(); } /** * 带分页的查询 * @param pagesize * @param pagenum * @return * @throws exception */ public static listgetpersoninf(int pagenum,int pagesize)throws exception { preparedstatement pstmt=con.preparestatement(select * from person limit ?,? ); pstmt.setint(1,(pagenum-1)*pagesize); pstmt.setint(2,pagesize); resultset rst=pstmt.executequery(); list ret=new arraylist (); while(rst.next()) { person temp=new person(); temp.setid(rst.getint(1)); temp.setname(rst.getstring(2)); temp.setgender(rst.getint(3)); temp.setphone(rst.getstring(4)); temp.setage(rst.getint(5)); temp.setaddress(rst.getstring(6)); ret.add(temp); } return ret; } /** * 获取记录总条数 * @return * @throws exception */ public static int getpersoncount()throws exception { statement pstmt=con.createstatement(); string sql = select * from person; resultset rst=pstmt.executequery(sql); rst.last();//移动到最后一行 return rst.getrow(); } }
jsp页面代码">jsp页面代码
<%@page import=java.util.*,com.gao.page.person%> <%@ page language=java contenttype=text/html; charset=utf-8 pageencoding=utf-8%> <% request.setcharacterencoding(utf-8); string path = request.getcontextpath(); string basepath = request.getscheme()+://+request.getservername()+:+request.getserverport()+path+/; %>
<% final int showpages = 5;//上一页和下一页之间显示的页码数 int pagenum = request.getparameter(pagenum)==null?1:integer.parseint(request.getparameter(pagenum));//默认为首页 int pagesize = request.getparameter(pagesize)==null?6:integer.parseint(request.getparameter(pagesize));//默认为6条 list list = person.getpersoninf(pagenum,pagesize); int totalrecords = person.getpersoncount();//总数据条数 int totalpages = totalrecords%pagesize==0?(totalrecords/pagesize):(totalrecords/pagesize+1);//总页码数 int pagestart = math.max(1, pagenum - showpages/2);//显示的起始页码 int pageend = math.min(totalpages, pagestart + showpages - 1);//显示的尾页 pagestart = math.max(1, pageend - showpages + 1); %>
员工信息
<% if(list.size()>0){ for(int i=0;i ();i++)> <%}else{%> <% } }%>
id
姓名
性别
手机号
年龄
地址
<%=list.get(i).getid()%>
<%=list.get(i).getname()%>
<%=list.get(i).getgender()%>
<%=list.get(i).getphone()%>
<%=list.get(i).getage()%>
<%=list.get(i).getaddress()%>
<%=list.get(i).getid()%>
<%=list.get(i).getname()%>
<%=list.get(i).getgender()%>
<%=list.get(i).getphone()%>
<%=list.get(i).getage()%>
<%=list.get(i).getaddress()%>
<% if(pagenum>1){ %> index.jsp?pagenum=<%=(pagenum-1)%> class=n><上一页 <% } while(pagestart<=pageend){ if(pagestart==pagenum){ %> index.jsp?pagenum=<%=pagestart%>> <%=pagestart %> <% }else if(pagestart%2!=0){ %> index.jsp?pagenum=<%=pagestart%>> <%=pagestart %> <% }else{ %> index.jsp?pagenum=<%=pagestart%>> <%=pagestart %> <% } pagestart++; } if(pagenumindex.jsp?pagenum=<%=(pagenum+1)%> class=n>下一页> <% } %> ){%>
<%}else{%>
暂时没有数据!
<% } %> <script type=text/javascript>
</script>
项目完整下载路径:http://download.csdn.net/detail/gao36951/8859947
效果图如下