学生信息管理系统(分页,判断是否登录)

做任何Web项目我们都现需要理清楚我们的需求

1.我们可以先写一个jsp的登陆页面


<div align="center">
		<h2>学生信息管理系统</h2>
		<form action="/MySchool/admin" method="post">
			账号:<input type="text" name="username" required><br>
			<br> 密码:<input type="password" name="password" required><br>
			<br> <input type="submit" value="登录">
		</form>
</div>

2.移步登陆页面的Servlet

这里需要注意的是 我们不管前台还是后台最好都要进行一个非空判断,这样可以减少我们的运行出错

@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.设置编码格式
		req.setCharacterEncoding("UTF8");
		// 2.接收前台值
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		// 3.校验是否为空
		if (username != null && password != null) {
			boolean result = adminService.login(username, password);
			if (result) {
				req.getSession().setAttribute("username", username);
				resp.sendRedirect("/MySchool/student");
			} else {
				resp.sendRedirect("login.jsp");
			}
		}
	}

3.service服务层 我这里都进行了接口的一个处理 方便我们用不同的数据库来处理 不清楚的可以私信我

private AdminDao adminDao = new AdminDaoImpl();
	@Override
	public boolean login(String username, String password) {
		int count = adminDao.countStudent(username,password);
		if (count > 0) {
			return true;
		}
		return false;
	}

4.Dao

@Override
	public int countStudent(String username, String password) {
		Connection conn = this.getConnection();
		String sql = "select count(*) from admin where username=? and password=?";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int count = 0;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, username);
			pstmt.setString(2, password);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				count = rs.getInt(1);	
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

到现在为止 我们的登录页面就算是完全的搞定 因为这只是一个基础的项目所以我们并没有普通用户登录,仅仅只有管理员

1.接下来就是判断我们这个项目的各种功能,无非也就是最简单的增删改查

2.一样的是我们先进行前台页面的编写,我们这里直接使用EL表达式跟JSTL进行编写

<head>
<meta charset="UTF-8">
<title>学生信息</title>
<style type="text/css">
td {
	text-align: center
}

tr:first-of-type {
	font-weight: bold;
}

tr:nth-child(even) {
	background-color: pink
}
</style>
</head>
<body>
	<%
	String name = (String) session.getAttribute("username");
	if (name == null) {
		response.sendRedirect("login.jsp");
		return;
	}
	%>
	<div align="center">
		<h2>学生信息登记表</h2>
		欢迎您,${name}<br> <a
			href="/MySchool/add.jsp">新增学生信息</a>
		<table border="1">
			<tr>
				<td>学号</td>
				<td>姓名</td>
				<td>年龄</td>
				<td>性别</td>
				<td>电话</td>
				<td>住址</td>
				<td>班级</td>
				<td>院系</td>
				<td>操作</td>
			</tr>
			<!-- varStatus可以调出索引 名称可以任意起名 -->
			<c:forEach var="stu" items="${page.results}">
			<tr>
				<td>${stu.num}</td>
				<td>${stu.name}</td>
				<td>${stu.age}</td>
				<td>${stu.sex}</td>
				<td>${stu.phone}</td>
				<td>${stu.address}</td>
				<td>${stu.class1}</td>
				<td>${stu.room}</td>
				<td><a
					href="/MySchool/student?num=${stu.num}&op=select">修改</a>
					<a href="/MySchool/student?num=${stu.num}&op=delete">删除</a>
				</td>
			</tr>
			</c:forEach>
		</table>
		<br> <a href="/MySchool/student?pageIndex=1">首页</a>&nbsp;&nbsp; <a
			href="/MySchool/student?pageIndex=${page.pageIndex - 1 < 1?1:page.pageIndex-1}">上一页</a>&nbsp;&nbsp;
		<a
			href="/MySchool/student?pageIndex=${page.pageIndex + 1 > page.pagecount?page.pageCount:page.pageIndex+1}">下一页</a>&nbsp;&nbsp;
		<a href="/MySchool/student?pageIndex=${page.pageCount}">尾页</a>&nbsp;&nbsp;
		当前第${page.pageIndex}页,总共${page.pageCount}页
	</div>
