Pure jsp version of Baidu paging Effect

Source: Internet
Author: User

Pure jsp version of Baidu paging Effect
Database Connection Tool

Package com. gao. page. utils; import java. SQL. connection; import java. SQL. driverManager; public class DatabaseConnection {/*** a static method that returns a database connection. * This achieves unified control over database connections. */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 entity class
Package com. gao. page;/*** @ author Relieved * @ creation date: July 22, June 14, 2015 * @ description (person class) * @ Version 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 ;}}
Database Connection operations
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; // constructor to obtain the database Connection. Static {con = DatabaseConnection. getConnection ();}/*** query with pagination * @ param pageSize * @ param pageNum * @ return * @ throws Exception */public static List
  
   
GetPersonInf (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 rst1_pstmt.exe cuteQuery (); 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;}/*** get the total number of records * @ return * @ throws Exception */public static int getPersonCount () throws Exception {Statement pstmt = con. createStatement (); String SQL = select * from person; ResultSet rst1_pstmt.exe cuteQuery (SQL); rst. last (); // move to the last line return rst. getRow ();}}
    
   
  
Jsp page code
<%@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+/;%>
<Script src =./js/jquery-1.9.1.js> </script> <% Final int showPages = 5; // number of page numbers displayed between the previous and next pages int pageNum = request. getParameter (pageNum) = null? 1: Integer. parseInt (request. getParameter (pageNum); // The default homepage is int pageSize = request. getParameter (pageSize) = null? 6: Integer. parseInt (request. getParameter (pageSize); // The default value is 6 lists. List = person. getPersonInf (pageNum, pageSize); int totalRecords = person. getPersonCount (); // total number of data entries int totalPages = totalRecords % pageSize = 0? (TotalRecords/pageSize) :( totalRecords/pageSize + 1); // total number of page numbers int pageStart = Math. max (1, pageNum-showPages/2); // display the starting page number int pageEnd = Math. min (totalPages, pageStart + showPages-1); // The Last pageStart = Math. max (1, pageEnd-showPages + 1); %> employee information
<% If (list. size ()> 0) {for (int I = 0; I <%} Else {%><%}}%>
Id Name Gender Mobile phone number Age Address
<% = 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> <previous page <%} 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 (pageNum Index. jsp? PageNum = <% = (pageNum + 1) %> class = n> next page ><%}%>

<%} Else {%> no data! <% }%> <Script type = text/javascript> </script>

Full Project download path: http://download.csdn.net/detail/gao36951/8859947
As follows:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.