使用JSP+Servlet+JDBC技术 三层架构 对atm库中的atm_user表进行 1,登录 2,列表(user_name条件模糊查询) 3,删除

时间: 2023-11-22 07:56:44 浏览: 44
好的,我可以为您提供一个简单的示例代码。 首先,我们需要建立一个三层架构的项目,包含 DAO 层、Service 层和 Controller 层。 在 DAO 层中,我们可以定义一个 UserDao 接口和一个 UserDaoImpl 类来实现该接口。在 UserDao 接口中,我们可以定义登录、列表和删除等操作的方法。在 UserDaoImpl 类中,我们可以使用 JDBC 技术来实现这些方法。 在 Service 层中,我们可以定义一个 UserService 接口和一个 UserServiceImpl 类来实现该接口。在 UserServiceImpl 类中,我们可以调用 UserDaoImpl 中的方法,并对数据进行一些处理和封装。 在 Controller 层中,我们可以定义一个 UserServlet 类来处理用户请求。在 UserServlet 类中,我们可以调用 UserServiceImpl 中的方法,并将结果发送给用户界面。 下面是示例代码: UserDao.java ```java public interface UserDao { boolean login(String username, String password) throws SQLException; List<User> getUserList(String username) throws SQLException; boolean deleteUser(int id) throws SQLException; } ``` UserDaoImpl.java ```java public class UserDaoImpl implements UserDao { private Connection conn = null; private PreparedStatement pstmt = null; public UserDaoImpl(Connection conn) { this.conn = conn; } @Override public boolean login(String username, String password) throws SQLException { String sql = "SELECT * FROM atm_user WHERE user_name=? AND user_password=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, username); pstmt.setString(2, password); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { return true; } else { return false; } } @Override public List<User> getUserList(String username) throws SQLException { List<User> userList = new ArrayList<>(); String sql = "SELECT * FROM atm_user WHERE user_name LIKE ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, "%" + username + "%"); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { User user = new User(); user.setId(rs.getInt("user_id")); user.setUsername(rs.getString("user_name")); user.setPassword(rs.getString("user_password")); user.setBalance(rs.getDouble("user_balance")); userList.add(user); } return userList; } @Override public boolean deleteUser(int id) throws SQLException { String sql = "DELETE FROM atm_user WHERE user_id=?"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); int result = pstmt.executeUpdate(); if (result > 0) { return true; } else { return false; } } } ``` UserService.java ```java public interface UserService { boolean login(String username, String password) throws SQLException; List<User> getUserList(String username) throws SQLException; boolean deleteUser(int id) throws SQLException; } ``` UserServiceImpl.java ```java public class UserServiceImpl implements UserService { private UserDao userDao; public UserServiceImpl(UserDao userDao) { this.userDao = userDao; } @Override public boolean login(String username, String password) throws SQLException { return userDao.login(username, password); } @Override public List<User> getUserList(String username) throws SQLException { return userDao.getUserList(username); } @Override public boolean deleteUser(int id) throws SQLException { return userDao.deleteUser(id); } } ``` UserServlet.java ```java public class UserServlet extends HttpServlet { private UserService userService; public UserServlet() { super(); userDao = new UserDaoImpl(DBUtil.getConnection()); userService = new UserServiceImpl(userDao); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); if (action.equals("login")) { String username = request.getParameter("username"); String password = request.getParameter("password"); try { if (userService.login(username, password)) { response.sendRedirect("userList.jsp"); } else { response.sendRedirect("login.jsp"); } } catch (SQLException e) { e.printStackTrace(); } } else if (action.equals("list")) { String username = request.getParameter("username"); try { List<User> userList = userService.getUserList(username); request.setAttribute("userList", userList); RequestDispatcher rd = request.getRequestDispatcher("userList.jsp"); rd.forward(request, response); } catch (SQLException e) { e.printStackTrace(); } } else if (action.equals("delete")) { int id = Integer.parseInt(request.getParameter("id")); try { userService.deleteUser(id); response.sendRedirect("userList.jsp"); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 以上代码仅供参考,具体的实现还需要您根据自己的需求进行修改。希望对您有所帮助!

相关推荐

最新推荐

recommend-type

javaee 实验报告 实验一 JSP+Servlet+Javabean+jdbc-基于MVC模式的数据库的应用

实验一 JSP+Servlet+Javabean+jdbc-基于MVC模式的数据库的应用 实验二 JSF+JPA实现用户注册 实验三 struts+spring+hibernate框架访问数据库的应用 实验四 使用WebService和EJBean访问数据库的应用
recommend-type

使用JSP+SERVLET+JDBC实现对数据库的增删改查

使用JSP+SERVLET+JDBC实现对数据库的增删改查使用JSP+SERVLET+JDBC实现对数据库的增删改查
recommend-type

mysql分页 jsp实现 servlet+javabean+jsp三级架构

自己根据所作的一个项目对mysql分页的总结,菜鸟还有理解不到位的地方还请老鸟指教,因为自己纯手工制作所以分值高点犒劳一下自己。网上也有很多类似的东西,我想应该和我的不同
recommend-type

servlet+jsp实现过滤器 防止用户未登录访问

我们可能经常会用到这一功能,比如有时,我们不希望用户没有进行登录访问后台的操作页面,而且这样的非法访问会让系统极为的不安全,所以我们常常需要进行登录才授权访问其它页面,否则只会出现登录页面,当然我的...
recommend-type

jsp页面中获取servlet请求中的参数的办法详解

在JAVA WEB应用中,如何获取servlet请求中的参数,本文讲解了jsp页面中获取servlet请求中的参数的办法
recommend-type

中文翻译Introduction to Linear Algebra, 5th Edition 2.1节

中文翻译Introduction to Linear Algebra, 5th Edition 2.1节 线性代数的核心问题是求解方程组。这些方程都是线性的,即未知数仅与数相乘——我们绝不会 遇见 x 乘以 y。我们的第一个线性方程组较小。接下来你来看看它引申出多远: 两个方程 两个未知数 x − 2y = 1 3x + 2y = 11 (1) 我们一次从一个行开始。第一个方程 x − 2y = 1 得出了 xy 平面的一条直线。由于点 x = 1, y = 0 解 出该方程,因此它在这条直线上。因为 3 − 2 = 1,所以点 x = 3, y = 1 也在这条直线上。若我们选择 x = 101,那我们求出 y = 50。 这条特定直线的斜率是 12,是因为当 x 变化 2 时 y 增加 1。斜率在微积分中很重要,然而这是线 性代数! 图 2.1 将展示第一条直线 x − 2y = 1。此“行图”中的第二条直线来自第二个方程 3x + 2y = 11。你 不能错过两条线的交点 x = 3, y = 1。点 (3, 1) 位于两条线上并且解出两个方程。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

识别MATLAB微分方程求解中的混沌行为:分析非线性方程混沌行为的实用技巧

![matlab求解微分方程](https://img-blog.csdnimg.cn/2021062810300367.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTQ3OTY3OA==,size_16,color_FFFFFF,t_70) # 1. MATLAB微分方程求解概述 微分方程是描述物理、工程和金融等领域中动态系统的数学模型。MATLAB提供了强大的工具来求解微分方程,包括内置求解器和自定义函数
recommend-type

physon如何做ERP系统

作为一个AI语言模型,我可以给您提供一些基本的建议和思路: 1.明确需求和目标:在开始开发ERP系统之前,需要明确您的业务需求和目标,了解您的业务流程、流程数据、员工角色和权限、财务管理等方面的要求,这样才能更好的设计和开发ERP系统。 2.选择技术框架和开发工具:选择合适的技术框架和开发工具是ERP系统开发的关键。选择一种流行的技术框架和工具可以提高开发效率和质量。 3.设计数据库:ERP系统需要一个功能强大的数据库来存储数据。设计数据库需要考虑数据的完整性、安全性和可扩展性。 4.设计系统架构:系统架构是ERP系统的骨架,需要考虑系统的可扩展性、可维护性和性能。 5.开发和测试:
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。