dao
public class StudentDao {QueryRunner queryRunner = QueryRunnerUtils.getQueryRunner();//查询全部学生信息public List<Student> selectStudent(){String sql = "select * from tb_student";List<Student> students = null;try {students =queryRunner.query(sql, new BeanListHandler<>(Student.class));} catch (SQLException e) {e.printStackTrace();}return students;}//根据学生姓名查询信息public List<Student> selectName(String username){String sql = "select * from tb_student where sname = ? ";List<Student> list = null;try {list = queryRunner.query(sql, new BeanListHandler<>(Student.class), username);} catch (SQLException e) {e.printStackTrace();}return list;}//添加一条学生信息public int insert(String username,int age,String sex,String email){int rows = 0;String sql = "insert into tb_student(sname,sage,ssex,semail) values(?,?,?,?)";try {rows = queryRunner.update(sql, username, age, sex, email);} catch (SQLException e) {e.printStackTrace();}return rows;}//删除一条学生信息public int delete(int id){String sql = "delete from tb_student where sid = ?";int rows = 0;try {rows = queryRunner.update(sql, id);} catch (SQLException e) {e.printStackTrace();}return rows;}//修改学生信息public int update(int id,String username,int age,String sex,String email){String sql = "update tb_student set sname=?,sage=?,ssex=?,semail=? where sid = ?";int rows = 0;try {rows = queryRunner.update(sql, username, age, sex, email, id);} catch (SQLException e) {e.printStackTrace();}return rows;}
}
<%@ page import="com.etime.entity.Student" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head><title>学生信息</title>
</head>
<body>
<div align="center"><h1>学生信息</h1><%--添加学生--%><a href="insertStudent.jsp"><button>新增</button></a><%--显示全部学生信息--%><a href="StudentServlet?type=student"><button>查看全部</button></a><%--搜索学生信息--%><form action="StudentServlet" method="get"><input type="hidden" name="type" value="selectName"><input type="text" name="selectText" placeholder="请输入要查询的人的姓名"><input type="submit" value="搜索"></form><%--展示学生信息--%><table border="2" cellpadding="0" cellspacing="0" align="center"><tr></tr><tr><th>学号</th><th>姓名</th><th>年龄</th><th>性别</th><th>邮箱</th><th>操作</th></tr><%-- 优化版--%><c:forEach var="stu" items="${list}"><tr><td>${stu.sid}</td><td>${stu.sname}</td><td>${stu.sage}</td><td>${stu.ssex}</td><td>${stu.semail}</td><td><a href="updateStudent.jsp?id=${stu.sid}&name=${stu.sname}&age=${stu.sage}&sex=${stu.ssex}&email=${stu.semail}"><input type="button" value="修改"></a><a href="StudentServlet?type=delete&id=${stu.sid}"><input type="button" value="删除"></a></td></tr></c:forEach></table><a href="PaginationServlet?type=first"><button>首页</button></a><a href="">上一页</a>1/12<a href="">下一页</a><a href="PaginationServlet?type=last">尾页</a>
</div>
</body>
</html>
servlet
package com.etime.servlet;import com.etime.dao.StudentDao;
import com.etime.entity.Student;import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;@WebServlet(name = "StudentServlet", value = "/StudentServlet")
public class StudentServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//防止乱码request.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");String type = request.getParameter("type");//获取学生数据StudentDao studentDao = new StudentDao();if ("student".equals(type)) {List<Student> studentList = studentDao.selectStudent();request.setAttribute("list", studentList);request.getRequestDispatcher("showStudent.jsp").forward(request, response);//获取字符流,将数据显示出来/* PrintWriter writer = response.getWriter();writer.print("<h1>学生信息</h1>");for (Student student : studentList) {writer.println(student + "<br>");}writer.close();*/} else if ("selectName".equals(type)) {String selectText = request.getParameter("selectText");List<Student> list = studentDao.selectName(selectText);request.setAttribute("list", list);request.getRequestDispatcher("showStudent.jsp").forward(request, response);} else if ("insertStudent".equals(type)) {String name = request.getParameter("name");int age = Integer.parseInt(request.getParameter("age"));String sex = request.getParameter("sex");String email = request.getParameter("email");int i = studentDao.insert(name, age, sex, email);if (i != 0) {request.setAttribute("state", "添加成功!");request.getRequestDispatcher("index.jsp").forward(request, response);//response.sendRedirect("showStudent.jsp");} else {request.setAttribute("state", "添加失败!");request.getRequestDispatcher("index.jsp").forward(request, response);//response.sendRedirect("insertStudent.jsp");}} else if ("delete".equals(type)) {int id = Integer.parseInt(request.getParameter("id"));int i = studentDao.delete(id);if (i != 0) {request.setAttribute("state", "删除成功!");request.getRequestDispatcher("index.jsp").forward(request, response);} else {request.setAttribute("state", "删除失败");request.getRequestDispatcher("index.jsp").forward(request, response);}} else if ("newUpdate".equals(type)){String name = request.getParameter("name");//int age = Integer.parseInt(request.getParameter("age"));String age = request.getParameter("age");String sex = request.getParameter("sex");String email = request.getParameter("email");int id = Integer.parseInt(request.getParameter("id"));int i = studentDao.update(id, name, Integer.parseInt(age), sex, email);if (i != 0) {request.setAttribute("state", "修改成功!");request.getRequestDispatcher("index.jsp").forward(request, response);} else {request.setAttribute("state", "修改失败!");request.getRequestDispatcher("index.jsp").forward(request, response);}}}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
}
部分。。。