写点什么

JavaWeb 期末复习

  • 2022 年 5 月 10 日
  • 本文字数:7293 字

    阅读完需:约 24 分钟

//查询所有的员工信息


List<Emp> getAllErp();


//查询详细员工信息


Emp getEmpById(int id);


//新增员工


boolean insertEmp(Emp emp);


//删除员工


boolean delEmp(int id);


//修改员工信息


boolean updateEmp(Emp emp);


}


UserDaoImpl.java


package com.blb.dao.impl;


import com.blb.dao.UserDao;


import com.blb.entity.User;


import com.blb.util.DBUtil;


import java.sql.Connection;


import java.sql.PreparedStatement;


import java.sql.ResultSet;


import java.sql.SQLException;


/**


  • UserDao 的实现类,做数据库表的增删改查


*/


public class UserDaoImpl implements UserDao {


Connection conn = null;


PreparedStatement ps = null;


ResultSet rs =null;


/**


  • 通过用户名从数据库查询用户信息,并返回

  • idea 提示快捷键 alt+enter

  • @param user

  • @return


*/


@Override


public User getUserByUserName(User user) {


//写数据查询


//获取连接


conn = DBUtil.getConnection();


User user1 = null;


//Sql 语句


String sql = "select * from user where u_name = ?";


try {


//预编译


ps=conn.prepareStatement(sql);


//值的插入


ps.setString(1,user.getUsername());


//查询


rs = ps.executeQuery();


while (rs.next()){


int id = rs.getInt("id");


String username = rs.getString("u_name");


String password = rs.getString("u_pwd");


//实例化 User 对象


user1 = new User(id,username,password);


//System.out.println(user1);


}


} catch (SQLException e) {


e.printStackTrace();


}


return user1;


}


@Override


public boolean insertUser(User user) {


conn = DBUtil.getConnection();


String sql = "insert into user (u_name,u_pwd) values(?,?)";


//


try {


ps = conn.prepareStatement(sql);


ps.setString(1,user.getUsername());


ps.setString(2,user.getPassword());


int count = ps.executeUpdate();


return count>0;


} catch (SQLException e) {


e.printStackTrace();


}


return false;


}


}


EmpDaoImpl.java


package com.blb.dao.impl;


import com.blb.dao.EmpDao;


import com.blb.entity.Emp;


import com.blb.util.DBUtil;


import java.sql.Connection;


import java.sql.PreparedStatement;


import java.sql.ResultSet;


import java.sql.SQLException;


import java.util.ArrayList;


import java.util.List;


