基于JavaWeb实现ATM机模拟取款系统实战(项目准备+管理端控制)

JSP,SERVLET,JDBC的应用,结合三层架构和MVC设计模式完成模拟ATM机存取款的实战项目

一:环境准备(JDK+Tomcat9)

1.1 JDK1.8 安装

1.1.1 JDK介绍

JDK(Java Development Kit) 又称 J2SDK(Java2 Software Development Kit),是 Java 开发工具包,它提供了 Java 的开发环境(提供了编译器 javac 等工具,用于将 java 文件编译为 class 文件)和运行环境(提 供了 JVM 和 Runtime 辅助包,用于解析 class 文件使其得到运行)。如果你下载并安装了 JDK,那么你不仅可以开发 Java 程序,也同时拥有了运行 Java 程序的平台。JDK 是整个 Java 的核心,包括了 Java 运行环境(JRE),一堆 Java 工具 tools.jar 和 Java 标准类库 (rt.jar)。

1.1.2 JDK下载与环境安装

自行到官网下载

Java 存档下载 — Java SE 8 | Oracle 中国

exe.zip文件压缩包解压后记住文件路径

配置环境变量

win+i 打开设置 找到系统环境变量

添加JAVA_HOME

JAVA_HOME             C:\Program Files\Java\jdk1.8.0_161

 path中添加

%JAVA_HOME%\bin

 1.2  tomcat 安装

 一:下载 tomcat的安装包

tomcat官网

Apache Tomcat® - Welcome!

解压后配置环境变量。。。

详细可以看 Windows下安装Tomcat_ZLY_2004的博客-CSDN博客

 二:项目实战

2.1 数据准备

 在数据库中创建 atm 数据库 导入执行sql文件

更改DButils 下对应数据库的配置信息,例如我的是mysql8则需要更换驱动以及jar

基于JavaWeb实现ATM机模拟取款系统实战(项目准备+管理端控制)_第1张图片

2.2 管理端页面撰写

2.2.1 管理员用户登录

Dao

/**
 * 管理员用户持久层操作
 */
public class AdminUserDao {
    public Manager findManager(String name,String password) {
        Connection connection = DBUtils.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Manager manager=null;
        try {
            preparedStatement = connection.prepareStatement("select * from manage where name = ? and password =?");
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,password);
             resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                manager= new Manager();
                manager.setId(resultSet.getInt(1));
                manager.setName(resultSet.getString(2));
                manager.setPassword(resultSet.getString(3));
            }
            return manager;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtils.release(connection,preparedStatement,resultSet);
        }

    }
}

Service

public interface AdminUserService {
    /**
     *
     * @param name 姓名
     * @param password 密码
     * @return  用户
     */
    public Manager findManager(String name , String password);
}

        impl

public class AdminUserServiceImpl implements AdminUserService {
    private AdminUserDao userDao=new AdminUserDao();


    @Override
    public Manager findManager(String name, String password) {
        return userDao.findManager(name, password);
    }
}

Servlet

@WebServlet("/login")
public class AdminLoginServlet extends HttpServlet {
    private AdminUserService adminUserService =new AdminUserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
        String name = req.getParameter("name");
        String password = req.getParameter("password");
//
        Manager manager = adminUserService.findManager(name, password);
        if (null==manager){
            req.setAttribute("error","用户登录失败");
            req.getRequestDispatcher("changepage/login.jsp").forward(req,resp);
        }else {
            //登录成功 跳转页面
            resp.sendRedirect("list");
//            req.getRequestDispatcher("changepage/userlist.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
2.2.2 用户增删改查

Dao


public class UserDao {
    public List findAllUsers() {
        Connection connection = DBUtils.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement  = connection.prepareStatement("select * from user");
            resultSet=preparedStatement.executeQuery();
            List users = new ArrayList<>();
            while (resultSet.next()) {
                User user = new User();
                user.setIcno(resultSet.getString(1));
                user.setName(resultSet.getString(2));
                user.setPwd(resultSet.getString(3));
                user.setMobile(resultSet.getString(4));
                user.setIdcard(resultSet.getString(5));
                user.setBalance(resultSet.getDouble(6));
                users.add(user);
            }
            return users;
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtils.release(connection,preparedStatement,resultSet);
        }
        return null;
    }

    public List queryUsers(String name) {
        Connection connection = DBUtils.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement  = connection.prepareStatement("select * from user where name like ?");
            preparedStatement.setString(1,"%"+name+"%");
            resultSet=preparedStatement.executeQuery();
            List users = new ArrayList<>();
            while (resultSet.next()) {
                User user = new User();
                user.setIcno(resultSet.getString(1));
                user.setName(resultSet.getString(2));
                user.setPwd(resultSet.getString(3));
                user.setMobile(resultSet.getString(4));
                user.setIdcard(resultSet.getString(5));
                user.setBalance(resultSet.getDouble(6));
                users.add(user);
            }
            return users;
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtils.release(connection,preparedStatement,resultSet);
        }
        return null;
    }

