简单实现了分页、删除、更新操作,原生分页其实很简单,只需要获取当前页数以及每页显示条数,通过后台查出当前页数据存入List集合中,然后request.setAttribute()将结果反馈给jsp
整个工程输入localhost:8080/Page/PageServlet即可访问
数据库cby localhost:3306
user user_id username
数据库连接
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConnection {
private static Connection conn=null;
private String driver="com.mysql.jdbc.Driver";
private String url="jdbc:mysql://localhost:3306/cby?useUnicode=true&characterEncoding=UTF-8";
private String user="root";
private String password="123";
private DbConnection(){
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
if(conn==null){
new DbConnection();
}
return conn;
}
}
DAO
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.bean.User;
public class PageDao {
private static Connection conn;
private static ResultSet rs;
private static PreparedStatement ps;
private static int pagesize = 3;
static{
conn = DbConnection.getConnection();
}
//遍历当前页信息
public static ArrayList getUserList(int pageNo) {
// TODO Auto-generated method stub
ArrayList list = new ArrayList();
int begin = (pageNo-1)*pagesize;
int end = pagesize;
try {
ps = conn.prepareStatement("SELECT user_id,username from user LIMIT "+begin+","+end);
rs = ps.executeQuery();
while(rs.next()){
User user = new User();
user.setUser_id(rs.getInt(1));
user.setUsername(rs.getString(2));
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//获取总信息条数,计算总页数
public static int getTotalPage() {
// TODO Auto-generated method stub
int totalCount = 0;
int totalPage = 0;
try {
ps = conn.prepareStatement("select count(user_id) from user");
rs = ps.executeQuery();
if(rs.next()){
totalCount = rs.getInt(1);
totalPage = (totalCount-1)/pagesize+1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return totalPage;
}
//删除
public static void delete(int id){
try {
ps = conn.prepareStatement("delete from user where user_id = "+id);
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//更新
public static void update(int id,String name){
try {
ps = conn.prepareStatement("update user set username = "+name+" where user_id = "+id);
ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
实体类
package com.bean;
public class User {
private int user_id;
private String username;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
分页Servlet
package com.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.User;
import com.dao.PageDao;
public class PageServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//获取当前页码
String currentPageNo = request.getParameter("currentpageno");
int pageNo = 1;
if(currentPageNo!=null){
pageNo = Integer.parseInt(currentPageNo);
}
ArrayList userList=PageDao.getUserList(pageNo);
request.setAttribute("userList", userList);
request.setAttribute("currentpageno", pageNo);
request.setAttribute("totalPage", PageDao.getTotalPage());
request.getRequestDispatcher("/showUserList.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
删除Servlet
package com.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.User;
import com.dao.PageDao;
public class deleteServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException{
String id = request.getParameter("user_id");
//删除操作
PageDao.delete(Integer.parseInt(id));
//仍然是分页
String currentPageNo = request.getParameter("currentpageno");
int pageNo = 1;
if(currentPageNo!=null){
pageNo = Integer.parseInt(currentPageNo);
}
ArrayList userList=PageDao.getUserList(pageNo);
request.setAttribute("userList", userList);
request.setAttribute("currentpageno", pageNo);
request.setAttribute("totalPage", PageDao.getTotalPage());
request.getRequestDispatcher("/showUserList.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
跳转更新页面
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class updateForward extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException{
String id = request.getParameter("user_id");
request.setAttribute("userId", id);
request.getRequestDispatcher("/updateUser.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
更新Servlet
package com.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.User;
import com.dao.PageDao;
public class updateServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException{
this.doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String userId = request.getParameter("userId");
String username = request.getParameter("userName");
PageDao.update(Integer.parseInt(userId), username);
String currentPageNo = request.getParameter("currentpageno");
int pageNum = 1;
if(currentPageNo!=null){
pageNum = Integer.parseInt(currentPageNo);
}
ArrayList userList=PageDao.getUserList(pageNum);
request.setAttribute("userList", userList);
request.setAttribute("currentpageno", pageNum);
request.setAttribute("totalPage", PageDao.getTotalPage());
request.getRequestDispatcher("/showUserList.jsp").forward(request, response);
}
}
分页JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Page
用户编号
用户姓名
操作
${list.user_id}
${list.username}
删除
修改
首页
上一页
下一页
尾页
首页
上一页
下一页
尾页
更新页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
addUser
web.xml
PageServlet
com.servlet.PageServlet
PageServlet
/PageServlet
deleteServlet
com.servlet.deleteServlet
deleteServlet
/deleteServlet
updateForward
com.servlet.updateForward
updateForward
/updateForward
updateServlet
com.servlet.updateServlet
updateServlet
/updateUser