JavaWeb学生信息管理系统_删除V2.0 - Go语言中文社区

JavaWeb学生信息管理系统_删除V2.0


  • 项目简介

1)在上个项目查询完成的基础上,添加一个删除的功能,点击删除,可以把数据库里的学生信息删除,并刷新显示学生信息的页面

2)在Web页面添加一个删除的超链接,发送请求到delStuServlet

3)根据发送的get请求,解析出对应字段,并进行数据库中学生信息的删除

4)删除成功后,将delStuServlet重定向到studentsList.jsp

  • 在Web页面添加一个删除的超链接,发送请求到delStuServlet

	<table border="1" cellspacing="0px" cellpadding="10px">
		<!-- 表格标题 -->
		<caption>学生信息</caption>
		<!-- 表首行 -->
		<tr>
			<th>流水号</th>
			<th>成绩类型</th>
			<th>身份证号</th>
			<th>考试编号</th>
			<th>学生姓名</th>
			<th>家庭住址</th>
			<th>考试成绩</th>
			<th>删除</th>
		</tr>
		<%
			List<Student> studentsList = (List<Student>)request.getAttribute("studentsList");
			for(Student stu : studentsList){
				
		%>
				<tr>
					<td><%= stu.getFlowId() %></td>
					<td><%= stu.getType() %></td>
					<td><%= stu.getIdCard() %></td>
					<td><%= stu.getExamCard() %></td>
					<td><%= stu.getStudentName() %></td>
					<td><%= stu.getLocation() %></td>
					<td><%= stu.getGrade() %></td>
					<td><a href="delStuServlet?flowId=<%= stu.getFlowId()%>">删除</a></td>
				</tr>
		<% 
			}
		%>
	</table>

技术要点在超链接的添加,需要用get请求携带参数的特性

                               <td><a href="delStuServlet?flowId=<%= stu.getFlowId()%>">删除</a></td>

  • 在delStuServlet中解析字段并发送给StudentDAO处理数据

		int delStuFlowId = Integer.parseInt(request.getParameter("flowId"));
		int result = new StudentDAO().delStuByFlowId(delStuFlowId);
  • 在数据库查询并删除记录

	public int delStuByFlowId( int flowId ) {
		//1准备连接数据库的基本信息
		String driverClass="com.mysql.jdbc.Driver";
		String jdbcUrl="jdbc:mysql://localhost:3306/myWebProject";
		String mysqlUser="root";
		String mysqlPassword="root";
		//2加载数据库驱动
		try {
			Class.forName(driverClass);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		//3获取数据库连接以及处理数据
		Connection connection=null;
		PreparedStatement preparestatement=null;
		String sqlDelStu="delete from StudentGradeTable where FlowId=?";
		int result=0;
		try {
			connection=DriverManager.getConnection(jdbcUrl, mysqlUser, mysqlPassword);
			preparestatement=connection.prepareStatement(sqlDelStu);
			preparestatement.setInt(1, flowId);
			result=preparestatement.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			if(connection!=null) {
				try {
					connection.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			if(preparestatement!=null) {
				try {
					preparestatement.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			
		}
		return result;
	}
  • delStuServlet根据数据库的返回结果进行重定向

		if(result==0) {
			request.getRequestDispatcher("/WEB-INF/error.jsp").forward(request, response);
		}else {
			response.sendRedirect("ListAllStudent");
		}
  • 最终效果

点击删除

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_23937341/article/details/81430485
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-31 11:52:43
  • 阅读 ( 702 )
  • 分类:研发管理

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