Web对数据库的增删改查(servlet+jsp+javaBean增删改查)Web+Java对数据库增删改查-javaWeb中级
1.开始之前的准备(servlet+jsp+javaBean增删改查)
jsp:页⾯的请求和展⽰
在前台主要学了jsp进⾏页⾯的请求和展⽰
java后台
mysql数据库
学习了java如何进⾏数据库增删改查
JDBC
JDBC重构Dao
Dao进⾏sql语句
将数据库的获取结果响应到页⾯上
页⾯向后台进⾏请求
请求:(url:xx.8080/student.do)
servlet进⾏响应
jsp+servlet+javaBean+mysql === M2
代码准备
StudentServlet.java
package Li;
import IOException;
import ServletException;
import HttpServlet;
import HttpServletRequest;
import HttpServletResponse;
/**
* @desc Student 模块的 Servlet
* @author Li Ya Hui
* @time 2021年6⽉2⽇上午8:34:22
*/
public class StudentServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
System.out.println("Student--->service");
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP+Servlet+JavaBean实现表单的增删改查</title>
</head>
<body>
<a href="<%=ContextPath()%>/student.do">Student模块的全部查询</a>
</body>
</html>
<servlet>
<servlet-name>ser</servlet-name>基本的增删改查语句
<servlet-class>com.Li.StudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ser</servlet-name>
<url-pattern>/studnet.do</url-pattern>
</servlet-mapping>
2.查询
⾸先创建⼀个Dao(连接数据库)
然后创建⼀个查询的类StudnetService实例化Dao(StudnetService)
在StudentServlet⾥创建⼀个StudnetService对象的query⽅法
query⽅法将数据转成list<map>然后再存储到request对象⾥去
在StudentServlet⾥service⽅法⾥获取链接⾥的method如果匹配 调⽤ query查询⽅法
在主页⾯⾥写⼀个超链接,超链接为<a href="<%=ContextPath()%>/student.do?method=query"> Student模块的全部查询 </a> method的参数为query代表我的⼀个匹配参数
当servlet匹配成功时,会将RequestDispatcher("/jsps/studnet/list_student.jsp").forward(request, response);发送请求⼦页⾯接收数据的代码(list_student.jsp)
<%@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>Student 模块的查询展⽰界⾯</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
table{
border-width:2px;
border-color: red;
text-align: center;
}
</style>
</head>
</head>
<body>
<table border="1">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>操作</td>
</tr>
<%
List list = (List) Attribute("list");
for(int i = 0 ; i < list.size() ; i++)
{
Map map =(Map) (i);
%>
<tr>
<td><%=("sno") %></td>
<td><%=("sname") %></td>
<td><%=("sage") %></td>
<td><%=("ssex") %></td>
<td>
<a href="#">添加</a>
<a href="#">修改</a>
<a href="#">删除</a>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
配置l
<?xml version="1.0" encoding="UTF-8"?>
<web-app xsi="/2001/XMLSchema-instance"xmlns="JAVA.sun/xml/ns/javaee"schemaLocation="jav
a.sun/xml/ns/javaee java.sun/xml/ns/javaee/web-app_3_0.xsd"version="3.0">
<display-name>WebM2Demo001</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.Li.student.StudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentServlet</servlet-name>
<url-pattern>/student.do</url-pattern>
</servlet-mapping>
</web-app>
3.删除
删除的第⼀步,⾸先给删除的按钮加上链接:<a href="<%=ContextPath()%>/student.do?method=delete&sno=<%=("sno") %>">删除</a>
method=delete 制造关键字
sno=<%=(“sno”) %> 制造主键
在StudentServlet的service⽅法⾥添加method=delete时的⽅法,调⽤delete⽅法
delete⽅法⾥调⽤StudentService⾥的deleteBySno⽅法
/**
* @desc 根据学号删除了⼀条学⽣信息
* @param sno
* @throws SQLException
* @throws ClassNotFoundException
*/
public void deleteBySno(String sno)throws ClassNotFoundException, SQLException {
// 删除
}
在StudentServlet的delete⽅法⾥进⾏重定向
//2.调⽤studentService中的⽅法执⾏sql语句实现删除该条记录
studentservice.deleteBySno(sno);
//3.重定向:刷新重新指向新的页⾯简介进⾏刷新页⾯
response.ContextPath()+"/student.do?method=query");
4.添加
添加的第⼀步,⾸先给添加按钮加上链接:<a href="<%=ContextPath()%>/student.do?method=addPage">添加</a>
method=addPage 代表要进⼊信息添加页⾯
新建student模块添加页⾯
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>student模块的添加页⾯</title>
<style type="text/css">
*{
margin:0;
padding:0;
}
</style>
</head>
<body>
<form action="<%=ContextPath()%>/student.do?method=add"method="post">
<table border="2"algin="center">
<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>
<tr>
<td>性别</td>
<td><input type="text"name="ssex"value=""/></td>
</tr>
<tr>
<td colspan="2"><input type="submit"value="提交"/></td>
</tr>
</table>
</form>
</body>
</html>
在添加页⾯的表单⾥提交地址为:action="<%=ContextPath()%>/student.do?method=add"
因为表单存在英⽂,当前前台向后台⽤form表单传输数据时,需要设定编码request.setCharacterEncoding("utf-8");
在StudentService⾥service判断⾥添加el se if(“addPage”.equals(method)) {this.addPage(request,response);
}
在StudentService⾥service判断⾥添加
else if("add".equals(method)){
this.add(request,response);
}
在StudentService⾥添加add数据⽅法
/**
* @desc 保存添加页⾯的数据
* @param request
* @param response
* @throws SQLException
* @throws ClassNotFoundException
* @throws IOException
*/
private void add(HttpServletRequest request, HttpServletResponse response)throws ClassNotFoundException, SQLException, IOException { System.out.println("StudentServlet--->add()");
//1.接值
String sname = Parameter("sname");
String sage = Parameter("sage");
String ssex = Parameter("ssex");
System.out.println(sname+sage+ssex);
//2.调⽤StudentService中的⽅法执⾏sql语句实现将数据保存到mysql数据库中
studentservice.save(sname,sage,ssex);
//3.进⾏重定向(刷新)
response.ContextPath()+"/student.do?method=query");
}
还需要在StudentService⾥添加⼀个save保存⽅法
/**
* @desc 根据前台的信息从⽽添加⼀条学⽣信息到数据库
* @param sname
* @param sage
* @param ssex
* @throws SQLException
* @throws ClassNotFoundException
*/
public void save(String sname, String sage, String ssex)throws ClassNotFoundException, SQLException {
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论