本次技术只由纯Serlet连接连接数据库进行用户的增删改查
首先创建一个DBUtil (JDBC的工具类,因为此次小项目连接数据库次数比较多)
import java.sql.*;
import java.util.ResourceBundle;
/**
* JDBC的工具类
*/
public class DBUtil {
//使用静态变量:类加载时执行
//并且是有顺序的,自上而下的顺序
private static ResourceBundle bundle = ResourceBundle.getBundle("resources.jdbc1");
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");
//注册驱动
//调用一次JDBC工具类执行一次注册驱动即可,所以直接写到静态代码块中【类加载时执行】
static {
try {
//如果"com.mysql.jdbc.Driver"是连接数据库驱动,不能写死,因为以后可能还会连接Oracle数据库。
//如果连接Oracle数据库的时候,还需要修改代码,则违背OCP开闭原则
//OCP开闭原则:对扩展开放,对修改关闭
//Class.forName("com.mysql.jdbc.Driver");
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接
//返回值为Connection,且为静态方法,工具类最好都用静态方法
public static Connection getConnection() throws SQLException {
Connection conn = DriverManager.getConnection(url,user,password);
return conn;
}
//获取数据库操作对象和执行sql语句以及处理结果集都是一行代码,不需要封装
//释放资源
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();
}
}
}
}
其次创建几个静态页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>欢迎使用OA系统</title>
</head>
<body>
<!--这里需要带项目名-->
<a href="/oa/dept/list">查看部门列表</a>
</body>
</html>
<!DOCTYPE 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>
<!DOCTYPE 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>
<!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>
然后将连接数据库需要的url、username、password等配置到properties文件中
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bjpowernode
user=root
password=root
最后用几个类实现功能
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.*;
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();
//输出到浏览器
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//静态页面
out.print("<!DOCTYPE html>");
out.print("<html>");
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>");
//连接数据库
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()){
i++;
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='"+contextPath+"/dept/delete?deptno="+deptno+"'>删除</a>");
out.print("<a href='"+contextPath+"/dept/modify?deptno="+deptno+"'>修改</a>");
out.print("<a href='"+contextPath+"/dept/detail?abc="+deptno+"'>详情</a>");
out.print("</td>");
out.print("</tr>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn,ps,rs);
}
//静态界面
out.print("</table>");
out.print("<hr >");
out.print("<a href='"+contextPath+"/add.html'>新增部门</a>");
out.print("</body>");
out.print("</html>");
}
}
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.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DeptSaveServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取部门信息
//注意乱码问题
request.setCharacterEncoding("UTF-8");
String deptno = request.getParameter("deptno");
String dname = request.getParameter("dname");
String loc = request.getParameter("loc");
//连接数据库
Connection conn =null;
PreparedStatement ps = null;
ResultSet rs = 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,rs);
}
if(count == 1){
//增加成功
request.getRequestDispatcher("/dept/list").forward(request,response);
}
else{
request.getRequestDispatcher("/error.html").forward(request,response);
}
}
}
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 DeptModifyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取根路径
String contextPath = request.getContextPath();
//获取部门信息
String deptno = request.getParameter("deptno");
//设置输出方式
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("<!DOCTYPE html>");
out.print("<html>");
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/realModify' method='post'>");
//连接数据库
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>");
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(" </form>");
out.print(" </body>");
out.print("</html>");
}
}
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.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DeptRealModifyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//防止乱码
request.setCharacterEncoding("UTF-8");
//获取部门信息
String dname = request.getParameter("dname");
String deptno = request.getParameter("deptno");
String loc = request.getParameter("loc");
//连接数据库
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);
}
}
}
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.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DeptDeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//根据部门编号删除部门
//获取部门编号
String deptno = request.getParameter("deptno");
//连接数据库删除部门
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
int count = 0;
try {
conn = DBUtil.getConnection();
//获取数据库操作对象
String sql = "delete from dept where deptno=?";
ps = conn.prepareStatement(sql);
//给?传值
ps.setString(1,deptno);
count = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn,ps,rs);
}
//判断是删除失败了还是删除成功了
if (count == 1){
//删除成功了!
//仍然跳转到部门列表页面
//部门列表页面的显示需要执行另一个Servlet。怎么办?转发
request.getRequestDispatcher("/dept/list").forward(request,response);
}else{
request.getRequestDispatcher("/error.html").forward(request,response);
}
}
}
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 {
@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>");
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 > ");
//获取请求里的deptno
String deptno = request.getParameter("abc");
//连接数据库
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("部门编号:"+deptno+" <br> ");
out.print("部门名称:"+dname+"<br>");
out.print("部门位置:"+loc+"<br>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn,ps,rs);
}
out.print("<input type='button' value='后退' onclick='window.history.back()'/>");
out.print("</body>");
out.print("</html>");
}
}
记得配置web.xml文件
<?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>com.bjpowernode.oa.web.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>com.bjpowernode.oa.web.action.DeptDetailServlet</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>com.bjpowernode.oa.web.action.DeptDeleteServlet</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>com.bjpowernode.oa.web.action.DeptSaveServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>save</servlet-name>
<!--这里不带项目名-->
<url-pattern>/dept/save</url-pattern>
</servlet-mapping>
<!--修改部门页面-->
<servlet>
<servlet-name>modify</servlet-name>
<servlet-class>com.bjpowernode.oa.web.action.DeptModifyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>modify</servlet-name>
<!--这里不带项目名-->
<url-pattern>/dept/modify</url-pattern>
</servlet-mapping>
<!--真正的修改部门操作-->
<servlet>
<servlet-name>realmodify</servlet-name>
<servlet-class>com.bjpowernode.oa.web.action.DeptRealModifyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>realmodify</servlet-name>
<!--这里不带项目名-->
<url-pattern>/dept/realModify</url-pattern>
</servlet-mapping>
</web-app>
|