Servlet实现增删改查(基础)

项目目录

项目目录

首页index

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="<%=request.getContextPath()%>/student.do?method=query">查看所有学生信息</a>
</body>
</html>

接口

/**
* @desc	      接口    
* @auther Wrry
* @time   2020年12月14日下午5:01:46
**/
public interface IStudentService {

	void UpdateStudent(String sno, String sname, String sage, String ssex) throws ClassNotFoundException, SQLException;

	Map<String, Object> queryForList(String sno) throws ClassNotFoundException, SQLException;

	void deleteStudentOne(String sno) throws ClassNotFoundException, SQLException;

	List<Map<String, Object>> queryStudentAll() throws ClassNotFoundException, SQLException;

	void add(String sname, String sage, String ssex) throws ClassNotFoundException, SQLException;

}

方法类继承了HttpServlet

/**
 * @desc   方法类
 * @auther Wrry
 * @time 2020年11月30日下午8:09:02
 **/
@SuppressWarnings("serial")
public class StudentServlet extends HttpServlet {

	IStudentService StudentService = new StudentServiceImpl();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		System.out.println("StudentServlet-->service()");
		String method = request.getParameter("method");

		try {
			if ("query".equals(method)) {
				this.query(request, response);
			} else if ("delete".equals(method)) {
				this.Delete(request, response);
			} else if ("addPage".equals(method)) {
				this.addPage(request, response);
			} else if ("add".equals(method)) {
				this.add(request, response);
			} else if ("editPage".equals(method)) {
				this.editPage(request, response);
			} else if ("edit".equals(method)) {
				this.edit(request, response);
			}

		} catch (ClassNotFoundException e) {
			System.out.println("类没有找到异常");
		} catch (SQLException e) {
			System.out.println("SQL语句异常");
		}

	}

	// 修改方法
	private void edit(HttpServletRequest request, HttpServletResponse response)
			throws ClassNotFoundException, SQLException, IOException {

		String sno = request.getParameter("sno");
		String sname = request.getParameter("sname");
		String sage = request.getParameter("sage");
		String ssex = request.getParameter("ssex");

		StudentService.UpdateStudent(sno, sname, sage, ssex);

		response.sendRedirect(request.getContextPath() + "/student.do?method=query");

	}

	// 跳转修改页面
	private void editPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, ClassNotFoundException, SQLException {

		// 1.获取修改id
		String sno = request.getParameter("sno");

		// 2.查询一条数据
		Map<String, Object> map = StudentService.queryForList(sno);

		// 3.传值到后台
		request.setAttribute("map", map);

		request.getRequestDispatcher("/jsps/students/edit_student.jsp").forward(request, response);

	}

	// 添加方法
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ClassNotFoundException, SQLException, ServletException, IOException {
		String sname = request.getParameter("sname");
		String sage = request.getParameter("sage");
		String ssex = request.getParameter("ssex");

		StudentService.add(sname, sage, ssex);

		response.sendRedirect(request.getContextPath() + "/student.do?method=query");

	}

	// 添加页面转向
	private void addPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.getRequestDispatcher("/jsps/students/add_student.jsp").forward(request, response);

	}

	// 删除数据方法
	private void Delete(HttpServletRequest request, HttpServletResponse response)
			throws ClassNotFoundException, SQLException, IOException {

		// 1.获取删除数据的ID
		String sno = request.getParameter("sno");
		System.out.println(sno);
		// 2.调用删除方法(传入删除的ID)
		StudentService.deleteStudentOne(sno);

		// 3.重定向
		response.sendRedirect(request.getContextPath() + "/student.do?method=query");

	}

	// 查询全部数据方法
	private void query(HttpServletRequest request, HttpServletResponse response)
			throws ClassNotFoundException, SQLException, ServletException, IOException {

		// 1.获取数值
		List<Map<String, Object>> list = StudentService.queryStudentAll();

		System.out.println(list);

		// 2.传值到后台
		request.setAttribute("list", list);

		request.getRequestDispatcher("/jsps/students/List_Student.jsp").forward(request, response);

	}

}

接口实现类

/**
* @desc	      实现类    
* @auther Wrry
* @time   2020年11月30日下午8:08:42
**/
public class StudentServiceImpl implements IStudentService {
	Dao dao = new DaoImpl();

