<%@page import="java.util.List"%><%@ page lan_学生信息管理系统javaweb源代码">
当前位置:   article > 正文

JavaWeb 学生信息管理系统_学生信息管理系统javaweb源代码

学生信息管理系统javaweb源代码

三层架构(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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

后端代码:

实体类:

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();
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

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);
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

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);
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

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);
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

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);
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

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);
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

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();
			}
		}
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230

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();
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/734799
推荐阅读
相关标签
  

闽ICP备14008679号