为了检验JavaWeb、Servlet、MySQL、JDBC的学习效果,实现了简易版的部门信息管理系统。没有使用任何框架,这样便于深入理解原理。
目录
2. 设计前端页面(5个,简单,文本编辑器即可,后期替换成java代码)
后端功能实现类6个:DeptDeleteServlet.java
前端->后端。即先设计前端页面及功能,然后后端连接MySQL实现功能。凡是与数据库产生交互的,成为功能。
查看部门列表
新增部门
删除部门
查看部门详细信息
跳转到修改页面
修改部门
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>欢迎使用OA系统title>
- head>
- <body>
- <a href="list.html">查看部门列表a>
- body>
- html>
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>部门列表页面title>
- head>
- <body>
-
- <script type="text/javascript">
- function del(dno){
- // 弹出确认框,用户点击确定,返回true,点击取消返回false
- var ok = window.confirm("亲,删了不可恢复哦!");
- if(ok){
- // 发送请求进行删除数据的操作。
- // 在JS代码当中如何发送请求给服务器?
- //alert("正在删除数据,请稍后...")
-
- //document.location.href = "请求路径"
- //document.location = "请求路径"
- //window.location.href = "请求路径"
- //window.location = "请求路径"
- document.location.href = "/oa/dept/delete?deptno=" + dno;
- }
- }
- script>
-
- <h1 align="center">部门列表h1>
- <hr >
- <table border="1px" align="center" width="50%">
- <tr>
- <th>序号th>
- <th>部门编号th>
- <th>部门名称th>
- <th>操作th>
- tr>
- <tr>
- <td>1td>
- <td>10td>
- <td>销售部td>
- <td>
- <a href="javascript:void(0)" onclick="del(10)">删除a>
- <a href="edit.html">修改a>
- <a href="detail.html">详情a>
- td>
- tr>
- <tr>
- <td>2td>
- <td>20td>
- <td>研发部td>
- <td>
-
-
-
- <a href="javascript:void(0)" onclick="del(20)" >删除a>
- <a href="edit.html">修改a>
- <a href="detail.html">详情a>
- td>
- tr>
- <tr>
- <td>3td>
- <td>30td>
- <td>运营部td>
- <td>
- <a href="javascript:void(0)" onclick="del(30)" >删除a>
- <a href="edit.html">修改a>
- <a href="detail.html">详情a>
- td>
- tr>
- table>
-
- <hr >
- <a href="add.html">新增部门a>
-
- body>
- html>
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>部门详情title>
- head>
- <body>
- <h1>部门详情h1>
- <hr >
- 部门编号:20 <br>
- 部门名称:销售部<br>
- 部门位置:北京<br>
-
- <input type="button" value="后退" onclick="window.history.back()"/>
- body>
- html>
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>修改部门title>
- head>
- <body>
- <h1>修改部门h1>
- <hr >
- <form action="list.html" method="get">
- 部门编号<input type="text" name="deptno" value="20" readonly /><br>
- 部门名称<input type="text" name="dname" value="销售部"/><br>
- 部门位置<input type="text" name="loc" value="北京"/><br>
- <input type="submit" value="修改"/><br>
- form>
- body>
- html>
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>新增部门title>
- head>
- <body>
- <h1>新增部门h1>
- <hr >
- <form action="list.html" method="get">
- 部门编号<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>
见这篇文章
(437条消息) 基于IDEA的JAVAWeb-Servlet开发,实现网页对MySQL进行简单的CRUD_天亮有惊喜的博客-CSDN博客
开发环境搭建完成后,需要把刚刚写的5个html文件复制在web文件夹内(与WEB-INF文件夹平级) ,并且另外开发DBUtil工具类(减少JDBC代码,每次使用同一个连接)
最终的目录结构如上图所示。
见下文《二、开发步骤》
在src包中新建文件夹resources,其中新增配置文件jdbc.properties(遵守java的开闭原则)
- driver=com.mysql.cj.jdbc.Driver
- url=jdbc:mysql://localhost:3306/bjpowernode
- user=root
- password=123456
新建包com.bjpowernode.oa->utils.DBUtil.java,系统的CRUD仅使用一次jdbc连接,减少系统资源浪费。同时抽象方法(如JDBC资源关闭方法),提高代码复用性。
- package com.bjpowernode.oa.utils;
-
- import java.sql.*;
- import java.util.ResourceBundle;
-
- /**
- * JDBC工具类
- */
- public class DBUtil {
- //静态变量:在类加载时执行,并且是有顺序的,自上而下
- private static ResourceBundle bundle = ResourceBundle.getBundle("resources.jdbc");
- 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.cj.jdbc.Driver是连接数据库的驱动,不能写死,因为以后可能还要连接Oracle数据库
- //如果连接Oracle数据库,还需要修改java代码,显然违背了OCP开闭原则
- //OCP开闭原则:对拓展开放,对修改关闭(功能拓展,不修改java源代码)
- //Class.forName("com.mysql.cj.jdbc.Driver");
- Class.forName(driver);
- } catch (ClassNotFoundException e) {
- throw new RuntimeException(e);
- }
- }
-
- /**
- * 获取数据库连接对象
- * @return conn 连接对象
- * @throws SQLException
- */
- public static Connection getConnection() throws SQLException {
- //获取连接
- Connection conn = DriverManager.getConnection(url, user, password);
- return conn;
- }
-
- //注册驱动
- //获取连接
- //获取数据库操作对象
- //执行SQL语句
- //处理查询结果集
-
- /**
- *释放资源
- * @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();
- }
- }
- }
- }
保存以下代码脚本 depttest.sql,运行即生成数据库表bjpowernode.depttest
- # 部门表
- drop table if exists depttest;
- create table depttest(
- deptno int primary key,
- dname varchar(255),
- loc varchar(255)
- );
- insert into depttest(deptno, dname, loc) values(10, 'XiaoShouBu', 'BEIJING');
- insert into depttest(deptno, dname, loc) values(20, 'YanFaBu', 'SHANGHAI');
- insert into depttest(deptno, dname, loc) values(30, 'JiShuBu', 'GUANGZHOU');
- insert into depttest(deptno, dname, loc) values(40, 'MeiTiBu', 'SHENZHEN');
- commit;
- select * from depttest;
测试该5个页面,连接正常,整体符合预期(写好跳转即可)


web.xml配置修改如下
- <servlet>
- <servlet-name>listservlet-name>
- <servlet-class>com.bjpowernode.oa.web.action.DeptListServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>listservlet-name>
-
- <url-pattern>/dept/listurl-pattern>
- servlet-mapping>
查看部门列表功能实现类com.bjpowernode.oa.web.action.DeptListServlet.java如下:
- package com.bjpowernode.oa.web.action;
-
- import com.bjpowernode.oa.utils.DBUtil;
- import jakarta.servlet.ServletException;
- import jakarta.servlet.http.HttpServlet;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public class DeptListServlet extends HttpServlet {
- @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();
-
- //tomcat10不用写,设置响应的内容类型以及字符集,防止中文乱码
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
-
- //这部分是静态不变的
- out.print(" ");
- out.print("");
- out.print(" ");
- out.print(" ");
- out.print("
部门列表页面 "); - out.print(" ");
- out.print(" ");
- out.print("
部门列表
"); - out.print("
"); - out.print("
");- out.print("
");- out.print("
序号 "); - out.print("
部门编号 "); - out.print("
部门名称 "); - out.print("
部门位置 "); - out.print("
操作 "); - out.print("
"); -
-
- //连接数据库,查询所有部门
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- //获取连接
- conn = DBUtil.getConnection();
- //获取预编译的数据库操作对象
- String sql = "select deptno, dname, loc from depttest";
- ps = conn.prepareStatement(sql);
- //执行sql语句
- rs = ps.executeQuery();
- //处理结果集
- int i = 0;
- String deptno = null;
- String dname = null;
- String loc = null;
- while (rs.next()) {
- deptno = rs.getString("deptno");
- dname = rs.getString("dname");
- loc = rs.getString("loc");
- //这部分是动态的
- }
- //这部分是静态不变的
- out.print("
"); - out.print("
"); - out.print(" 新增部门");
- out.print(" ");
- out.print("");
-
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- //释放资源
- DBUtil.close(conn, ps, rs);
- }
- }
- }
以此类推,实现全部功能:展示部门列表、查看部门详细信息、删除部门、新增部门信息、新增部门。完整目录结构如下所示:
完整代码在下文《四、完整代码》给出








项目文件夹存放路径:.
├─.idea
│ └─artifacts
├─oa
│ ├─src
│ │ ├─com.bjpowernode.oa
│ │ │ └─utils
│ │ │ └─DUBtil.java
│ │ │ └─web.action
│ │ │ └─DeptDeleteServlet.java
│ │ │ └─DeptDetailServlet.java
│ │ │ └─DeptEditServlet.java
│ │ │ └─DeptListServlet.java
│ │ │ └─DeptSavaServlet.java
│ │ │ └─DeptUpdateServlet.java
│ │ └─resources
│ │ │ └─jdbc.properties
│ └─web
│ │ └─WEB-INF
│ │ │ └─lib
│ │ │ └─mysql-connector-java-8.0.28.jar
│ │ │ └─web.xml
│ │ └─index.html
│ │ └─add.html
│ └─oa.iml
External Libraries
│─jsp.api.jar
│─servlet-api.kar
│─jdk1.8.0_111
- "1.0" encoding="UTF-8"?>
- <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
- version="5.0">
-
- <servlet>
- <servlet-name>listservlet-name>
- <servlet-class>com.bjpowernode.oa.web.action.DeptListServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>listservlet-name>
-
- <url-pattern>/dept/listurl-pattern>
- servlet-mapping>
-
- <servlet>
- <servlet-name>detailservlet-name>
- <servlet-class>com.bjpowernode.oa.web.action.DeptDetailServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>detailservlet-name>
- <url-pattern>/dept/detailurl-pattern>
- servlet-mapping>
-
- <servlet>
- <servlet-name>deleteservlet-name>
- <servlet-class>com.bjpowernode.oa.web.action.DeptDeleteServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>deleteservlet-name>
- <url-pattern>/dept/deleteurl-pattern>
- servlet-mapping>
-
-
- <servlet>
- <servlet-name>saveservlet-name>
- <servlet-class>com.bjpowernode.oa.web.action.DeptSaveServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>saveservlet-name>
- <url-pattern>/dept/saveurl-pattern>
- servlet-mapping>
-
-
- <servlet>
- <servlet-name>editservlet-name>
- <servlet-class>com.bjpowernode.oa.web.action.DeptEditServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>editservlet-name>
- <url-pattern>/dept/editurl-pattern>
- servlet-mapping>
-
-
- <servlet>
- <servlet-name>updateservlet-name>
- <servlet-class>com.bjpowernode.oa.web.action.DeptUpdateServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>updateservlet-name>
- <url-pattern>/dept/updateurl-pattern>
- servlet-mapping>
- web-app>
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>欢迎使用OA系统title>
- head>
-
- <body>
- <a href="/oa/dept/list">查看部门列表a>
- body>
- html>
- html>
- <html>
- <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>
- package com.bjpowernode.oa.web.action;
-
- import com.bjpowernode.oa.utils.DBUtil;
- import jakarta.servlet.ServletException;
- import jakarta.servlet.http.HttpServlet;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public class DeptDeleteServlet extends HttpServlet {
- /**
- * 根据部门编号展示部门信息,已经在DeptListServlet中传入了deptno
- * @param request
- * @param response
- * @throws ServletException
- * @throws IOException
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 获取部门编号
- // /oa/dept/detail
- String deptno = request.getParameter("deptno");
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- //连接数据库,查询所有部门
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- //获取连接
- conn = DBUtil.getConnection();
- //获取预编译的数据库操作对象
- String sql = "delete from depttest where deptno = ?";
- ps = conn.prepareStatement(sql);
- ps.setString(1, deptno);
- //执行sql语句
- int flag = ps.executeUpdate();
- if(flag == 1){
- //删除成功
- request.getRequestDispatcher("/dept/list").forward(request, response);
- }
- else {
- //删除失败
- request.getRequestDispatcher("/error.html").forward(request, response);
- };
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- //释放资源
- DBUtil.close(conn, ps, rs);
- }
- }
- }
- package com.bjpowernode.oa.web.action;
-
- import com.bjpowernode.oa.utils.DBUtil;
- import jakarta.servlet.ServletException;
- import jakarta.servlet.http.HttpServlet;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public class DeptDetailServlet extends HttpServlet {
- /**
- * 根据部门编号展示部门信息,已经在DeptListServlet中传入了deptno
- * @param request
- * @param response
- * @throws ServletException
- * @throws IOException
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 获取部门编号
- // /oa/dept/detail
- String deptno = request.getParameter("deptno");
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
-
- //静态页面
- out.print("");
- out.print("");
- out.print(" ");
- out.print(" ");
- out.print("
部门详情 "); - out.print(" ");
- out.print(" ");
- out.print("
部门详情
"); - out.print("
"); -
- //连接数据库,查询所有部门
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- //获取连接
- conn = DBUtil.getConnection();
- //获取预编译的数据库操作对象
- String sql = "select dname, loc from depttest where deptno = ?";
- ps = conn.prepareStatement(sql);
- ps.setString(1, deptno);
- //执行sql语句
- rs = ps.executeQuery();
- //处理结果集
- int i = 0;
- while(rs.next()) {
- String dname = rs.getString("dname");
- String loc = rs.getString("loc");
- //动态
- out.print(" 部门编号:"+deptno+"
"); - out.print(" 部门名称:"+dname+"
"); - out.print(" 部门位置:"+loc+"
"); - }
- //静态
- out.print(" ");
- out.print(" ");
- out.print("");
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- //释放资源
- DBUtil.close(conn, ps, rs);
- }
- }
- }
- package com.bjpowernode.oa.web.action;
-
- import com.bjpowernode.oa.utils.DBUtil;
- import jakarta.servlet.ServletException;
- import jakarta.servlet.http.HttpServlet;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
-
- public class DeptEditServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //初始化,得到当前路径,
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- //获取数据库连接
- Connection conn = null;
- PreparedStatement ps = null;
- String deptno = request.getParameter("deptno");
- String dname = request.getParameter("dname");
- String loc = request.getParameter("loc");
-
- out.print(" ");
- out.print("");
- out.print(" ");
- out.print(" ");
- out.print("
修改部门 "); - out.print(" ");
- out.print(" ");
- out.print("
修改部门
"); - out.print("
"); - out.print(" );
- out.print(" 部门编号" readonly />
"); - out.print(" 部门名称">
"); - out.print(" 部门位置">
"); - out.print("
"); - out.print(" ");
- out.print(" ");
- out.print("");
- }
- }
- package com.bjpowernode.oa.web.action;
-
- import com.bjpowernode.oa.utils.DBUtil;
- import jakarta.servlet.ServletException;
- import jakarta.servlet.http.HttpServlet;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public class DeptListServlet extends HttpServlet {
- @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();
-
- //tomcat10不用写,设置响应的内容类型以及字符集,防止中文乱码
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
-
- //这部分是静态不变的
- out.print(" ");
- out.print("");
- out.print(" ");
- out.print(" ");
- out.print("
部门列表页面 "); - out.print(" ");
- out.print(" ");
- out.print("
部门列表
"); - out.print("
"); - out.print("
");- out.print("
");- out.print("
序号 "); - out.print("
部门编号 "); - out.print("
部门名称 "); - out.print("
部门位置 "); - out.print("
操作 "); - out.print("
"); -
-
- //连接数据库,查询所有部门
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- //获取连接
- conn = DBUtil.getConnection();
- //获取预编译的数据库操作对象
- String sql = "select deptno, dname, loc from depttest";
- ps = conn.prepareStatement(sql);
- //执行sql语句
- rs = ps.executeQuery();
- //处理结果集
- int i = 0;
- String deptno = null;
- String dname = null;
- String loc = null;
- while (rs.next()) {
- deptno = rs.getString("deptno");
- dname = rs.getString("dname");
- loc = rs.getString("loc");
- //这部分是动态的
- }
- //这部分是静态不变的
- out.print("
"); - out.print("
"); - out.print(" 新增部门");
- out.print(" ");
- out.print("");
-
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- //释放资源
- DBUtil.close(conn, ps, rs);
- }
- }
- }
- package com.bjpowernode.oa.web.action;
-
- import com.bjpowernode.oa.utils.DBUtil;
- import jakarta.servlet.ServletException;
- import jakarta.servlet.http.HttpServlet;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
-
- public class DeptSaveServlet extends HttpServlet {
- /**
- * 保存新增部门记录
- * @param request
- * @param response
- * @throws ServletException
- * @throws IOException
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 获取部门编号
- // /oa/dept/detail
- String deptno = request.getParameter("deptno");
- String dname = request.getParameter("dname");
- String loc = request.getParameter("loc");
- response.setContentType("text/html;charset=UTF-8");
-
- //连接数据库,查询所有部门
- Connection conn = null;
- PreparedStatement ps = null;
- try {
- //获取连接
- conn = DBUtil.getConnection();
- //获取预编译的数据库操作对象
- String sql = "insert into depttest values(?, ?, ?)";
- ps = conn.prepareStatement(sql);
- ps.setString(1, deptno);
- ps.setString(2, dname);
- ps.setString(3, loc);
-
- // System.out.println(ps);
- //执行sql语句
- int flag = ps.executeUpdate();
- // System.out.println("flag="+flag);
- if(flag == 1){
- //执行成功
- request.getRequestDispatcher("/dept/list").forward(request, response);
- // request.getRequestDispatcher("/error.html").forward(request, response);
- }else {
- //执行成功
- request.getRequestDispatcher("/error.html").forward(request, response);
- }
-
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- //释放资源
- DBUtil.close(conn, ps, null);
- }
- }
- }
- package com.bjpowernode.oa.web.action;
-
- import com.bjpowernode.oa.utils.DBUtil;
- import jakarta.servlet.ServletException;
- import jakarta.servlet.http.HttpServlet;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
-
- public class DeptUpdateServlet extends HttpServlet {
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //初始化,得到当前路径,
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- //获取数据库连接
- Connection conn = null;
- PreparedStatement ps = null;
- String deptno = request.getParameter("deptno");
- String dname = request.getParameter("dname");
- String loc = request.getParameter("loc");
- try {
- conn = DBUtil.getConnection();
- //执行sql语句
- String sql = "update depttest set dname = ?, loc = ? where deptno = ?";
- //获取预编译的sql语句
- ps = conn.prepareStatement(sql);
- ps.setString(1, dname);
- ps.setString(2, loc);
- ps.setString(3, deptno);
- //执行sql语句
- int flag = ps.executeUpdate();
-
- if(flag == 1){
- //执行成功
- request.getRequestDispatcher("/dept/list").forward(request, response);
- } else {
- //执行失败
- request.getRequestDispatcher("/error.html").forward(request, response);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- DBUtil.close(conn, ps, null);
- }
- }
- }
PS:虽然已经写得很详细了,可以直接1:1复现。但是如果有想要原项目工程代码的话,在评论区留下邮箱号即可,看到之后会回复哦~