public class EmpDaoImpl implements EmpDao {


Connection conn = null;


PreparedStatement ps = null;


ResultSet rs = null;


/**


  • 查询所有员工信息

  • @return 存储员工对象的集合


*/


@Override


public List<Emp> getAllErp() {


List<Emp> emps = new ArrayList<>();


conn = DBUtil.getConnection();


String sql = "select * from erp";


try {


ps = conn.prepareStatement(sql);


rs = ps.executeQuery();


while (rs.next()) {


int id = rs.getInt("s_id");


String erpName = rs.getString("s_name");


String deptName = rs.getString("dept_name");


Emp emp = new Emp(id, erpName, deptName);


emps.add(emp);


}


} catch (SQLException e) {


e.printStackTrace();


}


return emps;


}


/**


  • 通过 id 查询对应员工的详细信息

  • @param id

  • @return emp 对象


*/


@Override


public Emp getEmpById(int id) {


conn = DBUtil.getConnection();


String sql = "Select * from erp where s_id = ?";


try {


ps = conn.prepareStatement(sql);


ps.setInt(1, id);


rs = ps.executeQuery();


while (rs.next()){


int sid = rs.getInt("s_id");


String erpName = rs.getString("s_name");


String deptName = rs.getString("dept_name");


Emp emp = new Emp(sid,erpName,deptName);


return emp;


}


} catch (SQLException e) {


e.printStackTrace();


}


return null;


}


/**


  • 新增员工

  • @param emp

  • @return 布尔信息


*/


@Override


public boolean insertEmp(Emp emp) {


conn = DBUtil.getConnection();


String sql = "insert into erp (s_name,dept_name) values(?,?)";


try {


ps = conn.prepareStatement(sql);


ps.setString(1,emp.getErpName());


ps.setString(2,emp.getDeptName());


int count = ps.executeUpdate();


return count>0;


} catch (SQLException e) {


e.printStackTrace();


}


return false;


}


/**


  • 删除员工

  • @param id

  • @return 布尔值


*/


@Override


public boolean delEmp(int id) {


conn = DBUtil.getConnection();


String sql = "delete from erp where s_id = ?";


try {


ps = conn.prepareStatement(sql);


ps.setInt(1,id);


int count = ps.executeUpdate();


return count>0;


} catch (SQLException e) {


e.printStackTrace();


}


return false;


}


@Override


public boolean updateEmp(Emp emp) {


conn = DBUtil.getConnection();


String sql = "update erp set s_name =? , dept_name = ? where s_id = ?";


try {


ps = conn.prepareStatement(sql);


ps.setString(1,emp.getErpName());


ps.setString(2,emp.getDeptName());


ps.setInt(3,emp.getId());


int count = ps.executeUpdate();


return count>0;


} catch (SQLException e) {


e.printStackTrace();


}


return false;


}


}


service 层


UserService(接口)


package com.blb.service;


import com.blb.entity.User;


/**


  • service 接口,一个 login(登录判断)


*/


public interface UserService {


//user 其实是 servlet 获取到的用户信息,封装的 user 对象


boolean login(User user);


//注册方法


boolean register(User user);


}


EmpService(接口)


package com.blb.service;


import com.blb.entity.Emp;


import java.util.List;


public interface EmpService {


List<Emp> getAllErp();


Emp getEmpById(int id);


boolean insertEmp(Emp emp);


boolean delEmp(int id);


boolean updateEmp(Emp emp);


}


UserServiceImpl.java


package com.blb.service.impl;


import com.blb.dao.UserDao;


import com.blb.dao.impl.UserDaoImpl;


import com.blb.entity.User;


import com.blb.service.UserService;


public class UserServiceImpl implements UserService {


UserDao userDao = new UserDaoImpl();


/**


  • 登录的实现方法

  • @param user

  • @return


*/


@Override


public boolean login(User user) {


User user1 = userDao.getUserByUserName(user);


if (user1==null){


return false;


}else if (!user.getPassword().equals(user1.getPassword())){


return false;


}


return true;


}


/**


  • 注册的实现方法

  • @param user

  • @return


*/


@Override


public boolean register(User user) {


System.out.println(userDao.getUserByUserName(user));


if (userDao.getUserByUserName(user)!=null){


return false;


} if (userDao.insertUser(user)==false){


return false;


}


return true;


}


}


EmpServiceImpl.java


package com.blb.service.impl;


import com.blb.dao.EmpDao;


import com.blb.dao.impl.EmpDaoImpl;


import com.blb.entity.Emp;


import com.blb.service.EmpService;


import java.util.List;


public class EmpServiceImpl implements EmpService {


EmpDao empDao = new EmpDaoImpl();


@Override


public List<Emp> getAllErp() {


return empDao.getAllErp();


}


@Override


public Emp getEmpById(int id) {


return empDao.getEmpById(id);


}


@Override


public boolean insertEmp(Emp emp) {


return empDao.insertEmp(emp);


}


@Override


public boolean delEmp(int id) {


return empDao.delEmp(id);


}


@Override


public boolean updateEmp(Emp emp) {


return empDao.updateEmp(emp);


}


}


Servlet 层:对接我们的 JSP 页面


DoAddServlet.java


package com.blb.servlet;


import com.blb.entity.Emp;


import com.blb.service.EmpService;


