核心提示:用户登录只有第一条数据成功,只能查询到第一条数据:实现用户登录功能。四条数据,只有第一条可登陆成功原因是: sql = select*from manager;应为 sql = select*from...
用户登录只有第一条数据成功,只能查询到第一条数据:实现用户登录功能。
四条数据,只有第一条可登陆成功
原因是:
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.jsp response.sendRedirect("success.jsp"); } else if (username.equals("admin") && password.equals("admin")) { System.out.println("super manager登陆成功"); // 要成功,跳转success.jsp response.sendRedirect("manager.jsp"); return; } else { System.out.println("登陆失败"); response.sendRedirect("fail.jsp"); return; } } } catch (Exception e) { e.printStackTrace(); } }