功能图
package com.ge.entity;
import java.util.List;
public class Page {
private int currentPage; //当前页数
private int totalPage; //总页数
private int pageSize; //每页的记录数
private int totalSize; //总的记录数
private List list;
public Page() {
}
public Page(int currentPage, int totalPage, int pageSize, int totalSize, List list) {
this.currentPage = currentPage;
this.totalPage = totalPage;
this.pageSize = pageSize;
this.totalSize = totalSize;
this.list = list;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Override
public String toString() {
return "Page{" +
"currentPage=" + currentPage +
", totalPage=" + totalPage +
", pageSize=" + pageSize +
", totalSize=" + totalSize +
", list=" + list +
'}';
}
}
package com.ge.entity;
public class Student {
private int id;
private String name;
private int age;
private String gender;
private String address;
public Student() {
}
public Student(int id, String name, int age, String gender, String address) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", address='" + address + '\'' +
'}';
}
}
dao
UserDao
package com.ge.dao;
import java.sql.SQLException;
public interface UserDao {
//登录
public Boolean login(String userName, String passWord) throws SQLException, ClassNotFoundException;
}
UserDaoImpl
package com.ge.dao;
import com.ge.utils.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDaoImpl implements UserDao{
@Override
public Boolean login(String userName ,String passWord) {
Connection conn = null;
PreparedStatement pre = null;
ResultSet rs =null;
try {
conn = JdbcUtil.getConnection();
String sql = "select * from t_user where username = ? and password = ?";
pre = conn.prepareStatement(sql);
pre.setObject(1, userName);
pre.setObject(2, passWord);
rs = pre.executeQuery();
return rs.next();
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtil.release(pre, conn, rs);
}
return false;
}
}
StuDao
package com.ge.dao;
import com.ge.entity.Student;
import java.sql.SQLException;
import java.util.List;
public interface StuDao {
int PAGE_SIZE = 5;
List findStudentByPage(int currentPage) throws SQLException, ClassNotFoundException;
int findStuCount() throws SQLException, ClassNotFoundException;
void deleteByID(int id) throws SQLException, ClassNotFoundException;
void insert(String name,int age,String gender,String address) throws SQLException, ClassNotFoundException;
Student selectByID(int id) throws SQLException, ClassNotFoundException;
void update(Student student) throws SQLException, ClassNotFoundException;
List searchCount(String name,String gender) throws SQLException, ClassNotFoundException;
List search(String name,String gender,int currentPage) throws SQLException, ClassNotFoundException;
}
StuDaoImpl
package com.ge.dao;
import com.ge.entity.Student;
import com.ge.utils.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StuDaoImpl implements StuDao{
@Override
public List findStudentByPage(int currentPage) throws SQLException, ClassNotFoundException {
Connection conn = JdbcUtil.getConnection();
String sql = "select * from t_stu limit ? offset ?";
PreparedStatement pre = conn.prepareStatement(sql);
pre.setObject(1,PAGE_SIZE);
pre.setObject(2, (currentPage-1)*PAGE_SIZE);
ResultSet rs = pre.executeQuery();
List list = new ArrayList();
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
String address = rs.getString("address");
Student student = new Student(id,name,age,gender,address);
list.add(student);
}
JdbcUtil.release(pre,conn,rs);
return list;
}
@Override
public int findStuCount() throws SQLException, ClassNotFoundException {
Connection conn = JdbcUtil.getConnection();
String sql = "select count(*) from t_stu";
PreparedStatement pre = conn.prepareStatement(sql);
ResultSet rs = pre.executeQuery();
int count = 0 ;
while(rs.next()){
count = rs.getInt(1);
}
JdbcUtil.release(pre, conn,rs);
return count;
}
@Override
public void deleteByID(int id) throws SQLException, ClassNotFoundException {
Connection conn = JdbcUtil.getConnection();
String sql = "delete from t_stu where id = ?";
PreparedStatement pre = conn.prepareStatement(sql);
pre.setInt(1,id);
pre.executeUpdate();
JdbcUtil.release(conn,pre);
}
@Override
public void insert(String name, int age, String gender, String address) throws SQLException, ClassNotFoundException {
Connection conn = JdbcUtil.getConnection();
String sql = "insert into t_stu(name,age,gender,address) values(?,?,?,?) ";
PreparedStatement pre = conn.prepareStatement(sql);
pre.setString(1,name);
pre.setInt(2,age);
pre.setString(3,gender);
pre.setString(4,address);
pre.executeUpdate();
JdbcUtil.release(conn,pre);
}
@Override
public Student selectByID(int id) throws SQLException, ClassNotFoundException {
Connection conn = JdbcUtil.getConnection();
String sql = "select * from t_stu where id = ?";
PreparedStatement pre = conn.prepareStatement(sql);
pre.setInt(1,id);
ResultSet rs = pre.executeQuery();
rs.next();
Student student = new Student(rs.getInt(1), rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5));
JdbcUtil.release(pre, conn, rs);
return student;
}
@Override
public void update(Student student) throws SQLException, ClassNotFoundException {
Connection conn = JdbcUtil.getConnection();
String sql = "update t_stu set name = ?, age = ?, gender = ?, address = ? where id = ?";
PreparedStatement pre = conn.prepareStatement(sql);
pre.setString(1,student.getName());
pre.setInt(2,student.getAge());
pre.setString(3,student.getGender());
pre.setString(4,student.getAddress());
pre.setInt(5,student.getId());
pre.executeUpdate();
JdbcUtil.release(conn,pre);
}
@Override
public List searchCount(String name, String gender) throws SQLException, ClassNotFoundException {
List list = new ArrayList();
Connection conn = JdbcUtil.getConnection();
String sql = "select * from t_stu where 1=1 ";
if(!("".equals(name))){
sql = sql + "and name = '"+name+"'";
}
if(!("".equals(gender))){
sql = sql + "and gender = '"+gender+"'";
}
PreparedStatement pre = conn.prepareStatement(sql);
ResultSet rs = pre.executeQuery();
while (rs.next()){
Student student = new Student(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5));
list.add(student);
}
return list;
}
@Override
public List search(String name, String gender,int currentPage) throws SQLException, ClassNotFoundException {
List list = new ArrayList();
Connection conn = JdbcUtil.getConnection();
String sql = "select * from t_stu where 1=1 ";
if(!("".equals(name))){
sql=sql+" and name = '"+name+"'";
}
if(!("".equals(gender))){
sql = sql+" and gender ='"+gender+"' ";
}
sql = sql+"limit "+PAGE_SIZE+" offset "+(currentPage-1)*PAGE_SIZE+" ";
System.out.println(sql);
PreparedStatement pre = conn.prepareStatement(sql);
ResultSet rs = pre.executeQuery();
while (rs.next()){
Student student = new Student(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5));
list.add(student);
}
JdbcUtil.release(pre, conn, rs);
return list;
}
}
Service
StuService
package com.ge.service;
import com.ge.entity.Student;
import java.sql.SQLException;
import java.util.List;
public interface StuService {
List findStudentByPage(int currentPage) throws SQLException, ClassNotFoundException;
int findStuCount() throws SQLException, ClassNotFoundException;
void deleteByID(int id) throws SQLException, ClassNotFoundException;
void insert(String name,int age,String gender,String address) throws SQLException, ClassNotFoundException;
Student selectByID(int id) throws SQLException, ClassNotFoundException;
void update(Student student) throws SQLException, ClassNotFoundException;
List searchCount(String name, String gender) throws SQLException, ClassNotFoundException;
List search(String name,String gender,int currentPage) throws SQLException, ClassNotFoundException;
}
StuServiceImpl
package com.ge.service;
import com.ge.dao.StuDao;
import com.ge.dao.StuDaoImpl;
import com.ge.entity.Student;
import java.sql.SQLException;
import java.util.List;
public class StuServiceImpl implements StuService{
@Override
public List findStudentByPage(int currentPage) throws SQLException, ClassNotFoundException {
StuDao stuDao = new StuDaoImpl();
return stuDao.findStudentByPage(currentPage);
}
@Override
public int findStuCount() throws SQLException, ClassNotFoundException {
StuDao stuDao = new StuDaoImpl();
return stuDao.findStuCount();
}
@Override
public void deleteByID(int id) throws SQLException, ClassNotFoundException {
StuDao stuDao = new StuDaoImpl();
stuDao.deleteByID(id);
}
@Override
public void insert(String name, int age, String gender, String address) throws SQLException, ClassNotFoundException {
StuDao stuDao = new StuDaoImpl();
stuDao.insert(name, age, gender, address);
}
@Override
public Student selectByID(int id) throws SQLException, ClassNotFoundException {
StuDao stuDao = new StuDaoImpl();
return stuDao.selectByID(id);
}
@Override
public void update(Student student) throws SQLException, ClassNotFoundException {
StuDao stuDao = new StuDaoImpl();
stuDao.update(student);
}
@Override
public List searchCount(String name, String gender) throws SQLException, ClassNotFoundException {
return new StuDaoImpl().searchCount(name, gender);
}
@Override
public List search(String name, String gender,int currentPage) throws SQLException, ClassNotFoundException {
StuDao stuDao = new StuDaoImpl();
return stuDao.search(name, gender, currentPage);
}
}
servlet
LoginServlet
package com.ge.servlet;
import com.ge.dao.UserDao;
import com.ge.dao.UserDaoImpl;
import com.ge.entity.Page;
import com.ge.service.StuService;
import com.ge.service.StuServiceImpl;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import static com.ge.dao.StuDao.PAGE_SIZE;
@WebServlet("/login")
public class loginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=UTF-8");
String userName = req.getParameter("userName");
String passWord = req.getParameter("passWord");
UserDao userDao = new UserDaoImpl();
try {
Boolean isSucess = userDao.login(userName, passWord);
if(isSucess){
//统计访问量
ServletContext servletContext = this.getServletContext();
Integer count = (Integer) servletContext.getAttribute("count");
if(count==null){
count=1;
servletContext.setAttribute("count",count);
}else{
servletContext.setAttribute("count",++count);
}
Page page = new Page();
StuService stuService = new StuServiceImpl();
page.setList(stuService.findStudentByPage(1));
req.getSession().setAttribute("userName", req.getParameter("userName"));
page.setCurrentPage(1);
page.setPageSize(PAGE_SIZE );
page.setTotalSize(stuService.findStuCount());
page.setTotalPage(stuService.findStuCount() % PAGE_SIZE == 0 ? stuService.findStuCount()/PAGE_SIZE : (stuService.findStuCount()/PAGE_SIZE+1));
req.setAttribute("page",page);
req.setAttribute("list",page.getList());
req.getRequestDispatcher("stu.jsp").forward(req,resp);
}else{
req.setAttribute("message","密码错误,请重新输入");
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
PageServlet
package com.ge.servlet;
import com.ge.entity.Page;
import com.ge.service.StuService;
import com.ge.service.StuServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import static com.ge.dao.StuDao.PAGE_SIZE;
@WebServlet("/pageServlet")
public class PageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp){
try {
int currentPage = Integer.parseInt(req.getParameter("currentPage"));
Page page = new Page();
StuService stuService = new StuServiceImpl();
page.setList(stuService.findStudentByPage(currentPage));
page.setCurrentPage(currentPage);
page.setTotalPage(stuService.findStuCount() % PAGE_SIZE ==0 ? stuService.findStuCount() / PAGE_SIZE : (stuService.findStuCount()/PAGE_SIZE+1));
page.setPageSize(PAGE_SIZE);
page.setTotalSize(stuService.findStuCount());
req.setAttribute("list",page.getList());
req.setAttribute("page",page);
req.getRequestDispatcher("stu.jsp").forward(req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
DeleteServlet
package com.ge.servlet;
import com.ge.service.StuService;
import com.ge.service.StuServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int id = Integer.parseInt(req.getParameter("id"));
StuService stuService = new StuServiceImpl();
try {
stuService.deleteByID(id);
} catch (Exception e) {
e.printStackTrace();
}
req.getRequestDispatcher("/pageServlet?currentPage=1").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doDelete(req, resp);
}
}
InsertServlet
package com.ge.servlet;
import com.ge.service.StuService;
import com.ge.service.StuServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/insert")
public class InsetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String name = req.getParameter("name");
int age = Integer.parseInt(req.getParameter("age"));
String gender = req.getParameter("gender");
String address = req.getParameter("address");
StuService stuService = new StuServiceImpl();
stuService.insert(name,age,gender,address);
req.getRequestDispatcher("/pageServlet?currentPage=1").forward(req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
SelectByIDServlet
package com.ge.servlet;
import com.ge.service.StuService;
import com.ge.service.StuServiceImpl;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/selectByID")
public class SelectByIDServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp){
try {
int id = Integer.parseInt(req.getParameter("id"));
StuService stuService = new StuServiceImpl();
req.setAttribute("student",stuService.selectByID(id));
req.getRequestDispatcher("/update.jsp").forward(req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
doGet(req,resp);
}
}
UpdateServlet
package com.ge.servlet;
import com.ge.entity.Student;
import com.ge.service.StuService;
import com.ge.service.StuServiceImpl;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/updateServlet")
public class UpdateServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp){
try {
Student student = new Student(Integer.parseInt(req.getParameter("id")),req.getParameter("name"),Integer.parseInt(req.getParameter("age")),req.getParameter("gender"),req.getParameter("address"));
StuService stuService = new StuServiceImpl();
stuService.update(student);
req.getRequestDispatcher("/pageServlet?currentPage=1").forward(req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp){
doGet(req,resp);
}
}
SearchServlet
package com.ge.servlet;
import com.ge.entity.Page;
import com.ge.entity.Student;
import com.ge.service.StuService;
import com.ge.service.StuServiceImpl;
import static com.ge.dao.StuDao.PAGE_SIZE;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@WebServlet("/search")
public class searchServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
try {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=UTF-8");
String name = req.getParameter("name");
String gender = req.getParameter("gender");
req.setAttribute("name",name);
req.setAttribute("gender",gender);
System.out.println(name+gender);
StuService stuService = new StuServiceImpl();
int currentPage = Integer.parseInt(req.getParameter("currentPage"));
List list = stuService.search(name, gender,currentPage);
Page page = new Page();
page.setList(list);
req.setAttribute("list",list);
page.setPageSize(PAGE_SIZE);
page.setCurrentPage(currentPage);
List lists = stuService.searchCount(name, gender);
page.setTotalSize(lists.size());
page.setTotalPage(lists.size()%PAGE_SIZE == 0 ? lists.size()/PAGE_SIZE : (lists.size()/PAGE_SIZE+1));
req.setAttribute("page",page);
req.getRequestDispatcher("/search.jsp").forward(req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
doGet(req,resp);
}
}
配置文件:datebase.properties
driverClass:com.mysql.jdbc.Driver
url:jdbc:mysql://localhost:3306/mybase
userName:root
passWord:123456
pom.xml
4.0.0
com.ge.stuMangers
stuMangers
1.0-SNAPSHOT
war
stuMangers Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
mysql
mysql-connector-java
5.1.6
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet
jstl
1.2
taglibs
standard
1.1.2
stuMangers
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
jsp
index.jsp
<%@page language="java" pageEncoding="UTF-8" %>
<%@ page isELIgnored="false" %>
stu.jsp
<%@ page isELIgnored="false"%> <%--关于el标签不起作用--%>
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
>学生管理系统
学生管理系统
insert.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加学生
添加学生信息
update.jsp
<%@ page isELIgnored="false"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加学生
修改学生信息
search.jsp
<%@ page isELIgnored="false"%> <%--关于el标签不起作用--%>
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
>学生管理系统
${gender}学生信息