日期:2014-05-19 浏览次数:20664 次
public User findUserByUserName(String userName) { // TODO Auto-generated method stub Connection conn = DBConnection.getConnection(); //获得连接对象 String findByUserNameSQL = "select * from user where username=?"; PreparedStatement pstmt = null; ResultSet rs = null; User user = new User(); try{ pstmt = conn.prepareStatement(findByUserNameSQL); pstmt.setString(1,userName); rs = pstmt.executeQuery(); if(rs.next()){ user = new User(); user.setUserID(rs.getInt(1)); user.setUserName(rs.getString(2)); user.setPassword(rs.getString(3)); } }catch(SQLException e){ e.printStackTrace(); }finally{ DBConnection.close(rs); DBConnection.close(pstmt); DBConnection.close(conn); } return user; }
if(password == null || "".equals(password)){ request.setAttribute("error", "请输入密码"); //判断是否输入密码 dispatcher = servletContext.getRequestDispatcher("/admin/index.jsp"); }else{ UserDAO userDAO = UserDAOFactory.getUserDAOInstance(); //获得DAO实现类实例 User user = userDAO.findUserByUserName(userName);//查询账号 if(user == null){ request.setAttribute("error", "该账户不存在"); dispatcher = servletContext.getRequestDispatcher("/admin/index.jsp"); }else{ if(password.equals(user.getPassword())){ request.getSession().setAttribute("user", user); //该账户信息保存 response.sendRedirect("admin.jsp"); return ; }else{ request.setAttribute("error", "密码不正确!"); dispatcher = servletContext.getRequestDispatcher("/admin/index.jsp"); } } }