JavaWeb 学生信息管理系统

本文详细介绍了使用MVC架构在Eclipse、Tomcat和MySQL环境下完成的学生管理系统,涵盖了增删改查功能的实现,包括前端界面设计与后端Servlet、实体类及DAO/Service的代码展示。
摘要由CSDN通过智能技术生成

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

包含了前端,Servlet,数据库等
使用Eclipse+Tomcat+MySQL完成
效果图:
首页:
在这里插入图片描述
增加,删除,更新,查看功能可以正常使用

增加功能:
在这里插入图片描述
更新功能:
在这里插入图片描述
详情页:
在这里插入图片描述
并未优化前端页面。

代码:

代码结构:
在这里插入图片描述

前端代码:

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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index</title>
<style type="text/css">		
			table {
				width: 90%;
				background: #ccc;
				margin: 10px auto;
				border-collapse: collapse;/*border-collapse:collapse合并内外边距(去除表格单元格默认的2个像素内外边距*/	
			}				
			th{
				height: 25px;
				line-height: 25px;
				text-align: center;
				border: 1px solid #ccc;
			}		
			th {
				background: #eee;
				font-weight: normal;
			}		
			tr {
				background: #fff;
			}		
			tr:hover {
				background: #cc0;
			}		
		</style>
</head>
<body>
	<%

		request.setCharacterEncoding("utf-8");
		List<Student> students = (List<Student>)request.getAttribute("students");
		
	%>
	<table>
		<tr>
			<th>Student No</th>
			<th>Student Name</th>
			<th>Student Age</th>
			<th>Student Address</th>
			<th>Delete student</th>
			<th>Update student</th>
			<th>Details page</th>
		<tr>
		<%
			for(Student student:students){
		%>
		<tr>
			<th><%=student.getSno() %></th>
			<th><%=student.getSname() %></th>
			<th><%=student.getSage() %></th>
			<th><%=student.getSaddress() %></th>
			<th><a href="DeleteStudentServlet?sno=<%=student.getSno()%>">Delete</a></th>
			<th><a href="update.jsp?sno=<%=student.getSno()%>">Update</a></th>
			<th><a href="QueryStudentBySnoServlet?sno=<%=student.getSno()%>">Details</a></th>
		</tr>
		<%
			}
		%>
	</table>
	<a href="add.jsp" style="display: block; text-align: center;">Add Student</a>
</body>
</html>

add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Student</title>
</head>
<body>
	Please enter the student Information of the student you want to add:
	<form action="AddStudentServlet" method="post">
		Number:<input type="text" name="sno"><br/>
		Name:<input type="text" name="sname"><br/>
		Age:<input type="text" name="sage"><br/>
		Address:<input type="text" name="saddress"><br/>
		<input type="submit" value="add"><br/>
	</form>
</body>
</html>

update.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Update Student Information</title>
</head>
<body>
	<%
		int sno = Integer.parseInt(request.getParameter("sno"));
	
	%>
	<form action="UpdateStudentServlet" method="post">
		Please enter the student number of the student to be modified:<input type="text" name="sno" value=<%=sno %> readonly="readonly"><br/>
		Please enter the modified Name:<input type="text" name="sname"><br/>
		Please enter the modified age:<input type="text" name="sage"><br/>
		Please enter the modified address:<input type="text" name="saddress"><br/>
		<input type="submit" value="修改"><br/>
	</form>
</body>
</html>

detailsPage.jsp

<%@page import="org.student.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Student Details Page</title>
</head>
<body>
	<%
		Student student = (Student)request.getAttribute("student");
	%>
	Student No Is : <%=student.getSno() %><br/>
	Student Name Is : <%=student.getSname() %><br/>
	Student Age Is : <%=student.getSage() %><br/>
	Student Address Is : <%=student.getSaddress() %><br/>
</body>
</html>

后端代码:

实体类:

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并未完善,数据分页并未实现

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值