JavaWeb 学生信息管理系统

三层架构(MVC)完成学生管理系统

包含了前端,Servlet,数据库等
使用Eclipse+Tomcat+MySQL完成
效果图:
首页:
JavaWeb 学生信息管理系统_第1张图片
增加,删除,更新,查看功能可以正常使用

增加功能:
JavaWeb 学生信息管理系统_第2张图片
更新功能:
JavaWeb 学生信息管理系统_第3张图片
详情页:
JavaWeb 学生信息管理系统_第4张图片
并未优化前端页面。

代码:

代码结构:
JavaWeb 学生信息管理系统_第5张图片

前端代码:

index.jsp

<%@page import="org.student.entity.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Index



	<%

		request.setCharacterEncoding("utf-8");
		List students = (List)request.getAttribute("students");
		
	%>
	
		<%
			for(Student student:students){
		%>
		
		<%
			}
		%>
	
Student No Student Name Student Age Student Address Delete student Update student Details page
<%=student.getSno() %> <%=student.getSname() %> <%=student.getSage() %> <%=student.getSaddress() %> Delete Update Details
Add Student

add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Add Student


	Please enter the student Information of the student you want to add:
	
Number:
Name:
Age:
Address:

update.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Update Student Information


	<%
		int sno = Integer.parseInt(request.getParameter("sno"));
	
	%>
	
Please enter the student number of the student to be modified: readonly="readonly">
Please enter the modified Name:
Please enter the modified age:
Please enter the modified address:

detailsPage.jsp

<%@page import="org.student.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Student Details Page


	<%
		Student student = (Student)request.getAttribute("student");
	%>
	Student No Is : <%=student.getSno() %>
Student Name Is : <%=student.getSname() %>
Student Age Is : <%=student.getSage() %>
Student Address Is : <%=student.getSaddress() %>

后端代码:

实体类:

package org.student.entity;

//实体类
public class Student {
	private int sno;
	private String sname;
	private int sage;
	private String saddress;
	public Student() {}
	public Student(String sname, int sage, String saddress) {
		this.sname = sname;
		this.sage = sage;
		this.saddress = saddress;
	}
	public Student(int sno, String sname, int sage, String saddress) {
		this.sno = sno;
		this.sname = sname;
		this.sage = sage;
		this.saddress = saddress;
	}
	public int getSno() {
		return sno;
	}
	public void setSno(int sno) {
		this.sno = sno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public int getSage() {
		return sage;
	}
	public void setSage(int sage) {
		this.sage = sage;
	}
	public String getSaddress() {
		return saddress;
	}
	public void setSaddress(String saddress) {
		this.saddress = saddress;
	}
	public String toString() {
		return this.getSno()+"--"+this.getSname()+"--"+this.getSage()+"--"+this.getSaddress();
	}
}

AddStudentServlet

package org.sudent.servlet;

//表示层:Servlet
import java.io.IOException;
import java.io.PrintWriter;

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 org.student.entity.Student;
import org.student.service.StudentService;

@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public AddStudentServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			response.setCharacterEncoding("utf-8");
			int no = Integer.parseInt(request.getParameter("sno")) ;
			String name = request.getParameter("sname");
			int age = Integer.parseInt(request.getParameter("sage"));
			String address = request.getParameter("saddress");
			Student student = new Student(no,name,age,address);
			