import com.blb.service.impl.EmpServiceImpl;


import javax.servlet.ServletException;


import javax.servlet.annotation.WebServlet;


import javax.servlet.http.HttpServlet;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import java.io.IOException;


@WebServlet("/doAdd")


public class DoAddServlet extends HttpServlet {


@Override


protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


//获取 jsp 页面的员工信息


String empName = req.getParameter("empName");


String deptName = req.getParameter("deptName");


//封装员工对象


Emp emp = new Emp(empName,deptName);


//实例化 empService


EmpService empService = new EmpServiceImpl();


//获取新增结果


boolean res = empService.insertEmp(emp);


//如果成功,则返回首页,失败则提示信息


if (res){


req.getRequestDispatcher("/toIndex").forward(req,resp);


}else{


req.setAttribute("msg","新增失败");


req.getRequestDispatcher("/add.jsp").forward(req,resp);


}


}


}


DoDeleteServlet.java


package com.blb.servlet;


import com.blb.service.EmpService;


import com.blb.service.impl.EmpServiceImpl;


import javax.servlet.ServletException;


import javax.servlet.annotation.WebServlet;


import javax.servlet.http.HttpServlet;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import java.io.IOException;


@WebServlet("/doDelete")


public class DoDeleteServlet extends HttpServlet {


@Override


protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


int id = Integer.parseInt(req.getParameter("id"));


EmpService empService = new EmpServiceImpl();


boolean res = empService.delEmp(id);


if(res){


req.getRequestDispatcher("/toIndex").forward(req,resp);


}else {


req.setAttribute("msg","删除失败");


req.getRequestDispatcher("/toIndex").forward(req,resp);


}


}


}


DoRegisterServlet.java


package com.blb.servlet;


import com.blb.entity.User;


import com.blb.service.UserService;


import com.blb.service.impl.UserServiceImpl;


import javax.servlet.ServletException;


import javax.servlet.annotation.WebServlet;


import javax.servlet.http.HttpServlet;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import java.io.IOException;


@WebServlet("/doRegister")


public class DoRegisterServlet extends HttpServlet {


@Override


protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


UserService userService = new UserServiceImpl();


//获取 jsp 页面数据


String username = req.getParameter("username");


String password = req.getParameter("password");


//实例化 User 对象


User user = new User(username,password);


boolean res = userService.register(user);


System.out.println(res);


if (res){


req.getRequestDispatcher("/login.jsp").forward(req,resp);


}else {


req.getRequestDispatcher("/register.jsp").forward(req,resp);


}


}


}


DoSelectServlet.java


package com.blb.servlet;


import com.blb.entity.Emp;


import com.blb.service.EmpService;


import com.blb.service.impl.EmpServiceImpl;


import javax.servlet.ServletException;


import javax.servlet.annotation.WebServlet;


import javax.servlet.http.HttpServlet;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import javax.servlet.http.HttpSession;


import java.io.IOException;


@WebServlet("/doSelect")


public class DoSelectServlet extends HttpServlet {


@Override


protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


HttpSession session = req.getSession();


int id = Integer.parseInt(req.getParameter("id"));


EmpService empService = new EmpServiceImpl();


Emp emp = empService.getEmpById(id);


session.setAttribute("emp",emp);


req.getRequestDispatcher("/xq.jsp").forward(req,resp);


}


}


DoUpdateServlet.java


package com.blb.servlet;


import com.blb.entity.Emp;


import com.blb.service.EmpService;


import com.blb.service.impl.EmpServiceImpl;


import javax.servlet.ServletException;


import javax.servlet.annotation.WebServlet;


import javax.servlet.http.HttpServlet;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import java.io.IOException;


@WebServlet("/doUpdate")


