1、步骤

1.1 准备一张数据库表

  1. create table dept(
  2. deptno int,
  3. dname varchar(255),
  4. loc varchar(255)
  5. );
  6. insert into dept(deptno,dname,loc)values(10,'销售部','北京'),
  7. (20,'研发部','上海'),
  8. (30,'技术部','广州'),
  9. (40,'媒体部','深圳');
  10. -- 提交
  11. COMMIT;
  12. select * from dept;

1.2 准备一套HTML页面

  • 新增页面:add.html
  • 修改页面:edit.html
  • 详情页面:detail.html
  • 欢迎页面:index.html
  • 部门列表页面:list.html(以列表页面为核心,展开操作) ```html <!DOCTYPE html> 查看部门列表

  1. ```html
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>部门列表</title>
  7. </head>
  8. <body>
  9. <h1 align="center">部门列表</h1>
  10. <hr>
  11. <table border="1px" align="center" width="50%">
  12. <tr>
  13. <th>序号</th>
  14. <th>部门编号</th>
  15. <th>部门名称</th>
  16. <th>操作</th>
  17. </tr>
  18. <tr>
  19. <td>1</td>
  20. <td>10</td>
  21. <td>销售部</td>
  22. <td>
  23. <a href="javascript:void(0)" onclick="window.confirm('亲,确认删除该数据吗?')">删除</a>
  24. <a href="edit.html">修改</a>
  25. <a href="detail.html">详情</a>
  26. </td>
  27. </tr>
  28. <tr>
  29. <td>2</td>
  30. <td>20</td>
  31. <td>研发部</td>
  32. <td>
  33. <a href="javascript:void(0)" onclick="window.confirm('亲,确认删除该数据吗?')">删除</a>
  34. <a href="edit.html">修改</a>
  35. <a href="detail.html">详情</a>
  36. </td>
  37. </tr>
  38. <tr>
  39. <td>3</td>
  40. <td>30</td>
  41. <td>运用部</td>
  42. <td>
  43. <a href="javascript:void(0)" onclick="window.confirm('亲,确认删除该数据吗?')">删除</a>
  44. <a href="edit.html">修改</a>
  45. <a href="detail.html">详情</a>
  46. </td>
  47. </tr>
  48. <tr>
  49. <td>4</td>
  50. <td>40</td>
  51. <td>媒体部</td>
  52. <td>
  53. <a href="javascript:void(0)" onclick="window.confirm('亲,确认删除该数据吗?')">删除</a>
  54. <a href="edit.html">修改</a>
  55. <a href="detail.html">详情</a>
  56. </td>
  57. </tr>
  58. </table>
  59. <hr>
  60. <a href="add.html">新增部门</a>
  61. </body>
  62. </html>
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>部门详情</title>
  6. </head>
  7. <body>
  8. <h1>部门详情</h1>
  9. <hr>
  10. 部门编号<br>
  11. 部门名称<br>
  12. 部门位置<br>
  13. <form action="list.html">
  14. <input type="button" value="后退" onclick="window.history.back()">
  15. </form>
  16. </body>
  17. </html>
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>修改部门</title>
  6. </head>
  7. <body>
  8. <h1>修改部门</h1>
  9. <hr>
  10. <form action="list.html" method="post">
  11. 部门编号<input type="text" name="deptno" value="20" readonly><br><!--readonly只读-->
  12. 部门名称<input type="text" name="dname" value="销售部"><br>
  13. 部门位置<input type="text" name="loc" value="北京"><br>
  14. <input type="submit" value="修改"><br>
  15. </form>
  16. </body>
  17. </html>
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>新增部门</title>
  6. </head>
  7. <body>
  8. <h1>新增部门</h1>
  9. <hr>
  10. <form action="list.html" method="post">
  11. 部门编号<input type="text" name="deptno"><br>
  12. 部门名称<input type="text" name="dname"><br>
  13. 部门位置<input type="text" name="loc"><br>
  14. <input type="submit" value="保存"><br>
  15. </form>
  16. </body>
  17. </html>