    public User getUserByIcno(String icno) {
        Connection connection = DBUtils.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement  = connection.prepareStatement("select * from user where icno = ?");
            preparedStatement.setString(1,icno);
            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()) {
                User user = new User();
                user.setIcno(resultSet.getString(1));
                user.setName(resultSet.getString(2));
                user.setPwd(resultSet.getString(3));
                user.setMobile(resultSet.getString(4));
                user.setIdcard(resultSet.getString(5));
                user.setBalance(resultSet.getDouble(6));
                return user;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtils.release(connection,preparedStatement,resultSet);
        }
        return null;
    }

    public int updateUser(User user) {
        Connection connection = DBUtils.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement  = connection
                    .prepareStatement("update user set name = ?, pwd = ? ,mobile = ?, idcard = ?, balance = ? where icno = ?");
            preparedStatement.setString(1,user.getName());
            preparedStatement.setString(2,user.getPwd());
            preparedStatement.setString(3,user.getMobile());
            preparedStatement.setString(4,user.getIdcard());
            preparedStatement.setDouble(5,user.getBalance());
            preparedStatement.setString(6,user.getIcno());
            int num= preparedStatement.executeUpdate();
            return num;
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtils.release(connection,preparedStatement,null);
        }
        return 0;
    }

    public int deleteUser(String icno) {
        Connection connection = DBUtils.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement  = connection
                    .prepareStatement("delete from user where icno =?");
            preparedStatement.setString(1,icno);
            int num= preparedStatement.executeUpdate();
            return num;
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtils.release(connection,preparedStatement,null);
        }
        return 0;
    }

    public int addUser(User user) {
        Connection connection = DBUtils.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement  = connection
                    .prepareStatement("insert into  user values(?,?,?,?,?,?)");
            preparedStatement.setString(1,user.getIcno());
            preparedStatement.setString(2,user.getName());
            preparedStatement.setString(3,user.getPwd());
            preparedStatement.setString(4,user.getMobile());
            preparedStatement.setString(5,user.getIdcard());
            preparedStatement.setDouble(6,user.getBalance());
            int num= preparedStatement.executeUpdate();
            return num;
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtils.release(connection,preparedStatement,null);
        }
        return 0;
    }
}

Service

public interface UserService {
    /**
     * 查询所用用户信息
     * @return 所有用户
     */
    List findAllUsers();

    /**
     * 模糊查询
     * @param name 名字
     * @return 模糊查询黁结果
     */
    List queryUsers(String name);

    /**
     * 根基银行卡查询信息
     * @param icno 银行卡信息
     */
    User getUserByIcno(String icno);

    /**
     *  更新用户
     * @param user 用户
     * @return 是否更新成功
     */
    boolean updateUser(User user);

    /**
     * 根据卡号删除用户
     * @param icno 卡号
     * @return 是否删除成功
     */
    boolean deleteUser(String icno);

    /**
     * 添加用户
     * @param user 用户信息
     * @return 是否添加成功
     */
    boolean addUser(User user);
}

        impl

public class UserServiceImpl implements UserService {
    private UserDao userDao=new UserDao();
    @Override
    public List findAllUsers() {
        return userDao.findAllUsers();
    }

