这是查询代码:
//根据用户ID查找指定用户 public Student studentByID(String sid){ Student student =new Student(); Statement stmt3=null; ResultSet rs=null; Connection con2=null; try{ stmt3=con2.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); rs=stmt3.executeQuery("select * from student where Sid='"+sid+"'"); if (rs.next()){ student.setSid(rs.getString(1)); student.setName(rs.getString(2)); student.setClasses(rs.getInt(3)); student.setSex(rs.getString(4)); student.setEmail(rs.getString(5)); student.setPolicalstatus(rs.getString(6)); student.setPassword(rs.getString(7)); student.setAge(rs.getInt(8)); student.setRole(rs.getString(9)); } rs.close(); stmt3.close(); con2.close(); } catch (SQLException ex){ ex.printStackTrace(); } return student; }
//这是登录页的代码:
String name=req.getParameter("names"); String password=req.getParameter("passwords"); String sid=req.getParameter("sid"); String role=""; //调用用户模型,获取用户角色 studentDAO studentDAO = new dbconnection(); student = studentDAO.studentByID(sid); req.setAttribute("student",student); if(password.equals(studentDAO.studentByID(sid).getPassword())){ role=studentDAO.validate(name,password); if(role.equals("普通用户")){ RequestDispatcher requestDispatcher = req.getRequestDispatcher("userpage.jsp"); requestDispatcher.forward(req,resp); } else if (role.equals("管理员")){ RequestDispatcher requestDispatcher =req.getRequestDispatcher("managerpage.jsp"); requestDispatcher.forward(req,resp); } else{ RequestDispatcher requestDispatcher = req.getRequestDispatcher("load.jsp"); requestDispatcher.forward(req,resp); } } else{ System.out.println("您的密码有误!"); RequestDispatcher requestDispatcher=req.getRequestDispatcher("load.jsp"); requestDispatcher.forward(req,resp); } }