public class DoUpdateServlet extends HttpServlet {


@Override


protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


//获取 jsp 页面的员工信息


String empName = req.getParameter("empName");


String deptName = req.getParameter("deptName");


int id = Integer.parseInt(req.getParameter("id"));


//封装员工对象


Emp emp = new Emp(id,empName,deptName);


//实例化 empService


EmpService empService = new EmpServiceImpl();


boolean res = empService.updateEmp(emp);


if(res){


req.getRequestDispatcher("/toIndex").forward(req,resp);


}else {


req.setAttribute("msg","更新失败");


req.getRequestDispatcher("/toIndex").forward(req,resp);


}


}


}


IndexServlet.java


package com.blb.servlet;


import com.blb.entity.Emp;


import com.blb.service.EmpService;


import com.blb.service.impl.EmpServiceImpl;


import javax.servlet.ServletException;


import javax.servlet.annotation.WebServlet;


import javax.servlet.http.HttpServlet;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import javax.servlet.http.HttpSession;


import java.io.IOException;


import java.util.List;


@WebServlet("/toIndex")


public class IndexServlet extends HttpServlet {


@Override


protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


//


HttpSession session = req.getSession();


EmpService empService = new EmpServiceImpl();


List<Emp> emps = empService.getAllErp();


session.setAttribute("emps", emps);


resp.sendRedirect("/User_demo_war_exploded/index.jsp");


}


@Override


protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


doGet(req,resp);


}


}


LoginServlet.java


package com.blb.servlet;


import com.blb.entity.User;


import com.blb.service.UserService;


import com.blb.service.impl.UserServiceImpl;


import javax.servlet.ServletException;


import javax.servlet.annotation.WebServlet;


import javax.servlet.http.HttpServlet;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import java.io.IOException;


@WebServlet("/login")


public class LoginServlet extends HttpServlet {


@Override


protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


//实例化 Service 对象


UserService userService = new UserServiceImpl();


//从 jsp 页面的请求获取用户名和密码


String username = req.getParameter("username");


String password = req.getParameter("password");


//实例化 User 对象


User user = new User(username,password);


//登录判断


boolean result = userService.login(user);


if (result){


req.setAttribute("username",username);


req.getRequestDispatcher("/toIndex").forward(req,resp);


}else {


req.setAttribute("msg","用户名或密码错误");


req.getRequestDispatcher("/login.jsp").forward(req,resp);


}


}


}


ToAddServlet.java


package com.blb.servlet;


import javax.servlet.ServletException;


import javax.servlet.annotation.WebServlet;


import javax.servlet.http.HttpServlet;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import java.io.IOException;


@WebServlet("/toAdd")


public class ToAddServlet extends HttpServlet {


@Override


protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


resp.sendRedirect("/User_demo_war_exploded/add.jsp");


}


} 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】


ToRegisterServlet.java


package com.blb.servlet;


import javax.servlet.ServletException;


import javax.servlet.annotation.WebServlet;


import javax.servlet.http.HttpServlet;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import java.io.IOException;


@WebServlet("/toRegister")


public class ToRegisterServlet extends HttpServlet {


@Override


protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


resp.sendRedirect("/User_demo_war_exploded/register.jsp");


}


}


JSP 页面


添加用户:


<html>


<head>


<title>Title</title>


</head>


<body>


${msg}


<form action="doAdd">


姓名:<input type="text" name="empName"><br>


部门:<select name="deptName">


<option>人力资源</option>


<option selected>市场</option>


<option>财务</option>


<option>研发部</option>


</select>


<input type="submit" value="添加">


</form>


</body>


</html>


首页:


<html>


<head>


<title></title>


</head>


<body>


${msg}


<table border="1" cellpadding="0" cellspacing="0">


<tr>


<th>员工编号</th>


<th>姓名</th>


<th>部门名称</th>


<th>操作</th>


</tr>


<c:forEach items="${emps}" var="emp">

用户头像

还未添加个人签名 2022.04.13 加入

还未添加个人简介

评论

发布
暂无评论
JavaWeb期末复习_Java_爱好编程进阶_InfoQ写作社区