	public List<Map<String, Object>> queryStudentAll() throws ClassNotFoundException, SQLException {
		return dao.executeQueryForList("select * from student");
	}

	public void deleteStudentOne(String sno) throws ClassNotFoundException, SQLException {
		dao.executeUpdate("delete from student where sno='"+sno+"' ");
		
	}

	public void add(String sname, String sage, String ssex) throws ClassNotFoundException, SQLException {
		dao.executeUpdate(" insert into student values ('"+UUID.randomUUID().toString().substring(0, 8)+"','"+sname+"','"+sage+"','"+ssex+"') ");
		
	}

	public Map<String, Object> queryForList(String sno) throws ClassNotFoundException, SQLException {
		return dao.executeQueryForMap("select * from student where sno='"+sno+"'");
		
		
	}

	public void UpdateStudent(String sno, String sname, String sage, String ssex) throws ClassNotFoundException, SQLException {
		
		dao.executeUpdate(" update student set sname='"+sname+"',sage='"+sage+"',ssex='"+ssex+"' where sno='"+sno+"' ");
		
	}

jsp Student 首页

<%@page import="java.util.Map"%>
<%@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>Insert title here</title>
</head>
<body>
	<table border="2px" style="text-align: center; margin: auto">
		<tr>
			<td>学号</td>
			<td>姓名</td>
			<td>年龄</td>
			<td>性别</td>
			<td>操作</td>			
		</tr>
			<%
		 List list = (List)request.getAttribute("list");
		for(int i = 0; i<list.size(); i++){
			Map map = (Map)list.get(i);		
	%>
		<tr>
			<td><%=(i+1)%></td>
			<td><%=map.get("sname") %></td>
			<td><%=map.get("sage") %></td>
			<td><%=map.get("ssex") %></td>
			<td>
			<a href="<%=request.getContextPath()%>/student.do?method=addPage">添加</a>
			<a href="<%=request.getContextPath()%>/student.do?method=editPage&sno=<%=map.get("sno")%>">修改</a>
			<a href="<%=request.getContextPath()%>/student.do?method=delete&sno=<%=map.get("sno")%>">删除</a>
			</td>
		</tr>				
	<%	
		}
	%>	
	</table>
</body>
</html>

jsp Student 添加页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="<%=request.getContextPath()%>/student.do?method=add"
		method="post">
		<table border="2px" style="text-align: center; margin: auto">
			<tr>
				<td>姓名</td>
				<td><input type="text" name="sname" value="" /></td>
			</tr>
			<tr>
				<td>年龄</td>
				<td><input type="text" name="sage" value="" /></td>
			</tr>
			<tr>
				<td>性别</td>
				<td><input type="text" name="ssex" value="" /></td>
			</tr>
			<tr>
				<td>操作</td>
				<td><input type="submit" value="提交" /></a></td>
			</tr>
		</table>
	</form>
</body>
</html>

jsp Student 修改页面

<%@page import="java.util.Map"%>
<%@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>Insert title here</title>
</head>
<body>
	<%
		Map map = (Map) request.getAttribute("map");
	%>
	<form action="<%=request.getContextPath()%>/student.do?method=edit"
		method="post">
		<table border="2px" style="text-align: center; margin: auto">
			<tr>
				<td>姓名</td>
				<td><input type="hidden" name="sno" value="<%=map.get("sno")%>" />
					<input type="text" name="sname" value="<%=map.get("sname")%>" /></td>
			</tr>
			<tr>
				<td>年龄</td>
				<td><input type="text" name="sage" value="<%=map.get("sage")%>" /></td>
			</tr>
			<tr>
				<td>性别</td>
				<td><input type="text" name="ssex" value="<%=map.get("ssex")%>" /></td>

			</tr>
			<tr>
				<td>操作</td>
				<td><input type="submit" value="提交" /></a></td>
			</tr>
		</table>
	</form>
</body>
</html>

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Testservlet</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>StudentServlet</servlet-name>
  <servlet-class>com.rj.bd.student.StudentServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>StudentServlet</servlet-name>
  <url-pattern>/student.do</url-pattern>
  </servlet-mapping>
</web-app>

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值