1.3 分析这个系统包括哪些功能

  • 查看部门列表
  • 新增部门
  • 删除部门
  • 查看部门详情信息
  • 跳转到修改页面
  • [x] 修改部门

    1.4 在IDEA中搭建开发环境

  • [x] 创建一个webapp(给这个webapp添加servlet-api-jar和jsp-api-jar到classpath当中

  • 向webapp中添加连接数据库的jar包(MySQL驱动)
    • 必须在WEB-INF目录下创建lib,然后将mysql驱动jar包导入
  • JDBC的工具类

image.png

  1. package oa;
  2. /**
  3. * @Author: 小雷学长
  4. * @Date: 2022/3/22 - 0:42
  5. * @Version: 1.8
  6. */
  7. import javax.xml.transform.Result;
  8. import java.sql.*;
  9. import java.util.ResourceBundle;
  10. /**
  11. * JDBC工具类
  12. */
  13. public class DBUtil {
  14. //静态变量:也是在类加载时执行,并且是有自上而下的顺序
  15. //资源绑定器
  16. private static ResourceBundle bundle = ResourceBundle.getBundle("resources.jdbc");
  17. //根据属性配置文件的key获取value
  18. private static String driver = bundle.getString("driver");
  19. private static String url = bundle.getString("url");
  20. private static String user = bundle.getString("user");
  21. private static String password = bundle.getString("password");
  22. static {
  23. //注册驱动(注册驱动只需要注册一次,放在静态代码块中,DBUtil类加载的时候执行)
  24. try {
  25. /* com.mysql.jdbc.Driver是连接数据库的驱动,不能写死,因为以后可能还会写Oracle数据库
  26. * OCP开闭原则:对扩展开放,对修改关闭。(OCP:在进行拓展的时候不需要修改Java源码
  27. * 通过属性配置文件jdbc.properties来获取驱动
  28. */
  29. //Class.forName("com.mysql.jdbc.Driver");
  30. Class.forName(driver);
  31. } catch (ClassNotFoundException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. /**
  36. * 获取数据库连接对象
  37. *
  38. * @return conn
  39. * @throws SQLException
  40. */
  41. public static Connection getConnection() throws SQLException {
  42. //获取连接
  43. Connection conn = DriverManager.getConnection(url, user, password);
  44. return conn;
  45. }
  46. /**
  47. * 释放资源
  48. * @param conn 连接对象
  49. * @param ps 数据库操作对象
  50. * @param rs 结果集对象
  51. */
  52. public static void close(Connection conn, Statement ps, ResultSet rs) {
  53. if (rs != null) {
  54. try {
  55. rs.close();
  56. } catch (SQLException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. if (ps != null) {
  61. try {
  62. ps.close();
  63. } catch (SQLException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. if (conn != null) {
  68. try {
  69. conn.close();
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. }
  75. }
  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:3306/study
  3. user=root
  4. password=root

image.png

  • [x] 将所有HTML页面拷贝到web目录下

    1.5 实现连接

    1.51 注意

  • [x] 如何实现?

    • 可以从后端一步一步往前端写,也可以从前端一步一步往后端写,不要想起来什么写什么。要根据程序执行的过程。程序到哪里,就写哪里,这样可以避免错误
  • 假设从前端开始,一定是从用户点击按钮开始

    1.52 步骤

  1. 先修改前端页面的超链接,因为用户先点击的就是超链接

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>欢迎使用oa系统</title>
    6. </head>
    7. <body>
    8. <a href="/oa/dept/list">查看部门列表</a>
    9. </body>
    10. </html>

  1. 编写web.xml文件 ```xml <?xml version=”1.0” encoding=”UTF-8”?>

    list oa.DeptListServlet list /dept/list

  1. ---
  2. 3. 编写DeptListServlet类继承HttpServlet类,然后重写doGet方法
  3. ```java
  4. package oa.action;
  5. import jakarta.servlet.ServletException;
  6. import jakarta.servlet.http.HttpServlet;
  7. import jakarta.servlet.http.HttpServletRequest;
  8. import jakarta.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. /**
  11. * @Author: 小雷学长
  12. * @Date: 2022/3/22 - 10:05
  13. * @Version: 1.8
  14. */
  15. public class DeptListServlet extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  18. throws ServletException, IOException {
  19. }
  20. }

  1. 在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)

    1. throws ServletException, IOException {
    2. //设置响应的内容类型
    3. response.setContentType("text/html");
    4. PrintWriter out = response.getWriter();
    5. out.print(" <!DOCTYPE html>");
    6. out.print("<html lang='en'>");
    7. out.print("<head>");
    8. out.print(" <meta charset='UTF-8'>");
    9. out.print(" <title>部门列表</title>");
    10. out.print("</head>");
    11. out.print("<body>");
    12. out.print("<h1 align='center'>部门列表</h1>");
    13. out.print("<hr>");
    14. out.print("<table border='1px' align='center' width='50%'>");
    15. out.print(" <tr>");
    16. out.print(" <th>序号</th>");
    17. out.print(" <th>部门编号</th>");
    18. out.print(" <th>部门名称</th>");
    19. out.print(" <th>操作</th>");
    20. out.print(" </tr>");
    21. out.print(" <!--以上是固定的-->");
    22. /*
    23. 上面一部分是死的
    24. */
    25. /*
    26. * 连接数据库
    27. */
    28. Connection conn = null;
    29. PreparedStatement ps = null;
    30. ResultSet rs = null;
    31. try {
    32. //获取连接
    33. conn = DBUtil.getConnection();
    34. //获取预编译的数据库操作对象
    35. String sql = "select deptno,dname ,loc from dept";
    36. ps = conn.prepareStatement(sql);
    37. //执行SQL语句
    38. rs = ps.executeQuery();
    39. //处理结果集
    40. int i = 0;
    41. while (rs.next()) {
    42. String deptno = rs.getString("deptno");
    43. String dname = rs.getString("dname");
    44. String loc = rs.getString("loc");
    45. out.print(" <tr>");
    46. out.print(" <td>" + (++i) + "</td>");
    47. out.print(" <td>" + deptno + "</td>");
    48. out.print(" <td>" + dname + "</td>");
    49. out.print(" <td>");
    50. out.print(" <a href=''></a>");
    51. out.print(" <a href='edit.html'>修改</a>");
    52. out.print(" <a href='detail.html'>详情</a>");
    53. out.print(" </td>");
    54. out.print(" </tr>");
    55. }
    56. } catch (SQLException e) {
    57. e.printStackTrace();
    58. } finally {
    59. //释放资源
    60. DBUtil.close(conn, ps, rs);
    61. }
    62. /*
    63. 下面一部分是死的
    64. */
    65. out.print(" <!--一下是固定的-->");
    66. out.print("</table>");
    67. out.print("<hr>");
    68. out.print("<a href=' add.html'>新增部门</a>");
    69. out.print("</body>");
    70. out.print("</html>");

    } }

  1. ---
  2. <a name="OjnZ1"></a>
  3. ## 1.6 查看部门详情
  4. - [x] 从前端往后端一步一步实现,要考虑的是用户要点击的是什么,用户点击的在哪里
  5. ```java
  6. out.print("<a href='" + contextPath + "/dept/detail?deptno = " + deptno + "'>详情</a>");
  • 向服务器提交数据的格式:url?name=value&name=value&name=value&name=value
  • 这里的问号必须是英文的问号,不能写中文的问号,url不能带空格

  1. 编写一个类: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)

    1. throws ServletException, IOException {
    2. response.setContentType("text/html");
    3. PrintWriter out = response.getWriter();
  1. out.print("<!DOCTYPE html>");
  2. out.print("<html lang='en'>");
  3. out.print("<head>");
  4. out.print(" <meta charset='UTF-8'>");
  5. out.print(" <title>部门详情</title>");
  6. out.print("</head>");
  7. out.print("<body>");
  8. out.print("<h1>部门详情</h1>");
  9. out.print("<hr>");
  10. //获取部门编号
  11. //虽然提交的是30,但服务器获取的是30这个字符串
  12. String deptno = request.getParameter("deptno");
  13. /*
  14. * 连接数据库
  15. */
  16. Connection conn = null;
  17. PreparedStatement ps = null;
  18. ResultSet rs = null;
  19. try {
  20. //获取连接
  21. conn = DBUtil.getConnection();
  22. //获取预编译的数据库操作对象
  23. String sql = "select deptno,dname,loc from dept where deptno=?";
  24. ps = conn.prepareStatement(sql);
  25. ps.setString(1, deptno);
  26. //这个结果集一定只有一个元素
  27. //执行SQL语句
  28. rs = ps.executeQuery();
  29. //处理结果集
  30. if (rs.next()) {
  31. String dname = rs.getString("dname");
  32. String loc = rs.getString("loc");
  33. out.print("部门编号" + deptno + "<br>");
  34. out.print("部门名称" + dname + "<br>");
  35. out.print("部门位置" + loc + "<br>");
  36. }
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. } finally {
  40. //释放资源
  41. DBUtil.close(conn, ps, rs);
  42. }
  43. out.print("");
  44. out.print("<form action='list.html'>");
  45. out.print(" <input type='button' value='后退' onclick='window.history.back()'>");
  46. out.print("");
  47. out.print("</form>");
  48. out.print("");
  49. out.print("</body>");
  50. out.print("</html>");
  51. }

}

  1. ---
  2. <a name="BIsh5"></a>
  3. ## 1.7 实现删除功能
  4. <a name="olPTU"></a>
  5. ### 1.71 方法
  6. - [x] `<!--href后面设置为javascript:void(0)表示:仍然保留住超链接的样子,只是点击超链接之后,只执行后面的js代码,不进行页面的跳转,不一定是0-->`
  7. - [x] JavaScript确认删除提示的方法
  8. ```javascript
  9. <script type="text/javascript">
  10. function del() {
  11. //弹出确认框,用户点击确定返回TRUE,点击取消返回FALSE
  12. var ok = window.confirm("亲,删了不可恢复哦!");
  13. if (ok){
  14. //发送请求,进行删除数据的操作
  15. //在js代码中如何发送请求给服务器???????
  16. alert("正在删除数据,请稍后……")
  17. /*
  18. 四种获取数据的方法
  19. */
  20. // document.location.href = "请求路径"
  21. //document.locaction = "请求路径"
  22. //window.location.href = "请求路径"
  23. //window.location = "请求路径"
  24. document.location.href = "项目名/请求路径";
  25. }
  26. }
  27. </script>
  28. <!--调用del方法-->
  29. <tr>
  30. <td>1</td>
  31. <td>10</td>
  32. <td>销售部</td>
  33. <td>
  34. <a href="javascript:void(0)" onclick="del()">删除</a>
  35. <a href='edit.html'>修改</a>
  36. <a href='detail.html'>详情</a>
  37. </td>
  38. </tr>

image.png
image.png

  • [x] 四种获取请求的方法

    1. document.location.href = "请求路径"
    2. document.locaction = "请求路径"
    3. window.location.href = "请求路径"
    4. 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)

    1. throws ServletException, IOException {
    2. //获取应用的根路径
    3. String contextPath = request.getContextPath();
    4. //设置响应的内容类型
    5. response.setContentType("text/html");
    6. PrintWriter out = response.getWriter();
    7. out.print(" <!DOCTYPE html>");
    8. out.print("<html lang='en'>");
    9. out.print("<head>");
    10. out.print(" <meta charset='UTF-8'>");
    11. out.print(" <title>部门列表</title>");
    12. out.print("</head>");
    13. out.print("<body>");
    14. out.print("<script type='text/javascript'>");
    15. out.print(" function del(dno) {");
    16. out.print(" if(window.confirm('亲,删了不可恢复哦!')){");
    17. out.print(" document.location.href = 'oa/dept/delete?deptno=' + dno;");
    18. out.print(" }");
    19. out.print("}");
    20. out.print("</script>");
    21. out.print("<h1 align='center'>部门列表</h1>");
    22. out.print("<hr>");
    23. out.print("<table border='1px' align='center' width='50%'>");
    24. out.print(" <tr>");
    25. out.print(" <th>序号</th>");
    26. out.print(" <th>部门编号</th>");
    27. out.print(" <th>部门名称</th>");
    28. out.print(" <th>操作</th>");
    29. out.print(" </tr>");
    30. out.print(" <!--以上是固定的-->");
    31. /*
    32. 上面一部分是死的
    33. */
    34. /*
    35. * 连接数据库
    36. */
    37. Connection conn = null;
    38. PreparedStatement ps = null;
    39. ResultSet rs = null;
    40. try {
    41. //获取连接
    42. conn = DBUtil.getConnection();
    43. //获取预编译的数据库操作对象
    44. String sql = "select deptno,dname ,loc from dept";
    45. ps = conn.prepareStatement(sql);
    46. //执行SQL语句
    47. rs = ps.executeQuery();
    48. //处理结果集
    49. int i = 0;
    50. while (rs.next()) {
    51. String deptno = rs.getString("deptno");
    52. String dname = rs.getString("dname");
    53. String loc = rs.getString("loc");
    54. out.print(" <tr>");
    55. out.print(" <td>" + (++i) + "</td>");
    56. out.print(" <td>" + deptno + "</td>");
    57. out.print(" <td>" + dname + "</td>");
    58. out.print(" <td>");
    59. out.print("<a href='javascript:void(0)' onclick='del(" + deptno + ")')'>删除</a>");
    60. out.print(" <a href='edit.html'>修改</a>");
    61. out.print(" <a href='" + contextPath + "/dept/detail?deptno=" + deptno + "'>详情</a>");
    62. out.print(" </td>");
    63. out.print(" </tr>");
    64. }
    65. } catch (SQLException e) {
    66. e.printStackTrace();
    67. } finally {
    68. //释放资源
    69. DBUtil.close(conn, ps, rs);
    70. }
    71. /*
    72. 下面一部分是死的
    73. */
    74. out.print(" <!--一下是固定的-->");
    75. out.print("</table>");
    76. out.print("<hr>");
    77. out.print("<a href=' add.html'>新增部门</a>");
    78. out.print("</body>");
    79. out.print("</html>");

    } }

  1. - [x] 点击删除会出现404问题,解决
  2. - 配置web.xml
  3. ```java
  4. <!--删除-->
  5. <servlet>
  6. <servlet-name>delete</servlet-name>
  7. <servlet-class>oa.action.DeptDelServlet</servlet-class>
  8. </servlet>
  9. <servlet-mapping>
  10. <servlet-name>delete</servlet-name>
  11. <url-pattern>/dept/delete</url-pattern>
  12. </servlet-mapping>

1.73 最终实现

image.png

  1. package oa.action;
  2. import com.sun.corba.se.spi.orb.PropertyParser;
  3. import jakarta.servlet.ServletException;
  4. import jakarta.servlet.http.HttpServlet;
  5. import jakarta.servlet.http.HttpServletRequest;
  6. import jakarta.servlet.http.HttpServletResponse;
  7. import oa.untils.DBUtil;
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. /**
  15. * @Author: 小雷学长
  16. * @Date: 2022/3/22 - 10:05
  17. * @Version: 1.8
  18. */
  19. public class DeptListServlet extends HttpServlet {
  20. @Override
  21. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  22. throws ServletException, IOException {
  23. //获取应用的根路径
  24. String contextPath = request.getContextPath();
  25. //设置响应的内容类型
  26. response.setContentType("text/html");
  27. PrintWriter out = response.getWriter();
  28. out.print(" <!DOCTYPE html>");
  29. out.print("<html lang='en'>");
  30. out.print("<head>");
  31. out.print(" <meta charset='UTF-8'>");
  32. out.print(" <title>部门列表</title>");
  33. out.print("</head>");
  34. out.print("<body>");
  35. out.print("<script type='text/javascript'>");
  36. out.print(" function del(dno) {");
  37. out.print(" if(window.confirm('亲,删了不可恢复哦!')){");
  38. out.print(" document.location.href = '"+contextPath+"/dept/delete?deptno=' + dno");
  39. out.print(" }");
  40. out.print("}");
  41. out.print("</script>");
  42. out.print("<h1 align='center'>部门列表</h1>");
  43. out.print("<hr>");
  44. out.print("<table border='1px' align='center' width='50%'>");
  45. out.print(" <tr>");
  46. out.print(" <th>序号</th>");
  47. out.print(" <th>部门编号</th>");
  48. out.print(" <th>部门名称</th>");
  49. out.print(" <th>操作</th>");
  50. out.print(" </tr>");
  51. out.print(" <!--以上是固定的-->");
  52. /*
  53. 上面一部分是死的
  54. */
  55. /*
  56. * 连接数据库
  57. */
  58. Connection conn = null;
  59. PreparedStatement ps = null;
  60. ResultSet rs = null;
  61. try {
  62. //获取连接
  63. conn = DBUtil.getConnection();
  64. //获取预编译的数据库操作对象
  65. String sql = "select deptno,dname ,loc from dept";
  66. ps = conn.prepareStatement(sql);
  67. //执行SQL语句
  68. rs = ps.executeQuery();
  69. //处理结果集
  70. int i = 0;
  71. while (rs.next()) {
  72. String deptno = rs.getString("deptno");
  73. String dname = rs.getString("dname");
  74. String loc = rs.getString("loc");
  75. out.print(" <tr>");
  76. out.print(" <td>" + (++i) + "</td>");
  77. out.print(" <td>" + deptno + "</td>");
  78. out.print(" <td>" + dname + "</td>");
  79. out.print(" <td>");
  80. out.print("<a href='javascript:void(0)' onclick='del("+ deptno +")'>删除</a>");
  81. out.print(" <a href='edit.html'>修改</a>");
  82. out.print(" <a href='" + contextPath + "/dept/detail?deptno=" + deptno + "'>详情</a>");
  83. out.print(" </td>");
  84. out.print(" </tr>");
  85. }
  86. } catch (SQLException e) {
  87. e.printStackTrace();
  88. } finally {
  89. //释放资源
  90. DBUtil.close(conn, ps, rs);
  91. }
  92. /*
  93. 下面一部分是死的
  94. */
  95. out.print(" <!--一下是固定的-->");
  96. out.print("</table>");
  97. out.print("<hr>");
  98. out.print("<a href=' add.html'>新增部门</a>");
  99. out.print("</body>");
  100. out.print("</html>");
  101. }
  102. }
  1. package oa.action;
  2. import jakarta.servlet.ServletException;
  3. import jakarta.servlet.http.HttpServlet;
  4. import jakarta.servlet.http.HttpServletRequest;
  5. import jakarta.servlet.http.HttpServletResponse;
  6. import oa.untils.DBUtil;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.sql.Connection;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. /**
  14. * @Author: 小雷学长
  15. * @Date: 2022/3/22 - 15:01
  16. * @Version: 1.8
  17. */
  18. /**
  19. * 实现查看部门详情
  20. */
  21. public class DeptDetailServelt extends HttpServlet {
  22. @Override
  23. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  24. throws ServletException, IOException {
  25. response.setContentType("text/html");
  26. PrintWriter out = response.getWriter();
  27. out.print("<!DOCTYPE html>");
  28. out.print("<html lang='en'>");
  29. out.print("<head>");
  30. out.print(" <meta charset='UTF-8'>");
  31. out.print(" <title>部门详情</title>");
  32. out.print("</head>");
  33. out.print("<body>");
  34. out.print("<h1>部门详情</h1>");
  35. out.print("<hr>");
  36. //获取部门编号
  37. //虽然提交的是30,但服务器获取的是30这个字符串
  38. String deptno = request.getParameter("deptno");
  39. /*
  40. * 连接数据库
  41. */
  42. Connection conn = null;
  43. PreparedStatement ps = null;
  44. ResultSet rs = null;
  45. try {
  46. //获取连接
  47. conn = DBUtil.getConnection();
  48. //获取预编译的数据库操作对象
  49. String sql = "select deptno,dname,loc from dept where deptno=?";
  50. ps = conn.prepareStatement(sql);
  51. ps.setString(1, deptno);
  52. //这个结果集一定只有一个元素
  53. //执行SQL语句
  54. rs = ps.executeQuery();
  55. //处理结果集
  56. if (rs.next()) {
  57. String dname = rs.getString("dname");
  58. String loc = rs.getString("loc");
  59. out.print("部门编号" + deptno + "<br>");
  60. out.print("部门名称" + dname + "<br>");
  61. out.print("部门位置" + loc + "<br>");
  62. }
  63. } catch (SQLException e) {
  64. e.printStackTrace();
  65. } finally {
  66. //释放资源
  67. DBUtil.close(conn, ps, rs);
  68. }
  69. out.print("");
  70. out.print("<form action='list.html'>");
  71. out.print(" <input type='button' value='后退' onclick='window.history.back()'>");
  72. out.print("");
  73. out.print("</form>");
  74. out.print("");
  75. out.print("</body>");
  76. out.print("</html>");
  77. }
  78. }
  1. package oa.action;
  2. import com.sun.net.httpserver.HttpPrincipal;
  3. import jakarta.servlet.ServletException;
  4. import jakarta.servlet.http.HttpServlet;
  5. import jakarta.servlet.http.HttpServletRequest;
  6. import jakarta.servlet.http.HttpServletResponse;
  7. import jdk.internal.org.objectweb.asm.TypeReference;
  8. import oa.untils.DBUtil;
  9. import java.io.IOException;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. /**
  15. * @Author: 小雷学长
  16. * @Date: 2022/3/22 - 18:08
  17. * @Version: 1.8
  18. */
  19. public class DeptDelServlet extends HttpServlet {
  20. @Override
  21. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  22. throws ServletException, IOException {
  23. //根据部门编号,删除部门
  24. //获取部门编号
  25. String deptno = request.getParameter("deptno");
  26. //连接数据库删除数据
  27. Connection conn = null;
  28. PreparedStatement ps = null;
  29. int count = 0;
  30. try {
  31. conn = DBUtil.getConnection();
  32. //开启事务(自动提交机制关闭)(非必要)
  33. // conn.setAutoCommit(false);
  34. String sql = "delete from dept where deptno=?";
  35. ps = conn.prepareStatement(sql);
  36. ps.setString(1, deptno);
  37. //返回值是:影响了数据库表当中多少条记录
  38. count = ps.executeUpdate();
  39. //事务提交(非必要)
  40. // conn.commit();
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. } finally {
  44. DBUtil.close(conn, ps, null);
  45. }
  46. //判断删除成功还是失败
  47. if (count == 1) {
  48. //删除成功
  49. //仍然跳回到部门列表页面
  50. //部门列表页面需要执行另一个Servlet,利用转发机制
  51. request.getRequestDispatcher("/dept/list").forward(request, response);
  52. } else {
  53. //删除失败
  54. request.getRequestDispatcher("/error.html").forward(request, response);
  55. }
  56. }
  57. }
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>error</title>
  6. </head>
  7. <body>
  8. <h1>操作失败<a href="javascript:void(0)" onclick="window.history.back()">返回</a></h1>
  9. </body>
  10. </html>
  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:3306/study
  3. user=root
  4. password=123456
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!--查看部门列表-->
  7. <servlet>
  8. <servlet-name>list</servlet-name>
  9. <servlet-class>oa.action.DeptListServlet</servlet-class>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>list</servlet-name>
  13. <url-pattern>/dept/list</url-pattern>
  14. </servlet-mapping>
  15. <!--查看详情-->
  16. <servlet>
  17. <servlet-name>detail</servlet-name>
  18. <servlet-class>oa.action.DeptDetailServelt</servlet-class>
  19. </servlet>
  20. <servlet-mapping>
  21. <servlet-name>detail</servlet-name>
  22. <url-pattern>/dept/detail</url-pattern>
  23. </servlet-mapping>
  24. <!--删除-->
  25. <servlet>
  26. <servlet-name>delete</servlet-name>
  27. <servlet-class>oa.action.DeptDelServlet</servlet-class>
  28. </servlet>
  29. <servlet-mapping>
  30. <servlet-name>delete</servlet-name>
  31. <url-pattern>/dept/delete</url-pattern>
  32. </servlet-mapping>
  33. </web-app>

OA.7z


1.8 部门新增功能

  1. package oa.action;
  2. import com.sun.corba.se.spi.orb.PropertyParser;
  3. import jakarta.servlet.ServletException;
  4. import jakarta.servlet.http.HttpServlet;
  5. import jakarta.servlet.http.HttpServletRequest;
  6. import jakarta.servlet.http.HttpServletResponse;
  7. import oa.untils.DBUtil;
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.util.Queue;
  15. /**
  16. * @Author: 小雷学长
  17. * @Date: 2022/3/22 - 10:05
  18. * @Version: 1.8
  19. */
  20. public class DeptListServlet extends HttpServlet {
  21. //处理Post请求
  22. @Override
  23. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  24. throws ServletException, IOException {
  25. doGet(request, response);
  26. }
  27. @Override
  28. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  29. throws ServletException, IOException {
  30. //获取应用的根路径
  31. String contextPath = request.getContextPath();
  32. //设置响应的内容类型
  33. response.setContentType("text/html");
  34. PrintWriter out = response.getWriter();
  35. out.print(" <!DOCTYPE html>");
  36. out.print("<html lang='en'>");
  37. out.print("<head>");
  38. out.print(" <meta charset='UTF-8'>");
  39. out.print(" <title>部门列表</title>");
  40. out.print("</head>");
  41. out.print("<body>");
  42. out.print("<script type='text/javascript'>");
  43. out.print(" function del(dno) {");
  44. out.print(" if(window.confirm('亲,删了不可恢复哦!')){");
  45. out.print(" document.location.href = '" + contextPath + "/dept/delete?deptno=' + dno");
  46. out.print(" }");
  47. out.print("}");
  48. out.print("</script>");
  49. out.print("<h1 align='center'>部门列表</h1>");
  50. out.print("<hr>");
  51. out.print("<table border='1px' align='center' width='50%'>");
  52. out.print(" <tr>");
  53. out.print(" <th>序号</th>");
  54. out.print(" <th>部门编号</th>");
  55. out.print(" <th>部门名称</th>");
  56. out.print(" <th>操作</th>");
  57. out.print(" </tr>");
  58. out.print(" <!--以上是固定的-->");
  59. /*
  60. 上面一部分是死的
  61. */
  62. /*
  63. * 连接数据库
  64. */
  65. Connection conn = null;
  66. PreparedStatement ps = null;
  67. ResultSet rs = null;
  68. try {
  69. //获取连接
  70. conn = DBUtil.getConnection();
  71. //获取预编译的数据库操作对象
  72. String sql = "select deptno,dname ,loc from dept";
  73. ps = conn.prepareStatement(sql);
  74. //执行SQL语句
  75. rs = ps.executeQuery();
  76. //处理结果集
  77. int i = 0;
  78. while (rs.next()) {
  79. String deptno = rs.getString("deptno");
  80. String dname = rs.getString("dname");
  81. String loc = rs.getString("loc");
  82. out.print(" <tr>");
  83. out.print(" <td>" + (++i) + "</td>");
  84. out.print(" <td>" + deptno + "</td>");
  85. out.print(" <td>" + dname + "</td>");
  86. out.print(" <td>");
  87. out.print("<a href='javascript:void(0)' onclick='del(" + deptno + ")'>删除</a>");
  88. out.print(" <a href='edit.html'>修改</a>");
  89. out.print(" <a href='" + contextPath + "/dept/detail?deptno=" + deptno + "'>详情</a>");
  90. out.print(" </td>");
  91. out.print(" </tr>");
  92. }
  93. } catch (SQLException e) {
  94. e.printStackTrace();
  95. } finally {
  96. //释放资源
  97. DBUtil.close(conn, ps, rs);
  98. }
  99. /*
  100. 下面一部分是死的
  101. */
  102. out.print(" <!--一下是固定的-->");
  103. out.print("</table>");
  104. out.print("<hr>");
  105. out.print("<a href='" + contextPath + "/add.html'>新增部门</a>");
  106. out.print("</body>");
  107. out.print("</html>");
  108. }
  109. }
  1. package oa.action;
  2. import jakarta.servlet.ServletException;
  3. import jakarta.servlet.http.HttpServlet;
  4. import jakarta.servlet.http.HttpServletRequest;
  5. import jakarta.servlet.http.HttpServletResponse;
  6. import oa.untils.DBUtil;
  7. import java.io.IOException;
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.SQLException;
  11. /**
  12. * @Author: 小雷学长
  13. * @Date: 2022/3/22 - 20:36
  14. * @Version: 1.8
  15. */
  16. public class DeptSaveServlet extends HttpServlet {
  17. @Override
  18. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  19. throws ServletException, IOException {
  20. //获取部门信息
  21. String deptno = request.getParameter("deptno");
  22. String dname = request.getParameter("dname");
  23. String loc = request.getParameter("loc");
  24. //连接数据库执行insert语句
  25. Connection conn = null;
  26. PreparedStatement ps = null;
  27. int count = 0;
  28. try {
  29. conn = DBUtil.getConnection();
  30. String sql = "insert into dept(deptno,dname,loc) values(?,?,?)";
  31. ps = conn.prepareStatement(sql);
  32. ps.setString(1, deptno);
  33. ps.setString(2, dname);
  34. ps.setString(3, loc);
  35. count = ps.executeUpdate();
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. } finally {
  39. DBUtil.close(conn, ps, null);
  40. }
  41. //判断新增成功还是失败
  42. if (count == 1) {
  43. //新增成功
  44. //仍然跳回到部门列表页面
  45. //部门列表页面需要执行另一个Servlet,利用转发机制
  46. //转发一次请求
  47. request.getRequestDispatcher("/dept/list").forward(request, response);
  48. } else {
  49. //新增失败
  50. request.getRequestDispatcher("/error.html").forward(request, response);
  51. }
  52. }
  53. }
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>新增部门</title>
  6. </head>
  7. <body>
  8. <h1>新增部门</h1>
  9. <hr>
  10. <form action="/oa/dept/save" method="post">
  11. 部门编号<input type="text" name="deptno"><br>
  12. 部门名称<input type="text" name="dname"><br>
  13. 部门位置<input type="text" name="loc"><br>
  14. <input type="submit" value="保存"><br>
  15. </form>
  16. </body>
  17. </html>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!--查看部门列表-->
  7. <servlet>
  8. <servlet-name>list</servlet-name>
  9. <servlet-class>oa.action.DeptListServlet</servlet-class>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>list</servlet-name>
  13. <url-pattern>/dept/list</url-pattern>
  14. </servlet-mapping>
  15. <!--查看详情-->
  16. <servlet>
  17. <servlet-name>detail</servlet-name>
  18. <servlet-class>oa.action.DeptDetailServelt</servlet-class>
  19. </servlet>
  20. <servlet-mapping>
  21. <servlet-name>detail</servlet-name>
  22. <url-pattern>/dept/detail</url-pattern>
  23. </servlet-mapping>
  24. <!--删除-->
  25. <servlet>
  26. <servlet-name>delete</servlet-name>
  27. <servlet-class>oa.action.DeptDelServlet</servlet-class>
  28. </servlet>
  29. <servlet-mapping>
  30. <servlet-name>delete</servlet-name>
  31. <url-pattern>/dept/delete</url-pattern>
  32. </servlet-mapping>
  33. <!--保存-->
  34. <servlet>
  35. <servlet-name>save</servlet-name>
  36. <servlet-class>oa.action.DeptSaveServlet</servlet-class>
  37. </servlet>
  38. <servlet-mapping>
  39. <servlet-name>save</servlet-name>
  40. <url-pattern>/dept/save</url-pattern>
  41. </servlet-mapping>
  42. </web-app>

1.9 实现部门的修改

  1. package oa.action;
  2. import jakarta.servlet.ServletException;
  3. import jakarta.servlet.http.HttpServlet;
  4. import jakarta.servlet.http.HttpServletRequest;
  5. import jakarta.servlet.http.HttpServletResponse;
  6. import oa.untils.DBUtil;
  7. import java.awt.print.Printable;
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. /**
  15. * @Author: 小雷学长
  16. * @Date: 2022/3/23 - 11:19
  17. * @Version: 1.8
  18. */
  19. public class DeptUpdateServlet extends HttpServlet {
  20. @Override
  21. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  22. throws ServletException, IOException {
  23. response.setContentType("text/html");
  24. PrintWriter out = response.getWriter();
  25. String deptno = request.getParameter("deptno");
  26. String dname = request.getParameter("dname");
  27. String loc = request.getParameter("loc");
  28. //连接数据库执行insert语句
  29. Connection conn = null;
  30. PreparedStatement ps = null;
  31. ResultSet rs = null;
  32. int count = 0;
  33. try {
  34. conn = DBUtil.getConnection();
  35. String sql = "update dept set dname = ?,loc = ? where deptno = ?";
  36. ps = conn.prepareStatement(sql);
  37. ps.setString(1, dname);
  38. ps.setString(2, loc);
  39. ps.setString(3, deptno);
  40. count = ps.executeUpdate();
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. } finally {
  44. DBUtil.close(conn, ps, rs);
  45. }
  46. if (count == 1){
  47. //更新成功
  48. //跳转到部门列表页面,转发机制
  49. request.getRequestDispatcher("/dept/list").forward(request,response);
  50. }
  51. else {
  52. //更新失败
  53. request.getRequestDispatcher("/error.html").forward(request,response);
  54. }
  55. }
  56. }
  1. package oa.action;
  2. import com.sun.net.httpserver.HttpPrincipal;
  3. import jakarta.servlet.ServletException;
  4. import jakarta.servlet.http.HttpServlet;
  5. import jakarta.servlet.http.HttpServletRequest;
  6. import jakarta.servlet.http.HttpServletResponse;
  7. import oa.untils.DBUtil;
  8. import javax.xml.transform.Result;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. import java.sql.Connection;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. /**
  16. * @Author: 小雷学长
  17. * @Date: 2022/3/22 - 22:59
  18. * @Version: 1.8
  19. */
  20. public class DeptEditServlet extends HttpServlet {
  21. @Override
  22. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  23. throws ServletException, IOException {
  24. response.setContentType("text/html");
  25. PrintWriter out = response.getWriter();
  26. String contextPath = request.getContextPath();
  27. out.print("<html lang='en'>");
  28. out.print("<head>");
  29. out.print(" <meta charset='UTF-8'>");
  30. out.print(" <title>修改部门</title>");
  31. out.print("</head>");
  32. out.print("<body>");
  33. out.print("<h1>修改部门</h1>");
  34. out.print("<hr>");
  35. out.print("<form action='" + contextPath + "/dept/update' method='post'>");
  36. //获取部门编号
  37. String deptno = request.getParameter("deptno");
  38. //连接数据库执行insert语句
  39. Connection conn = null;
  40. PreparedStatement ps = null;
  41. ResultSet rs = null;
  42. try {
  43. conn = DBUtil.getConnection();
  44. String sql = "select dname,loc from dept where deptno=?";
  45. ps = conn.prepareStatement(sql);
  46. ps.setString(1, deptno);
  47. rs = ps.executeQuery();
  48. if (rs.next()) {
  49. String dname = rs.getString("dname");
  50. String loc = rs.getString("loc");
  51. //输出动态网页
  52. out.print(" 部门编号<input type='text' name='deptno' value='" + deptno + "' readonly><br><!--readonly只读-->");
  53. out.print(" 部门名称<input type='text' name='dname' value='" + dname + "'><br>");
  54. out.print(" 部门位置<input type='text' name='loc' value='" + loc + "'><br>");
  55. }
  56. } catch (SQLException e) {
  57. e.printStackTrace();
  58. } finally {
  59. DBUtil.close(conn, ps, rs);
  60. }
  61. //判断新增成功还是失败
  62. out.print(" <input type='submit' value='修改'><br>");
  63. out.print("");
  64. out.print("</form>");
  65. out.print("");
  66. out.print("</body>");
  67. out.print("</html>");
  68. }
  69. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!--查看部门列表-->
  7. <servlet>
  8. <servlet-name>list</servlet-name>
  9. <servlet-class>oa.action.DeptListServlet</servlet-class>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>list</servlet-name>
  13. <url-pattern>/dept/list</url-pattern>
  14. </servlet-mapping>
  15. <!--查看详情-->
  16. <servlet>
  17. <servlet-name>detail</servlet-name>
  18. <servlet-class>oa.action.DeptDetailServelt</servlet-class>
  19. </servlet>
  20. <servlet-mapping>
  21. <servlet-name>detail</servlet-name>
  22. <url-pattern>/dept/detail</url-pattern>
  23. </servlet-mapping>
  24. <!--删除-->
  25. <servlet>
  26. <servlet-name>delete</servlet-name>
  27. <servlet-class>oa.action.DeptDelServlet</servlet-class>
  28. </servlet>
  29. <servlet-mapping>
  30. <servlet-name>delete</servlet-name>
  31. <url-pattern>/dept/delete</url-pattern>
  32. </servlet-mapping>
  33. <!--保存-->
  34. <servlet>
  35. <servlet-name>save</servlet-name>
  36. <servlet-class>oa.action.DeptSaveServlet</servlet-class>
  37. </servlet>
  38. <servlet-mapping>
  39. <servlet-name>save</servlet-name>
  40. <url-pattern>/dept/save</url-pattern>
  41. </servlet-mapping>
  42. <!--修改页面-->
  43. <servlet>
  44. <servlet-name>edit</servlet-name>
  45. <servlet-class>oa.action.DeptEditServlet</servlet-class>
  46. </servlet>
  47. <servlet-mapping>
  48. <servlet-name>edit</servlet-name>
  49. <url-pattern>/dept/edit</url-pattern>
  50. </servlet-mapping>
  51. <!--修改部门-->
  52. <servlet>
  53. <servlet-name>update</servlet-name>
  54. <servlet-class>oa.action.DeptUpdateServlet</servlet-class>
  55. </servlet>
  56. <servlet-mapping>
  57. <servlet-name>update</servlet-name>
  58. <url-pattern>/dept/update</url-pattern>
  59. </servlet-mapping>
  60. </web-app>