dao
public class StudentDao {
QueryRunner queryRunner = QueryRunnerUtils.getQueryRunner();
//查询全部学生信息
public List selectStudent(){
String sql = "select * from tb_student";
List students = null;
try {
students =queryRunner.query(sql, new BeanListHandler<>(Student.class));
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
//根据学生姓名查询信息
public List selectName(String username){
String sql = "select * from tb_student where sname = ? ";
List 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" %>
学生信息
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 {
@Override
protected 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 studentList = studentDao.selectStudent();
request.setAttribute("list", studentList);
request.getRequestDispatcher("showStudent.jsp").forward(request, response);
//获取字符流,将数据显示出来
/* PrintWriter writer = response.getWriter();
writer.print("学生信息
");
for (Student student : studentList) {
writer.println(student + "
");
}
writer.close();*/
} else if ("selectName".equals(type)) {
String selectText = request.getParameter("selectText");
List 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);
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
部分。。。