1、步骤
1.1 准备一张数据库表
create table dept(
deptno int,
dname varchar(255),
loc varchar(255)
);
insert into dept(deptno,dname,loc)values(10,'销售部','北京'),
(20,'研发部','上海'),
(30,'技术部','广州'),
(40,'媒体部','深圳');
-- 提交
COMMIT;
select * from dept;
1.2 准备一套HTML页面
- 新增页面:add.html
- 修改页面:edit.html
- 详情页面:detail.html
- 欢迎页面:index.html
- 部门列表页面:list.html(以列表页面为核心,展开操作) ```html <!DOCTYPE html> 查看部门列表
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>部门列表</title>
</head>
<body>
<h1 align="center">部门列表</h1>
<hr>
<table border="1px" align="center" width="50%">
<tr>
<th>序号</th>
<th>部门编号</th>
<th>部门名称</th>
<th>操作</th>
</tr>
<tr>
<td>1</td>
<td>10</td>
<td>销售部</td>
<td>
<a href="javascript:void(0)" onclick="window.confirm('亲,确认删除该数据吗?')">删除</a>
<a href="edit.html">修改</a>
<a href="detail.html">详情</a>
</td>
</tr>
<tr>
<td>2</td>
<td>20</td>
<td>研发部</td>
<td>
<a href="javascript:void(0)" onclick="window.confirm('亲,确认删除该数据吗?')">删除</a>
<a href="edit.html">修改</a>
<a href="detail.html">详情</a>
</td>
</tr>
<tr>
<td>3</td>
<td>30</td>
<td>运用部</td>
<td>
<a href="javascript:void(0)" onclick="window.confirm('亲,确认删除该数据吗?')">删除</a>
<a href="edit.html">修改</a>
<a href="detail.html">详情</a>
</td>
</tr>
<tr>
<td>4</td>
<td>40</td>
<td>媒体部</td>
<td>
<a href="javascript:void(0)" onclick="window.confirm('亲,确认删除该数据吗?')">删除</a>
<a href="edit.html">修改</a>
<a href="detail.html">详情</a>
</td>
</tr>
</table>
<hr>
<a href="add.html">新增部门</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>部门详情</title>
</head>
<body>
<h1>部门详情</h1>
<hr>
部门编号<br>
部门名称<br>
部门位置<br>
<form action="list.html">
<input type="button" value="后退" onclick="window.history.back()">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改部门</title>
</head>
<body>
<h1>修改部门</h1>
<hr>
<form action="list.html" method="post">
部门编号<input type="text" name="deptno" value="20" readonly><br><!--readonly只读-->
部门名称<input type="text" name="dname" value="销售部"><br>
部门位置<input type="text" name="loc" value="北京"><br>
<input type="submit" value="修改"><br>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新增部门</title>
</head>
<body>
<h1>新增部门</h1>
<hr>
<form action="list.html" method="post">
部门编号<input type="text" name="deptno"><br>
部门名称<input type="text" name="dname"><br>
部门位置<input type="text" name="loc"><br>
<input type="submit" value="保存"><br>
</form>
</body>
</html>
1.3 分析这个系统包括哪些功能
- 查看部门列表
- 新增部门
- 删除部门
- 查看部门详情信息
- 跳转到修改页面
-
1.4 在IDEA中搭建开发环境
[x] 创建一个webapp(给这个webapp添加servlet-api-jar和jsp-api-jar到classpath当中
- 向webapp中添加连接数据库的jar包(MySQL驱动)
- 必须在WEB-INF目录下创建lib,然后将mysql驱动jar包导入
- JDBC的工具类
package oa;
/**
* @Author: 小雷学长
* @Date: 2022/3/22 - 0:42
* @Version: 1.8
*/
import javax.xml.transform.Result;
import java.sql.*;
import java.util.ResourceBundle;
/**
* JDBC工具类
*/
public class DBUtil {
//静态变量:也是在类加载时执行,并且是有自上而下的顺序
//资源绑定器
private static ResourceBundle bundle = ResourceBundle.getBundle("resources.jdbc");
//根据属性配置文件的key获取value
private static String driver = bundle.getString("driver");
private static String url = bundle.getString("url");
private static String user = bundle.getString("user");
private static String password = bundle.getString("password");
static {
//注册驱动(注册驱动只需要注册一次,放在静态代码块中,DBUtil类加载的时候执行)
try {
/* com.mysql.jdbc.Driver是连接数据库的驱动,不能写死,因为以后可能还会写Oracle数据库
* OCP开闭原则:对扩展开放,对修改关闭。(OCP:在进行拓展的时候不需要修改Java源码
* 通过属性配置文件jdbc.properties来获取驱动
*/
//Class.forName("com.mysql.jdbc.Driver");
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接对象
*
* @return conn
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
//获取连接
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
* 释放资源
* @param conn 连接对象
* @param ps 数据库操作对象
* @param rs 结果集对象
*/
public static void close(Connection conn, Statement ps, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/study
user=root
password=root
-
1.5 实现连接
1.51 注意
[x] 如何实现?
- 可以从后端一步一步往前端写,也可以从前端一步一步往后端写,不要想起来什么写什么。要根据程序执行的过程。程序到哪里,就写哪里,这样可以避免错误
- 假设从前端开始,一定是从用户点击按钮开始
1.52 步骤
先修改前端页面的超链接,因为用户先点击的就是超链接
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>欢迎使用oa系统</title>
</head>
<body>
<a href="/oa/dept/list">查看部门列表</a>
</body>
</html>
编写web.xml文件 ```xml <?xml version=”1.0” encoding=”UTF-8”?>
list oa.DeptListServlet list /dept/list
---
3. 编写DeptListServlet类继承HttpServlet类,然后重写doGet方法
```java
package oa.action;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author: 小雷学长
* @Date: 2022/3/22 - 10:05
* @Version: 1.8
*/
public class DeptListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
- 在DeptServlet类的doGet方法中连接数据库,查询所有的部门,动态的展示部门列表页面
- 分析list.html页面中哪部分是固定不变的,哪部分是需要动态展示的
- list.html页面中的内容所有的双引号,要变成单引号,因为print.out(“”)这里有一个双引号,容易冲突 ```java package oa.action;
import com.sun.corba.se.spi.orb.PropertyParser; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import oa.untils.DBUtil;
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
/**
- @Author: 小雷学长
- @Date: 2022/3/22 - 10:05
@Version: 1.8 */ public class DeptListServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置响应的内容类型
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print(" <!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>部门列表</title>");
out.print("</head>");
out.print("<body>");
out.print("<h1 align='center'>部门列表</h1>");
out.print("<hr>");
out.print("<table border='1px' align='center' width='50%'>");
out.print(" <tr>");
out.print(" <th>序号</th>");
out.print(" <th>部门编号</th>");
out.print(" <th>部门名称</th>");
out.print(" <th>操作</th>");
out.print(" </tr>");
out.print(" <!--以上是固定的-->");
/*
上面一部分是死的
*/
/*
* 连接数据库
*/
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获取连接
conn = DBUtil.getConnection();
//获取预编译的数据库操作对象
String sql = "select deptno,dname ,loc from dept";
ps = conn.prepareStatement(sql);
//执行SQL语句
rs = ps.executeQuery();
//处理结果集
int i = 0;
while (rs.next()) {
String deptno = rs.getString("deptno");
String dname = rs.getString("dname");
String loc = rs.getString("loc");
out.print(" <tr>");
out.print(" <td>" + (++i) + "</td>");
out.print(" <td>" + deptno + "</td>");
out.print(" <td>" + dname + "</td>");
out.print(" <td>");
out.print(" <a href=''></a>");
out.print(" <a href='edit.html'>修改</a>");
out.print(" <a href='detail.html'>详情</a>");
out.print(" </td>");
out.print(" </tr>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//释放资源
DBUtil.close(conn, ps, rs);
}
/*
下面一部分是死的
*/
out.print(" <!--一下是固定的-->");
out.print("</table>");
out.print("<hr>");
out.print("<a href=' add.html'>新增部门</a>");
out.print("</body>");
out.print("</html>");
} }
---
<a name="OjnZ1"></a>
## 1.6 查看部门详情
- [x] 从前端往后端一步一步实现,要考虑的是用户要点击的是什么,用户点击的在哪里
```java
out.print("<a href='" + contextPath + "/dept/detail?deptno = " + deptno + "'>详情</a>");
- 向服务器提交数据的格式:
url?name=value&name=value&name=value&name=value
- 这里的问号必须是英文的问号,不能写中文的问号,url不能带空格
- 编写一个类:DeptDetailServelt.java ```java package oa.action;
import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import oa.untils.DBUtil;
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
/**
- @Author: 小雷学长
- @Date: 2022/3/22 - 15:01
@Version: 1.8 */ public class DeptDetailServelt extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("<!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>部门详情</title>");
out.print("</head>");
out.print("<body>");
out.print("<h1>部门详情</h1>");
out.print("<hr>");
//获取部门编号
//虽然提交的是30,但服务器获取的是30这个字符串
String deptno = request.getParameter("deptno");
/*
* 连接数据库
*/
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获取连接
conn = DBUtil.getConnection();
//获取预编译的数据库操作对象
String sql = "select deptno,dname,loc from dept where deptno=?";
ps = conn.prepareStatement(sql);
ps.setString(1, deptno);
//这个结果集一定只有一个元素
//执行SQL语句
rs = ps.executeQuery();
//处理结果集
if (rs.next()) {
String dname = rs.getString("dname");
String loc = rs.getString("loc");
out.print("部门编号" + deptno + "<br>");
out.print("部门名称" + dname + "<br>");
out.print("部门位置" + loc + "<br>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//释放资源
DBUtil.close(conn, ps, rs);
}
out.print("");
out.print("<form action='list.html'>");
out.print(" <input type='button' value='后退' onclick='window.history.back()'>");
out.print("");
out.print("</form>");
out.print("");
out.print("</body>");
out.print("</html>");
}
}
---
<a name="BIsh5"></a>
## 1.7 实现删除功能
<a name="olPTU"></a>
### 1.71 方法
- [x] `<!--href后面设置为javascript:void(0)表示:仍然保留住超链接的样子,只是点击超链接之后,只执行后面的js代码,不进行页面的跳转,不一定是0-->`
- [x] JavaScript确认删除提示的方法
```javascript
<script type="text/javascript">
function del() {
//弹出确认框,用户点击确定返回TRUE,点击取消返回FALSE
var ok = window.confirm("亲,删了不可恢复哦!");
if (ok){
//发送请求,进行删除数据的操作
//在js代码中如何发送请求给服务器???????
alert("正在删除数据,请稍后……")
/*
四种获取数据的方法
*/
// document.location.href = "请求路径"
//document.locaction = "请求路径"
//window.location.href = "请求路径"
//window.location = "请求路径"
document.location.href = "项目名/请求路径";
}
}
</script>
<!--调用del方法-->
<tr>
<td>1</td>
<td>10</td>
<td>销售部</td>
<td>
<a href="javascript:void(0)" onclick="del()">删除</a>
<a href='edit.html'>修改</a>
<a href='detail.html'>详情</a>
</td>
</tr>
[x] 四种获取请求的方法
document.location.href = "请求路径"
document.locaction = "请求路径"
window.location.href = "请求路径"
window.location = "请求路径"
1.72 实现删除
[x] 前端程序要写到后端的java代码中,因为没有JSP的支持,只能在后端里写前端代码 ```java package oa.action;
import com.sun.corba.se.spi.orb.PropertyParser; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import oa.untils.DBUtil;
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
/**
- @Author: 小雷学长
- @Date: 2022/3/22 - 10:05
@Version: 1.8 */ public class DeptListServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取应用的根路径
String contextPath = request.getContextPath();
//设置响应的内容类型
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print(" <!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>部门列表</title>");
out.print("</head>");
out.print("<body>");
out.print("<script type='text/javascript'>");
out.print(" function del(dno) {");
out.print(" if(window.confirm('亲,删了不可恢复哦!')){");
out.print(" document.location.href = 'oa/dept/delete?deptno=' + dno;");
out.print(" }");
out.print("}");
out.print("</script>");
out.print("<h1 align='center'>部门列表</h1>");
out.print("<hr>");
out.print("<table border='1px' align='center' width='50%'>");
out.print(" <tr>");
out.print(" <th>序号</th>");
out.print(" <th>部门编号</th>");
out.print(" <th>部门名称</th>");
out.print(" <th>操作</th>");
out.print(" </tr>");
out.print(" <!--以上是固定的-->");
/*
上面一部分是死的
*/
/*
* 连接数据库
*/
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获取连接
conn = DBUtil.getConnection();
//获取预编译的数据库操作对象
String sql = "select deptno,dname ,loc from dept";
ps = conn.prepareStatement(sql);
//执行SQL语句
rs = ps.executeQuery();
//处理结果集
int i = 0;
while (rs.next()) {
String deptno = rs.getString("deptno");
String dname = rs.getString("dname");
String loc = rs.getString("loc");
out.print(" <tr>");
out.print(" <td>" + (++i) + "</td>");
out.print(" <td>" + deptno + "</td>");
out.print(" <td>" + dname + "</td>");
out.print(" <td>");
out.print("<a href='javascript:void(0)' onclick='del(" + deptno + ")')'>删除</a>");
out.print(" <a href='edit.html'>修改</a>");
out.print(" <a href='" + contextPath + "/dept/detail?deptno=" + deptno + "'>详情</a>");
out.print(" </td>");
out.print(" </tr>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//释放资源
DBUtil.close(conn, ps, rs);
}
/*
下面一部分是死的
*/
out.print(" <!--一下是固定的-->");
out.print("</table>");
out.print("<hr>");
out.print("<a href=' add.html'>新增部门</a>");
out.print("</body>");
out.print("</html>");
} }
- [x] 点击删除会出现404问题,解决
- 配置web.xml
```java
<!--删除-->
<servlet>
<servlet-name>delete</servlet-name>
<servlet-class>oa.action.DeptDelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>delete</servlet-name>
<url-pattern>/dept/delete</url-pattern>
</servlet-mapping>
1.73 最终实现
package oa.action;
import com.sun.corba.se.spi.orb.PropertyParser;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import oa.untils.DBUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Author: 小雷学长
* @Date: 2022/3/22 - 10:05
* @Version: 1.8
*/
public class DeptListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取应用的根路径
String contextPath = request.getContextPath();
//设置响应的内容类型
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print(" <!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>部门列表</title>");
out.print("</head>");
out.print("<body>");
out.print("<script type='text/javascript'>");
out.print(" function del(dno) {");
out.print(" if(window.confirm('亲,删了不可恢复哦!')){");
out.print(" document.location.href = '"+contextPath+"/dept/delete?deptno=' + dno");
out.print(" }");
out.print("}");
out.print("</script>");
out.print("<h1 align='center'>部门列表</h1>");
out.print("<hr>");
out.print("<table border='1px' align='center' width='50%'>");
out.print(" <tr>");
out.print(" <th>序号</th>");
out.print(" <th>部门编号</th>");
out.print(" <th>部门名称</th>");
out.print(" <th>操作</th>");
out.print(" </tr>");
out.print(" <!--以上是固定的-->");
/*
上面一部分是死的
*/
/*
* 连接数据库
*/
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获取连接
conn = DBUtil.getConnection();
//获取预编译的数据库操作对象
String sql = "select deptno,dname ,loc from dept";
ps = conn.prepareStatement(sql);
//执行SQL语句
rs = ps.executeQuery();
//处理结果集
int i = 0;
while (rs.next()) {
String deptno = rs.getString("deptno");
String dname = rs.getString("dname");
String loc = rs.getString("loc");
out.print(" <tr>");
out.print(" <td>" + (++i) + "</td>");
out.print(" <td>" + deptno + "</td>");
out.print(" <td>" + dname + "</td>");
out.print(" <td>");
out.print("<a href='javascript:void(0)' onclick='del("+ deptno +")'>删除</a>");
out.print(" <a href='edit.html'>修改</a>");
out.print(" <a href='" + contextPath + "/dept/detail?deptno=" + deptno + "'>详情</a>");
out.print(" </td>");
out.print(" </tr>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//释放资源
DBUtil.close(conn, ps, rs);
}
/*
下面一部分是死的
*/
out.print(" <!--一下是固定的-->");
out.print("</table>");
out.print("<hr>");
out.print("<a href=' add.html'>新增部门</a>");
out.print("</body>");
out.print("</html>");
}
}
package oa.action;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import oa.untils.DBUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Author: 小雷学长
* @Date: 2022/3/22 - 15:01
* @Version: 1.8
*/
/**
* 实现查看部门详情
*/
public class DeptDetailServelt extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("<!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>部门详情</title>");
out.print("</head>");
out.print("<body>");
out.print("<h1>部门详情</h1>");
out.print("<hr>");
//获取部门编号
//虽然提交的是30,但服务器获取的是30这个字符串
String deptno = request.getParameter("deptno");
/*
* 连接数据库
*/
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获取连接
conn = DBUtil.getConnection();
//获取预编译的数据库操作对象
String sql = "select deptno,dname,loc from dept where deptno=?";
ps = conn.prepareStatement(sql);
ps.setString(1, deptno);
//这个结果集一定只有一个元素
//执行SQL语句
rs = ps.executeQuery();
//处理结果集
if (rs.next()) {
String dname = rs.getString("dname");
String loc = rs.getString("loc");
out.print("部门编号" + deptno + "<br>");
out.print("部门名称" + dname + "<br>");
out.print("部门位置" + loc + "<br>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//释放资源
DBUtil.close(conn, ps, rs);
}
out.print("");
out.print("<form action='list.html'>");
out.print(" <input type='button' value='后退' onclick='window.history.back()'>");
out.print("");
out.print("</form>");
out.print("");
out.print("</body>");
out.print("</html>");
}
}
package oa.action;
import com.sun.net.httpserver.HttpPrincipal;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jdk.internal.org.objectweb.asm.TypeReference;
import oa.untils.DBUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Author: 小雷学长
* @Date: 2022/3/22 - 18:08
* @Version: 1.8
*/
public class DeptDelServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//根据部门编号,删除部门
//获取部门编号
String deptno = request.getParameter("deptno");
//连接数据库删除数据
Connection conn = null;
PreparedStatement ps = null;
int count = 0;
try {
conn = DBUtil.getConnection();
//开启事务(自动提交机制关闭)(非必要)
// conn.setAutoCommit(false);
String sql = "delete from dept where deptno=?";
ps = conn.prepareStatement(sql);
ps.setString(1, deptno);
//返回值是:影响了数据库表当中多少条记录
count = ps.executeUpdate();
//事务提交(非必要)
// conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
//判断删除成功还是失败
if (count == 1) {
//删除成功
//仍然跳回到部门列表页面
//部门列表页面需要执行另一个Servlet,利用转发机制
request.getRequestDispatcher("/dept/list").forward(request, response);
} else {
//删除失败
request.getRequestDispatcher("/error.html").forward(request, response);
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h1>操作失败<a href="javascript:void(0)" onclick="window.history.back()">返回</a></h1>
</body>
</html>
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/study
user=root
password=123456
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--查看部门列表-->
<servlet>
<servlet-name>list</servlet-name>
<servlet-class>oa.action.DeptListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>list</servlet-name>
<url-pattern>/dept/list</url-pattern>
</servlet-mapping>
<!--查看详情-->
<servlet>
<servlet-name>detail</servlet-name>
<servlet-class>oa.action.DeptDetailServelt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>detail</servlet-name>
<url-pattern>/dept/detail</url-pattern>
</servlet-mapping>
<!--删除-->
<servlet>
<servlet-name>delete</servlet-name>
<servlet-class>oa.action.DeptDelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>delete</servlet-name>
<url-pattern>/dept/delete</url-pattern>
</servlet-mapping>
</web-app>
1.8 部门新增功能
package oa.action;
import com.sun.corba.se.spi.orb.PropertyParser;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import oa.untils.DBUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Queue;
/**
* @Author: 小雷学长
* @Date: 2022/3/22 - 10:05
* @Version: 1.8
*/
public class DeptListServlet extends HttpServlet {
//处理Post请求
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取应用的根路径
String contextPath = request.getContextPath();
//设置响应的内容类型
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print(" <!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>部门列表</title>");
out.print("</head>");
out.print("<body>");
out.print("<script type='text/javascript'>");
out.print(" function del(dno) {");
out.print(" if(window.confirm('亲,删了不可恢复哦!')){");
out.print(" document.location.href = '" + contextPath + "/dept/delete?deptno=' + dno");
out.print(" }");
out.print("}");
out.print("</script>");
out.print("<h1 align='center'>部门列表</h1>");
out.print("<hr>");
out.print("<table border='1px' align='center' width='50%'>");
out.print(" <tr>");
out.print(" <th>序号</th>");
out.print(" <th>部门编号</th>");
out.print(" <th>部门名称</th>");
out.print(" <th>操作</th>");
out.print(" </tr>");
out.print(" <!--以上是固定的-->");
/*
上面一部分是死的
*/
/*
* 连接数据库
*/
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获取连接
conn = DBUtil.getConnection();
//获取预编译的数据库操作对象
String sql = "select deptno,dname ,loc from dept";
ps = conn.prepareStatement(sql);
//执行SQL语句
rs = ps.executeQuery();
//处理结果集
int i = 0;
while (rs.next()) {
String deptno = rs.getString("deptno");
String dname = rs.getString("dname");
String loc = rs.getString("loc");
out.print(" <tr>");
out.print(" <td>" + (++i) + "</td>");
out.print(" <td>" + deptno + "</td>");
out.print(" <td>" + dname + "</td>");
out.print(" <td>");
out.print("<a href='javascript:void(0)' onclick='del(" + deptno + ")'>删除</a>");
out.print(" <a href='edit.html'>修改</a>");
out.print(" <a href='" + contextPath + "/dept/detail?deptno=" + deptno + "'>详情</a>");
out.print(" </td>");
out.print(" </tr>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//释放资源
DBUtil.close(conn, ps, rs);
}
/*
下面一部分是死的
*/
out.print(" <!--一下是固定的-->");
out.print("</table>");
out.print("<hr>");
out.print("<a href='" + contextPath + "/add.html'>新增部门</a>");
out.print("</body>");
out.print("</html>");
}
}
package oa.action;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import oa.untils.DBUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* @Author: 小雷学长
* @Date: 2022/3/22 - 20:36
* @Version: 1.8
*/
public class DeptSaveServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取部门信息
String deptno = request.getParameter("deptno");
String dname = request.getParameter("dname");
String loc = request.getParameter("loc");
//连接数据库执行insert语句
Connection conn = null;
PreparedStatement ps = null;
int count = 0;
try {
conn = DBUtil.getConnection();
String sql = "insert into dept(deptno,dname,loc) values(?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, deptno);
ps.setString(2, dname);
ps.setString(3, loc);
count = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
//判断新增成功还是失败
if (count == 1) {
//新增成功
//仍然跳回到部门列表页面
//部门列表页面需要执行另一个Servlet,利用转发机制
//转发一次请求
request.getRequestDispatcher("/dept/list").forward(request, response);
} else {
//新增失败
request.getRequestDispatcher("/error.html").forward(request, response);
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新增部门</title>
</head>
<body>
<h1>新增部门</h1>
<hr>
<form action="/oa/dept/save" method="post">
部门编号<input type="text" name="deptno"><br>
部门名称<input type="text" name="dname"><br>
部门位置<input type="text" name="loc"><br>
<input type="submit" value="保存"><br>
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--查看部门列表-->
<servlet>
<servlet-name>list</servlet-name>
<servlet-class>oa.action.DeptListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>list</servlet-name>
<url-pattern>/dept/list</url-pattern>
</servlet-mapping>
<!--查看详情-->
<servlet>
<servlet-name>detail</servlet-name>
<servlet-class>oa.action.DeptDetailServelt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>detail</servlet-name>
<url-pattern>/dept/detail</url-pattern>
</servlet-mapping>
<!--删除-->
<servlet>
<servlet-name>delete</servlet-name>
<servlet-class>oa.action.DeptDelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>delete</servlet-name>
<url-pattern>/dept/delete</url-pattern>
</servlet-mapping>
<!--保存-->
<servlet>
<servlet-name>save</servlet-name>
<servlet-class>oa.action.DeptSaveServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>save</servlet-name>
<url-pattern>/dept/save</url-pattern>
</servlet-mapping>
</web-app>
1.9 实现部门的修改
package oa.action;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import oa.untils.DBUtil;
import java.awt.print.Printable;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Author: 小雷学长
* @Date: 2022/3/23 - 11:19
* @Version: 1.8
*/
public class DeptUpdateServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String deptno = request.getParameter("deptno");
String dname = request.getParameter("dname");
String loc = request.getParameter("loc");
//连接数据库执行insert语句
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
int count = 0;
try {
conn = DBUtil.getConnection();
String sql = "update dept set dname = ?,loc = ? where deptno = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, dname);
ps.setString(2, loc);
ps.setString(3, deptno);
count = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
if (count == 1){
//更新成功
//跳转到部门列表页面,转发机制
request.getRequestDispatcher("/dept/list").forward(request,response);
}
else {
//更新失败
request.getRequestDispatcher("/error.html").forward(request,response);
}
}
}
package oa.action;
import com.sun.net.httpserver.HttpPrincipal;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import oa.untils.DBUtil;
import javax.xml.transform.Result;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Author: 小雷学长
* @Date: 2022/3/22 - 22:59
* @Version: 1.8
*/
public class DeptEditServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String contextPath = request.getContextPath();
out.print("<html lang='en'>");
out.print("<head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>修改部门</title>");
out.print("</head>");
out.print("<body>");
out.print("<h1>修改部门</h1>");
out.print("<hr>");
out.print("<form action='" + contextPath + "/dept/update' method='post'>");
//获取部门编号
String deptno = request.getParameter("deptno");
//连接数据库执行insert语句
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "select dname,loc from dept where deptno=?";
ps = conn.prepareStatement(sql);
ps.setString(1, deptno);
rs = ps.executeQuery();
if (rs.next()) {
String dname = rs.getString("dname");
String loc = rs.getString("loc");
//输出动态网页
out.print(" 部门编号<input type='text' name='deptno' value='" + deptno + "' readonly><br><!--readonly只读-->");
out.print(" 部门名称<input type='text' name='dname' value='" + dname + "'><br>");
out.print(" 部门位置<input type='text' name='loc' value='" + loc + "'><br>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
//判断新增成功还是失败
out.print(" <input type='submit' value='修改'><br>");
out.print("");
out.print("</form>");
out.print("");
out.print("</body>");
out.print("</html>");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--查看部门列表-->
<servlet>
<servlet-name>list</servlet-name>
<servlet-class>oa.action.DeptListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>list</servlet-name>
<url-pattern>/dept/list</url-pattern>
</servlet-mapping>
<!--查看详情-->
<servlet>
<servlet-name>detail</servlet-name>
<servlet-class>oa.action.DeptDetailServelt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>detail</servlet-name>
<url-pattern>/dept/detail</url-pattern>
</servlet-mapping>
<!--删除-->
<servlet>
<servlet-name>delete</servlet-name>
<servlet-class>oa.action.DeptDelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>delete</servlet-name>
<url-pattern>/dept/delete</url-pattern>
</servlet-mapping>
<!--保存-->
<servlet>
<servlet-name>save</servlet-name>
<servlet-class>oa.action.DeptSaveServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>save</servlet-name>
<url-pattern>/dept/save</url-pattern>
</servlet-mapping>
<!--修改页面-->
<servlet>
<servlet-name>edit</servlet-name>
<servlet-class>oa.action.DeptEditServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>edit</servlet-name>
<url-pattern>/dept/edit</url-pattern>
</servlet-mapping>
<!--修改部门-->
<servlet>
<servlet-name>update</servlet-name>
<servlet-class>oa.action.DeptUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>update</servlet-name>
<url-pattern>/dept/update</url-pattern>
</servlet-mapping>
</web-app>