</body>
</html>

3.我们这里进行了分页的处理,接下来就是新增跟修改页面,我们这里新增跟修改页面用的是同一个页面

<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
	td{
		text-align: center;
	}
	rt:first-of-type {
	font-size: 20px;
	font-weight: bold;
	}
</style>
</head>
<body>
	<div align="center">
	<c:if test="${!empty errorMsg}">
		<script type="text/javascript">
		alert("${errorMsg}");
		
		</script>
	</c:if>
		<form action="/MySchool/student" method="post">
					<input name="id" type="hidden" value="${stu.a}">
			<table border="1">
				<tr>
					<td colspan="2">学生信息管理系统</td>
				</tr>
				<tr>
					<td>学号:</td>
					<td><input name="num" type="text" value="${stu.num}" required></td>
				</tr>
				<tr>
					<td>姓名:</td>
					<td><input name="name" type="text" value="${stu.name}" required></td>
				</tr>
				<tr>
					<td>年龄:</td>
					<td><input name="age" type="text" value="${stu.age}" required></td>
				</tr>
				<tr>
					<td>性别:</td>
					<td><input name="sex" type="radio" value="${stu.sex}" required></td>
				</tr>
				<tr>
					<td>电话:</td>
					<td><input name="phone" type="text" value="${stu.phone}" required></td>
				</tr>
				<tr>
					<td>住址:</td>
					<td><input name="address" type="text" value="${stu.address}" required></td>
				</tr>
				<tr>
					<td>班级:</td>
					<td><input name="class1" type="text" value="${stu.class1}" required></td>
				</tr>
				<tr>
					<td>院系:</td>
					<td><input name="room" type="text" value="${stu.room}" required></td>
				</tr>
				<tr>
					<td colspan="2"><input type="submit" value="提交"><input type="reset" value="取消"></td>
				</tr>
				
			</table>
		</form>
	</div>
</body>
</html>

这里用同一个页面,可以让我们的代码量减少,也可以方便我们的编写,当然我们这里有一个主键来让我们判断我们进行的是哪一步操作

4.后台代码,理论上来讲我们前后台都需要进行非空判断 ,但是为了少些一点代码,我在前台设置了必填项

@WebServlet(urlPatterns = "/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService = new StudentServiceImpl();

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.设置编码格式
		req.setCharacterEncoding("UTF8");
		String op = req.getParameter("op");
		if ("delete".equals(op)) {
			String num = req.getParameter("num");
			studentService.removeStudent(num);
			resp.sendRedirect("/MySchool/student");
		} else if ("select".equals(op)) {
			String num = req.getParameter("num");
			Student stu = studentService.selectStudent(num);
			req.setAttribute("stu", stu);
			req.getRequestDispatcher("add.jsp").forward(req, resp);
		} else {
//			List<Student> stu = studentService.studentList();
//			req.setAttribute("stu", stu);
//			req.getRequestDispatcher("index.jsp").forward(req, resp);
			String pageIndex = req.getParameter("pageIndex");
			if (pageIndex == null) {
				pageIndex = "1";
			}
			int pageSize = 5;
			//将Student对象放入Page类
			Page<Student> page = studentService.getStudentByPage(Integer.parseInt(pageIndex), pageSize);
			page = new Page<Student>();
			//放到里面便于前台拿值
			req.setAttribute("page", page);
			req.getRequestDispatcher("index.jsp").forward(req, resp);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.设置编码格式
		req.setCharacterEncoding("UTF8");
		// 2.接收前台值
		String id = req.getParameter("id");
		if (id != "") {
			System.out.println(id);
			String num = req.getParameter("num");
			String phone = req.getParameter("phone");
			String address = req.getParameter("address");
			boolean reslut = studentService.updateStudent(num, phone, address);
			if (reslut) {
				resp.sendRedirect("/MySchool/student");
			}
		} else {
			String num = req.getParameter("num");
			String name = req.getParameter("name");
			String sex = req.getParameter("sex");
			int age = Integer.valueOf(req.getParameter("age"));
			String phone = req.getParameter("phone");
			String address = req.getParameter("address");
			String class1 = req.getParameter("class1");
			String room = req.getParameter("room");
			/**
			 * 省略非空判断
			 */
			// 3.添加
			Student stu = new Student(num, name, age, sex, phone, address, class1, room);

			boolean reslut = studentService.addStudent(stu);
			if (reslut) {
				resp.sendRedirect("/MySchool/student");
			} else {
				req.setAttribute("errMsg", "添加失败!");
				req.getRequestDispatcher("add.jsp").forward(req, resp);
			}
		}

	}

}