    @Override
    public List queryUsers(String name) {
        return userDao.queryUsers(name);
    }

    @Override
    public User getUserByIcno(String icno) {
        return userDao.getUserByIcno(icno);
    }

    @Override
    public boolean updateUser(User user) {
        return userDao.updateUser(user)>=1?true:false;
    }

    @Override
    public boolean deleteUser(String icno) {
        return userDao.deleteUser(icno)>=1?true:false;
    }

    @Override
    public boolean addUser(User user) {
        return userDao.addUser(user)>=1?true:false;
    }
}

Servlet

/**
 * 添加用户
 */
@WebServlet("/insert")
public class UserAddServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
        String icno = req.getParameter("icno");
        String name = req.getParameter("name");
        String mobile = req.getParameter("mobile");
        String idcard = req.getParameter("idcard");
        Double balance=null;
        if (req.getParameter("balance")!=null){
            balance  = Double.parseDouble(req.getParameter("balance"));
        }
        User user = new User();
        user.setBalance(balance);
        user.setIdcard(idcard);
        user.setPwd("123456");
        user.setMobile(mobile);
        user.setName(name);
        user.setIcno(icno);
        //业务添加
        boolean result = userService.addUser(user);
        if (result){
            //页面显示
            resp.sendRedirect("/list");
        }else {
            resp.getOutputStream().print("添加失败");
        }
    }
}
/**
 * 删除用户
 */
@WebServlet("/delete")
public class UserDeleteServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String icno = req.getParameter("icno");
        boolean result =userService.deleteUser(icno);
        if (result){
            resp.sendRedirect("/list");
        }else {
            resp.getOutputStream().print("更新失败");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
/**
 * 用户查询
 */
@WebServlet("/list")
public class UserListServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用用户业务接口方法
        List allUsers = userService.findAllUsers();
        req.getSession().setAttribute("list",allUsers);
//        req.getRequestDispatcher("/changepage/userlist.jsp").forward(req,resp);
        resp.sendRedirect("/changepage/userlist.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
/**
 * 模糊查询
 */
@WebServlet("/select")
public class UserSelectListServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
        String name=req.getParameter("name");
        List users= userService.queryUsers(name);
        req.getSession().setAttribute("list2",users);
        resp.sendRedirect("/changepage/userlist.jsp");
    }
}
/**
 * 跳转修改页面
 */
@WebServlet("/toUpdate")
public class UserToUpdateServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String icno = req.getParameter("icno");
        User userByIcno = userService.getUserByIcno(icno);
        if (userByIcno==null){
            req.getSession().setAttribute("error","此用户不存在");
        }else {
            req.getSession().setAttribute("user",userByIcno);
            resp.sendRedirect("changepage/update.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
@WebServlet("/update")
public class UserUpdateServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
        String icno = req.getParameter("icno");
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        String mobile = req.getParameter("mobile");
        String idcard = req.getParameter("idcard");
        Double balance=null;
        if (req.getParameter("balance")!=null){
            balance  = Double.parseDouble(req.getParameter("balance"));
        }
        User user = new User();
        user.setBalance(balance);
        user.setIdcard(idcard);
        user.setPwd(pwd);
        user.setMobile(mobile);
        user.setName(name);
        user.setIcno(icno);
        boolean result = userService.updateUser(user);
        if (result){
            resp.sendRedirect("/list");
        }else {
            resp.getOutputStream().print("更新失败");
        }
    }
}

2.2.3 全局Filter

/**
 * 过滤器 添加编码过滤 只对post请求有效
 */
@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("UTF-8");
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

解决中文乱码问题

三:演示效果

 基于JavaWeb实现ATM机模拟取款系统实战(项目准备+管理端控制)_第2张图片

 基于JavaWeb实现ATM机模拟取款系统实战(项目准备+管理端控制)_第3张图片

 基于JavaWeb实现ATM机模拟取款系统实战(项目准备+管理端控制)_第4张图片

 基于JavaWeb实现ATM机模拟取款系统实战(项目准备+管理端控制)_第5张图片

 

你可能感兴趣的:(java,tomcat)