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;
}
}
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 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 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();
}
}
<%@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 <%}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(pageNum index.jsp?pageNum=<%=(pageNum+1)%> class=n>下一頁> <% } %>
<%}else{%>
暫時沒有數據!
<% } %> <script type=text/javascript>
</script>
項目完整下載路徑:http://download.csdn.net/detail/gao36951/8859947
效果圖如下