5.service接口

public interface StudentService {

	List<Student> studentList();

	boolean addStudent(Student stu);

	void removeStudent(String num);

	Student selectStudent(String num);

	boolean updateStudent(String num, String phone, String address);

	Page<Student> getStudentByPage(int pageIndex, int pageSize);

}

6.service的实现

public class StudentServiceImpl implements StudentService {
	private StudentDao studentDao = new StudentDaoImpl();
	@Override
	public List<Student> studentList() {
		
		return studentDao.studentList();
	}
	@Override
	public boolean addStudent(Student stu) {
		int count = studentDao.addStudent(stu);
		if (count > 0) {
			return true;
		}
		return false;
	}
	@Override
	public void removeStudent(String num) {
		studentDao.removeStudent(num);
		
	}
	@Override
	public Student selectStudent(String num) {
		Student stu = studentDao.selectStudent(num);
		return stu;
	}
	@Override
	public boolean updateStudent(String num, String phone, String address) {
		int count = studentDao.updateStudent(num,phone,address);
		if (count > 0) {
			return true;
		}
		return false;
	}
	@Override
	public Page<Student> getStudentByPage(int pageIndex, int pageSize) {
		
		Page<Student> page = new Page<Student>();
		//当前页
		page.setPageIndex(pageIndex);
		//每页数据量
		page.setPageSize(pageSize);
		//总数据量
		int total = studentDao.countStudents();
		page.setTotal(total);
		//查询当前页数据 
		List<Student> results = studentDao.getStudentByPage(pageIndex,pageSize);
		page.setResults(results);
		return page;
	}

}

7.Dao接口

public interface StudentDao {

	List<Student> studentList();

	int addStudent(Student stu);

	void removeStudent(String num);

	Student selectStudent(String num);

	int updateStudent(String num, String phone, String address);

	int countStudents();

	List<Student> getStudentByPage(int pageIndex, int pageSize);

}

8.Dao的实现

public class StudentDaoImpl extends BaseDao implements StudentDao {

