目录
模型层(Student.java):
数据访问对象层(StudentDAO.java):
视图层(add.jsp, delete.jsp, edit.jsp, list.jsp):
add.jsp:
delete.jsp
edit.jsp
list.jsp
控制器层(addStudent.jsp, deleteStudent.jsp, editStudent.jsp, StudentController.java):
addStudent.jsp
deleteStudent.jsp
editStudent.jsp
StudentController.java
public class Student {
private int id;
private String name;
private String gender;
private String birthday;
private String major;
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 String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender; }
public String getBirthday() { return birthday; }
public void setBirthday(String birthday) { this.birthday = birthday; }
public String getMajor() { return major; }
public void setMajor(String major) { this.major = major; }
}
public class StudentDAO {
private Connection conn;
public StudentDAO() {
conn = Database.getConnection();
}
public void addStudent(Student student) {
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO student(name, gender, birthday, major) VALUES (?, ?, ?, ?)");
ps.setString(1, student.getName());
ps.setString(2, student.getGender());
ps.setString(3, student.getBirthday());
ps.setString(4, student.getMajor());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteStudent(int id) {
try {
PreparedStatement ps = conn.prepareStatement("DELETE FROM student WHERE id=?");
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateStudent(Student student) {
try {
PreparedStatement ps = conn.prepareStatement("UPDATE student SET name=?, gender=?, birthday=?, major=? WHERE id=?");
ps.setString(1, student.getName());
ps.setString(2, student.getGender());
ps.setString(3, student.getBirthday());
ps.setString(4, student.getMajor());
ps.setInt(5, student.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List getAllStudents() {
List students = new ArrayList();
try {
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM student");
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setBirthday(rs.getString("birthday"));
student.setMajor(rs.getString("major"));
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
public Student getStudentById(int id) {
Student student = new Student();
try {
PreparedStatement ps = conn.prepareStatement("SELECT * FROM student WHERE id=?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setBirthday(rs.getString("birthday"));
student.setMajor(rs.getString("major"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Add Student
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Delete Student
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Edit Student
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
List Students
ID
Name
Gender
Birthday
Major
Edit
Delete
<%
StudentDAO studentDAO = new StudentDAO();
List students = studentDAO.getAllStudents();
for (Student student : students) {
out.println("");
out.println("" + student.getId() + " ");
out.println("" + student.getName() + " ");
out.println("" + student.getGender() + " ");
out.println("" + student.getBirthday() + " ");
out.println("" + student.getMajor() + " ");
out.println("Edit ");
out.println("Delete ");
out.println(" ");
}
%>
Add Student
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
Student student = new Student();
student.setName(request.getParameter("name"));
student.setGender(request.getParameter("gender"));
student.setBirthday(request.getParameter("birthday"));
student.setMajor(request.getParameter("major"));
StudentDAO studentDAO = new StudentDAO();
studentDAO.addStudent(student);
response.sendRedirect("list.jsp");
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
int id = Integer.parseInt(request.getParameter("id"));
StudentDAO studentDAO = new StudentDAO();
studentDAO.deleteStudent(id);
response.sendRedirect("list.jsp");
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
Student student = new Student();
student.setId(Integer.parseInt(request.getParameter("id")));
student.setName(request.getParameter("name"));
student.setGender(request.getParameter("gender"));
student.setBirthday(request.getParameter("birthday"));
student.setMajor(request.getParameter("major"));
StudentDAO studentDAO = new StudentDAO();
studentDAO.updateStudent(student);
response.sendRedirect("list.jsp");
%>
@WebServlet("/")
public class StudentController extends HttpServlet {
private static final long serialVersionUID = 1L;
private StudentDAO studentDAO;
public void init() {
studentDAO = new StudentDAO();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getServletPath();
switch (action) {
case "/new":
showAddForm(request, response);
break;
case "/insert":
addStudent(request, response);
break;
case "/delete":
deleteStudent(request, response);
break;
case "/edit":
showEditForm(request, response);
break;
case "/update":
updateStudent(request, response);
break;
default:
listStudents(request, response);
break;
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
private void listStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List students = studentDAO.getAllStudents();
request.setAttribute("students", students);
RequestDispatcher dispatcher = request.getRequestDispatcher("list.jsp");
dispatcher.forward(request, response);
}
private void showAddForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("add.jsp");
dispatcher.forward(request, response);
}
private void addStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student student = new Student();
student.setName(request.getParameter("name"));
student.setGender(request.getParameter("gender"));
student.setBirthday(request.getParameter("birthday"));
student.setMajor(request.getParameter("major"));
studentDAO.addStudent(student);
response.sendRedirect("list");
}
private void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
studentDAO.deleteStudent(id);
response.sendRedirect("list");
}
private void showEditForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Student student = studentDAO.getStudentById(id);
RequestDispatcher dispatcher = request.getRequestDispatcher("edit.jsp");
request.setAttribute("student", student);
dispatcher.forward(request, response);
}
private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student student = new Student();
student.setId(Integer.parseInt(request.getParameter("id")));
student.setName(request.getParameter("name"));
student.setGender(request.getParameter("gender"));
student.setBirthday(request.getParameter("birthday"));
student.setMajor(request.getParameter("major"));
studentDAO.updateStudent(student);
response.sendRedirect("list");
}
}
注:如果使用Maven进行工程管理,则需要在pom.xml文件中添加相应的依赖包