			StudentService studentService = new StudentService();
			boolean rs = studentService.addStudent(student);
			if(rs) {
				response.sendRedirect("QueryAllStudentServlet");
			}else {
				out.println("增加失败!");
			}
//
//			StudentDao studentDao = new StudentDao();
//			Student student1 = studentDao.queryStudentBySno(no);
			
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

DeleteStudentServlet:

package org.sudent.servlet;

import java.io.IOException;
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 org.student.service.StudentService;

@WebServlet("/DeleteStudentServlet")
public class DeleteStudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public DeleteStudentServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		int no = Integer.parseInt(request.getParameter("sno")) ;
		StudentService studentService = new StudentService();
		studentService.deleteStudentBySno(no);
		response.sendRedirect("QueryAllStudentServlet");
		
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

UpdateStudentServlet:

package org.sudent.servlet;

import java.io.IOException;
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 org.student.entity.Student;
import org.student.service.StudentService;
@WebServlet("/UpdateStudentServlet")
public class UpdateStudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public UpdateStudentServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		int no = Integer.parseInt(request.getParameter("sno")) ;
		String name = request.getParameter("sname");
		int age = Integer.parseInt(request.getParameter("sage"));
		String address = request.getParameter("saddress");
		Student student = new Student(name,age,address);
		StudentService studentService = new StudentService();
		studentService.updateStudentBySno(no, student);
		response.sendRedirect("QueryAllStudentServlet");
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

QueryStudentBySnoServlet

package org.sudent.servlet;

import java.io.IOException;
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 org.student.entity.Student;
import org.student.service.StudentService;
@WebServlet("/QueryStudentBySnoServlet")
public class QueryStudentBySnoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public QueryStudentBySnoServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		int no = Integer.parseInt(request.getParameter("sno")) ;
		StudentService studentService = new StudentService();
		Student student = studentService.queryStudentBySno(no);
		System.out.println(student);
		request.setAttribute("student", student);
		request.getRequestDispatcher("detailsPage.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

QueryAllStudentServlet

package org.sudent.servlet;

import java.io.IOException;
import java.util.List;

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 org.student.entity.Student;
import org.student.service.StudentService;
@WebServlet("/QueryAllStudentServlet")
public class QueryAllStudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public QueryAllStudentServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		StudentService studentService = new StudentService();
		 List<Student> students = studentService.queryAllStudent();
		System.out.println(students);
		request.setAttribute("students", students);
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

Dao层
StudentDao.java:

package org.student.dao;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.student.entity.Student;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;


//数据访问层——》数据库
public class StudentDao {//原子性的:增删改查 无法拆分
	private final String URL = "jdbc:mysql://localhost:3306/student";
	private final String USERNAME = "root";
	private final String PASSWORD = "123456";
	//根据学号判断是否存在该学生 
	public boolean isExist(int sno) {
		return queryStudentBySno(sno)==null?false:true;
	}
	
	//删除学生
	public boolean deleteStudentBySno(int sno) {
		Connection connection = null;
		PreparedStatement pstmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
			String sql = "delete from student where sno=?";
			pstmt = (PreparedStatement) connection.prepareStatement(sql);
			pstmt.setInt(1, sno);
			int count = pstmt.executeUpdate();
	           if(count > 0)
	        	   return true;
	           else
	        	   return false;
		}catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		finally {
			try {
				if(pstmt!=null)pstmt.close();
				if(connection!=null)connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	//增加学生
	public boolean addStudent(Student student){
		Connection connection = null;
		PreparedStatement pstmt = null;
		try {
            //加载驱动
           Class.forName("com.mysql.jdbc.Driver" );
           connection = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
           String sql = "insert into student(sno,sname,sage,saddress) values(?,?,?,?)";
           pstmt = (PreparedStatement) connection.prepareStatement(sql);
           pstmt.setInt(1, student.getSno());
           pstmt.setString(2, student.getSname());
           pstmt.setInt(3, student.getSage());
           pstmt.setString(4, student.getSaddress());

           int count = pstmt.executeUpdate();
           if(count > 0)
        	   return true;
           else
        	   return false;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		finally {
			try {
				if(pstmt!=null)pstmt.close();
				if(connection!=null)connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	//根据学号查找学生
	public Student queryStudentBySno(int sno) {
		Student student = null;
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
            //加载驱动
		   Class.forName("com.mysql.jdbc.Driver" );
	       connection = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
           String sql = "select * from student where sno = ?";
           pstmt = (PreparedStatement) connection.prepareStatement(sql);
           pstmt.setInt(1, sno);
           rs = pstmt.executeQuery();
           if(rs.next()) {
        	   int no = rs.getInt("sno");
        	   String name = rs.getString("sname");
        	   int age = rs.getInt("sage");
        	   String address = rs.getString("saddress");
//        	   System.out.println(no+name+age+address);
        	   student = new Student(no,name,age,address);
           }
           return student;
           
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (SQLException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally {
			try {
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
				if(connection!=null)connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	//查询全部学生
	public List<Student> queryAllStudent() {
		List<Student> students = new ArrayList<>();
		Student student = null;
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
            //加载驱动
		   Class.forName("com.mysql.jdbc.Driver" );
	       connection = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
           String sql = "select * from student";
           pstmt = (PreparedStatement) connection.prepareStatement(sql);
           rs = pstmt.executeQuery();
           while(rs.next()) {
        	   int no = rs.getInt("sno");
        	   String name = rs.getString("sname");
        	   int age = rs.getInt("sage");
        	   String address = rs.getString("saddress");
        	   student = new Student(no,name,age,address);
        	   students.add(student);
           }
           return students;
           
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (SQLException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally {
			try {
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
				if(connection!=null)connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	//根据学号修改学生信息
	public boolean updateStudentBySno(int sno,Student student) {
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
            //加载驱动
		   Class.forName("com.mysql.jdbc.Driver" );
	       connection = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
           String sql = "update student set sname=?,sage=?,saddress=? where sno=?";
           pstmt = (PreparedStatement) connection.prepareStatement(sql);
           pstmt.setString(1, student.getSname());
           pstmt.setInt(2, student.getSage());
           pstmt.setString(3, student.getSaddress());
           pstmt.setInt(4, sno);
           int count = pstmt.executeUpdate();
           if(count > 0)
        	   return true;
           else
        	   return false;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		finally {
			try {
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
				if(connection!=null)connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

service层
StudentService.java:

package org.student.service;

import java.util.List;

import org.student.dao.StudentDao;
import org.student.entity.Student;

//业务逻辑层 可拆分
public class StudentService {
	StudentDao studentDao = new StudentDao();
	
	//增加学生,先判断是否存在学生,在增加学生
	public boolean addStudent(Student student) {
		if(!studentDao.isExist(student.getSno())) {
			studentDao.addStudent(student);
			return true;
		}else {
     	   System.out.println(student.getSno()+ student.getSname()+student.getSage() +student.getSaddress());
			System.out.println("此人已存在!增加失败!");
			return false;
		}
	}
	//删除学生,先判断是否存在学生,在删除学生
	public boolean deleteStudentBySno(int sno) {
		if(studentDao.isExist(sno)) {
			return studentDao.deleteStudentBySno(sno);
		}else {
			System.out.println("此人不存在!删除失败!");
			return false;
		}
	}
	//根据学号修改学生信息,先判断学生是否存在,在修改
	public boolean updateStudentBySno(int sno,Student student) {
		if(studentDao.isExist(sno)) {
			return studentDao.updateStudentBySno(sno, student);
		}else {
			System.out.println("此人不存在!修改失败!");
			return false;
		}
	}
	//根据学号查询学生
	public Student queryStudentBySno(int sno) {
		return studentDao.queryStudentBySno(sno);
	}
	//查询全部学生
	public List<Student> queryAllStudent(){
		return studentDao.queryAllStudent();
	}
}

结构基本完善,后期的接口和DBUtil并未完善,数据分页并未实现

你可能感兴趣的:(javaweb)