	@Override
	public List<Student> studentList() {
		Connection conn = this.getConnection();
		String sql = "select * from student";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Student> list = new ArrayList<Student>();
		Student sch = null;
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				sch = new Student();
				sch.setNum(rs.getString("student_num"));
				sch.setName(rs.getString("student_name"));
				sch.setSex(rs.getString("student_sex"));
				sch.setAge(rs.getInt("student_age"));
				sch.setPhone(rs.getString("student_phone"));
				sch.setAddress(rs.getString("student_address"));
				sch.setClass1(rs.getString("student_class"));
				sch.setRoom(rs.getString("student_room"));
				list.add(sch);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeConnection(rs, pstmt, conn);
		}
		return list;
	}

	@Override
	public int addStudent(Student stu) {
		Connection conn = this.getConnection();
		String sql = "insert into student values(null,?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt = null;
		int result = 0;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, stu.getNum());
			pstmt.setString(2, stu.getName());
			pstmt.setString(3, stu.getSex());
			pstmt.setInt(4, stu.getAge());
			pstmt.setString(5, stu.getPhone());
			pstmt.setString(6, stu.getAddress());
			pstmt.setString(7, stu.getClass1());
			pstmt.setString(8, stu.getRoom());
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeConnection(null, pstmt, conn);
		}
		return result;
	}

	@Override
	public void removeStudent(String num) {
		Connection conn = this.getConnection();
		String sql = "delete from student where student_num=?";
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, num);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeConnection(null, pstmt, conn);
		}
	}

	@Override
	public Student selectStudent(String num) {
		Connection conn = this.getConnection();
		String sql = "select * from student where student_num=?";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Student stu = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, num);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				stu = new Student();
				stu.setA(rs.getInt("id"));
				stu.setNum(rs.getString("student_num"));
				stu.setName(rs.getString("student_name"));
				stu.setSex(rs.getString("student_sex"));
				stu.setAge(rs.getInt("student_age"));
				stu.setPhone(rs.getString("student_phone"));
				stu.setAddress(rs.getString("student_address"));
				stu.setClass1(rs.getString("student_class"));
				stu.setRoom(rs.getString("student_room"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeConnection(rs, pstmt, conn);
		}
		return stu;
	}

	@Override
	public int updateStudent(String num, String phone, String address) {
		Connection conn = this.getConnection();
		String sql = "update student set student_phone=?,student_address=? where student_num=?";
		PreparedStatement pstmt = null;
		int count = 0;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, phone);
			pstmt.setString(2, address);
			pstmt.setString(3, num);
			count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeConnection(null, pstmt, conn);
		}
		return count;
	}

	@Override
	public int countStudents() {
		Connection conn = this.getConnection();
		//查询到所有数据
		String sql = "select count(*) from student";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int count = 0;
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				count = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeConnection(rs, pstmt, conn);
		}
		return count;
	}

	@Override
	public List<Student> getStudentByPage(int pageIndex, int pageSize) {
		Connection conn = this.getConnection();
		String sql = "select * from student limit ?, ?";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Student> list = new ArrayList<Student>();
		Student sch = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, (pageIndex - 1) * pageSize);
			pstmt.setInt(2, pageSize);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				sch = new Student();
				sch.setNum(rs.getString("student_num"));
				sch.setName(rs.getString("student_name"));
				sch.setSex(rs.getString("student_sex"));
				sch.setAge(rs.getInt("student_age"));
				sch.setPhone(rs.getString("student_phone"));
				sch.setAddress(rs.getString("student_address"));
				sch.setClass1(rs.getString("student_class"));
				sch.setRoom(rs.getString("student_room"));
				list.add(sch);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeConnection(rs, pstmt, conn);
		}
		return list;
	}

}

我们的工具类

1.学生信息类

这个很简单我就不进行编写了

2.分页类

public class Page<T> {
	// 当前页码
	private int pageIndex;
	
	// 每页数据量
	private int pageSize = 5;

	// 总页数
	private int pageCount;
	
	// 总数据量
	private int total;
	
	// 当前页数据
	private List<T> results;

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		if (total % pageSize == 0) {
			pageCount = total / pageSize;
		}else {
			pageCount = total / pageSize +1;
		}
		this.total = total;
	}

	public List<T> getResults() {
		return results;
	}

	public void setResults(List<T> results) {
		this.results = results;
	}
	
	
	
}

3.BescDao

public class BaseDao {

	private String url = "jdbc:mysql://127.0.0.1:3306/student_table?useUnicode=true&characterEncoding=utf-8";
	private String name = "root";
	private String password = "xuyifan991225";
	
	/**
	 * 获取数据库连接
	 * @return
	 */
	public Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = (Connection) DriverManager.getConnection(url, name, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	/**
	 * 关闭数据库连接
	 * @param rs
	 * @param pstmt
	 * @param conn
	 */
	public void closeConnection(ResultSet rs,PreparedStatement pstmt,Connection conn) {
		if(rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

我们一定要用好面向对象的三大特征,这里进行接口的方法是为了我们更换数据库时的方便

毕业设计等计算机类毕业设计可私信

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值