1,首先前台jsp页面发出请求
<a class=menuchild href="search?topage=1" target=right><!--链接到查询所有成员的servlet-->人员查找</a>
<!--topage是指当前页数,首先一开始查的是第一页,所以传递的是智能是1
-->
2,然后就是servlet来处理jsp页面发过来的请求
package action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.PageBean;
import util.helper;
public class search extends HttpServlet {
/**
* Constructor of the object.
*/
public search() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=gb2312");
request.setCharacterEncoding("gb2312");
try {
PageBean pagebean = new PageBean();
helper contact = new helper();//数据库的操作类
int pagecount = pagebean.countPage();//获取总共的页数
String topage = request.getParameter("topage");//获取当前的页码
if (Integer.parseInt(topage) > pagecount) { //判断当前的页码是否越界,如果是的话就进行处理
topage =String.valueOf(pagecount);
} else if (Integer.parseInt(topage) <= 0) {
topage = "1";
}
ArrayList news = contact.getData(topage, pagecount);
request.getSession().setAttribute("pagecount", pagecount);
request.getSession().setAttribute("showpage", topage);//将当前的页码返回给页面,这样进行计算
request.getSession().setAttribute("news1", news);
response.sendRedirect("search.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
3,其中的PageBean
package util;
public class PageBean {//分页操作的类
public int maxPage; // 一共有多少页
public int maxRowCount; // 一共有多少行
public int pagesize = 5; // 每页多少行
public PageBean() {
}
public int countPage() throws Exception {
int maxRowCount=this.MaxCount();
/*if (maxRowCount % this.pagesize == 0) {//方法一
maxPage = maxRowCount / this.pagesize;
} else {
maxPage =maxRowCount/ this.pagesize + 1;
}*/
maxPage = (maxRowCount-1)/this.pagesize+1;//方法二
return maxPage;
}
public int MaxCount() throws Exception {
helper contact=new helper();
this.maxRowCount = contact.getAvailableCount(); // 得到总行数
return maxRowCount;
}
}
3,数据库的处理
package util;
public class PageBean {//分页操作的类
public int maxPage; // 一共有多少页
public int maxRowCount; // 一共有多少行
public int pagesize = 5; // 每页多少行
public PageBean() {
}
public int countPage() throws Exception {
int maxRowCount=this.MaxCount();
/*if (maxRowCount % this.pagesize == 0) {//方法一
maxPage = maxRowCount / this.pagesize;
} else {
maxPage =maxRowCount/ this.pagesize + 1;
}*/
maxPage = (maxRowCount-1)/this.pagesize+1;//方法二
return maxPage;
}
public int MaxCount() throws Exception {
helper contact=new helper();
this.maxRowCount = contact.getAvailableCount(); // 得到总行数
return maxRowCount;
}
}
4,链接数据库的代码(里面有需要用的函数,位置你们可以自己换)
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.mysql.jdbc.PreparedStatement;
import model.member;
public final class helper {
public Connection connstr = null;
public ResultSet res = null;
public PreparedStatement prase = null;
public static final Connection getConnection(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/syyz??useUnicode=true&characterEncoding=utf-8&autoReconnect=true", "root", "123456");
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage()+"OK");
e.printStackTrace();
} catch (SQLException e) {
System.out.println(e.getMessage()+"OK");
e.printStackTrace();
}
return conn;
}
public static final void close(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (connection!= null) {
connection.close();
}
if (statement!= null) {
statement.close();
}
if (resultSet!= null) {
resultSet.close();
}
} catch (SQLException e) {
System.out.println(e.getMessage()+"OK");
e.printStackTrace();
}
}
/** *返回要查询的记录数 */
public int getAvailableCount() throws Exception {
int ret = 0;
String strSql = "select count(*) from member";
Connection conn = this.getConnection();
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(strSql);
while (rset.next()) {
ret = rset.getInt(1);
}
return ret;
}
/*获取查寻的记录数*/
public ArrayList getData(String topage, int pagecount)//topage是要转向的页数,pagecount是总页数
throws NumberFormatException, SQLException {
String sql = "select * from member";
Statement state = null;
ResultSet rs = null;
int pagesize = 5;//每页的记录数
int showpage = 1;//当前的页数
ArrayList list = new ArrayList();
int j = 0;
try {
Connection conn = this.getConnection();
state = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = state.executeQuery(sql);
} catch (Exception e) {
System.out.println("exception");
}
if (!rs.next()) {
System.out.println("no records!");
} else {
rs.last();
if (topage != null) {
showpage = Integer.parseInt(topage);
if (showpage > pagecount) {
showpage = pagecount;
} else if (showpage <= 0) {
showpage = 1;
}
}
rs.absolute((showpage - 1) * pagesize + 1);//指针移动在结果集中的位置
for (int i = 1; i <= pagesize; i++) {
member mem = new member();
mem.setMemberName(rs.getString(1));
mem.setEmail(rs.getString(2));
mem.setImageFile(rs.getString(3));
list.add(j, mem);
++j;
if (!rs.next())
break;
}
}
return list;
}
}