用户登录只有第一条数据成功,只能查询到第一条数据

来源:互联网 发布:天国王朝知乎 编辑:程序博客网 时间:2024/04/24 03:39

实现用户登录功能


四条数据,只有第一条可登陆成功

原因是:

sql = "select*from manager";


应为

             

sql = "select*from manager where username='"+username+"'and password='"+password+"'";

response.setContentType("test/html;charset=utf-8");response.setCharacterEncoding("utf-8");// 获取jsp中填入数据String username = request.getParameter("username");String password = request.getParameter("password");// 数据库相关String Dusername = null;String Dpassword = null;int Did = 0;String sql = null;// 与数据库建立连接try {Class.forName("com.mysql.jdbc.Driver");System.out.println("Loading Database success");} catch (Exception e) {System.out.println("Class not found exception");}String url = "jdbc:mysql://localhost:3306/Creation";Connection con = null;Statement stmt = null;ResultSet rs = null;try {con = (Connection) DriverManager.getConnection(url, "root", "");stmt = (Statement) con.createStatement();sql = "select*from manager where username='"+username+"'and password='"+password+"'";rs = stmt.executeQuery(sql);while (rs.next()) {// 因为不止一个数据,就要循环,对每条数据都进行验证匹配Dusername = rs.getString("username");Dpassword = rs.getString("password");System.out.println(Dusername);System.out.println(Dpassword);if (username.equals(Dusername) && password.equals(Dpassword)) {System.out.println("manager登陆成功");// 要成功,跳转success.jspresponse.sendRedirect("success.jsp");}else if (username.equals("admin") && password.equals("admin")) {System.out.println("super manager登陆成功");// 要成功,跳转success.jspresponse.sendRedirect("manager.jsp");return;}else {System.out.println("登陆失败");response.sendRedirect("fail.jsp");return;}}} catch (Exception e) {e.printStackTrace();}}


0 0
原创粉丝点击