Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) (2)

(4)User.java 文件:

package com.pojo; //用来存放用户登录时,输入输出的信息 public class User { private String uname; private String upwd; private String role; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } }

(5)UserDAO.java 文件:

package com.dao; import com.dbutil.DBUtils; import com.pojo.User; public class UserDAO { //返回 0 或 1 public int login(User user){ DBUtils dbUtils = new DBUtils(); //与数据库是否匹配,匹配为 1 int temp = dbUtils.login(user); return temp; } }

(6)DBUtils.java 文件:
!!! 成败在与这个文件

自行更改数据库名

自行更改表名(两个)

自行更改 root 的密码

检查服务是否启动

package com.dbutil; import com.pojo.User; import java.sql.*; public class DBUtils { Connection conn; //预处理 PreparedStatement pstmt; //结果集 ResultSet rs; public DBUtils(){ try{ Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC","root","密码"); } catch (Exception e){ e.printStackTrace(); } } public int login(User user){ try{ pstmt=conn.prepareStatement("select * from 表名 where uname = ? and upwd = md5(?)"); pstmt.setString(1, user.getUname()); pstmt.setString(2, user.getUpwd()); rs = pstmt.executeQuery(); if (rs.next()) return 1; else return 0; }catch (Exception e){ e.printStackTrace(); } return 0; } }

(7)Servlet.java 文件:

package com.loginServlet; import com.pojo.User; import com.service.UserService; 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(name = "Servlet",urlPatterns = "/Servlet") public class Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doPost"); request.setCharacterEncoding("utf-8"); //获取 post 请求中的两个参数 String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); String role = request.getParameter("role"); /*硬编码, 不从数据库获取信息,直接验证 if (uname.equals("admin") && upwd.equals("123456")) { response.sendRedirect("index.jsp"); }else{ response.sendRedirect("loginServlet.html"); }*/ //从服务获取值 UserService userService = new UserService(); User user = new User(); user.setUname(uname); user.setUpwd(upwd); user.setRole(role); if (userService.login(user)) { response.sendRedirect("index.jsp"); }else{ response.sendRedirect("failed.jsp"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doGet"); } }

(8)UserService.java 文件:

package com.service; //服务层,给控制层提供服务 import com.dao.UserDAO; import com.pojo.User; public class UserService { UserDAO dao = new UserDAO(); //参数,处理 User 对象,返回 True 或 False public boolean login(User user){ int temp = dao.login(user); if (temp == 0) return false; else return true; } } 四、运行调试

在这里插入图片描述

五、常见错误

(1)MySQL 8.0 版本的 jdbc 个数据版本不匹配

(2)数据库服务没有运行

(3)配置文件数据库信息没有自行更改

(4)文件名自定义时,依赖失败

更多文章链接:

Spring 笔记

支持博主

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzjsfy.html