用户控制层:
@Controller @RequestMapping("UsersServlet") public class UsersController { private UsersDao usersDao = new UsersDao(); @Autowired private HttpServletRequest request; @RequestMapping("/loginadmin") public String loginadmin() { String username = request.getParameter("username"); String password = request.getParameter("password"); Users users = usersDao.login(username, password); if (users != null) { request.getSession().setAttribute("loginUsers", users); return "admin_index"; } else { request.setAttribute("msg", "登录失败,账号密码不匹配"); return "admin_login"; } } @RequestMapping("/userlogin") public String userlogin() { String userName = request.getParameter("username"); String password = request.getParameter("password"); String clientCheckcode = request.getParameter("validateCode"); String serverCheckcode = (String) request.getSession().getAttribute("checkcode"); if (clientCheckcode.equals(serverCheckcode)) { // 2. 去访问dao , 看看是否满足登录。 Users Users = usersDao.userlogin(userName, password); // 3. 针对dao的返回结果,做出响应 if (Users != null) { request.getSession().setAttribute("usersLogin", Users); CategoryDao categoryDao = new CategoryDao(); List数据库连接工具:categoryList = categoryDao.queryAll(); request.setAttribute("categoryList", categoryList.stream().filter(x -> x.getState().equals("1")).collect(Collectors.toList())); return "index"; } else { request.setAttribute("error", "用户名或密码错误!"); return "login"; } } else { request.setAttribute("error", "登录失败,验证码不正确!"); return "login"; } } @RequestMapping("/userreg") public String userreg() { String username = request.getParameter("username"); String password = request.getParameter("password"); String account = request.getParameter("account"); String email = request.getParameter("email"); String password2 = request.getParameter("password2"); if (!password.equals(password2)) { request.setAttribute("error", "注册失败,密码与确认密码不一致!"); return "reg"; } else { boolean isSuccess = usersDao.isReg(account); if (!isSuccess) { request.setAttribute("error", "注册失败,该用户名已存在!"); return "reg"; } else { usersDao.reg(username, account, password, email); request.setAttribute("error", "注册成功!"); return "reg"; } } } @RequestMapping("/listforadmin") public String listforadmin() { List list = usersDao.getUsers(); request.setAttribute("list", list); return "listusers"; } @RequestMapping("/del") public String del(Integer id) { usersDao.del(id); List list = usersDao.getUsers(); request.setAttribute("list", list); return "listusers"; } }
public class DbUtil { private static final String URL = "jdbc:mysql://localhost:3306/dbnews?useUnicode=true&characterEncoding=utf8"; private static final String USERNAME = ""; private static final String PASSWORD = ""; public static Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); return conn; } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } catch (SQLException e) { e.printStackTrace(); return null; } } public static boolean update(String sql, Object... params) { // TODO Auto-generated method stub // ������ boolean flag = false; // ��ȡ���Ӷ��� Connection conn = DbUtil.getConnection(); try { conn.setAutoCommit(false); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // ��ȡ�������� PreparedStatement pstm = null; try { pstm = conn.prepareStatement(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // ��SQL�������ֵ try { if (params != null) { for (int i = 0; i < params.length; i++) { pstm.setObject(i + 1, params[i]); } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // ִ��SQL��� int state = 0; try { state = pstm.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (state > 0) { // �ɹ� �ύ���� flag = true; // �ύ���� try { conn.commit(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { // ʧ�� �ع����� flag = false; try { conn.rollback(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return flag; } public ListlistCategory(String sql, Object... params) { Connection conn = getConnection(); PreparedStatement stat = null; List list = new ArrayList (); try { stat = conn.prepareStatement(sql); if (params != null) { for (int i = 0; i < params.length; i++) { // stat.setString(i, params[i]); stat.setObject(i + 1, params[i]); } } ResultSet rs = stat.executeQuery(); while (rs.next()) { Category category = new Category(); category.setId(rs.getInt("id")); category.setName(rs.getString("name")); category.setState(rs.getString("state")); list.add(category); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (stat != null) stat.close(); if (conn != null) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return list; } public List listNews(String sql, Object... params) { Connection conn = getConnection(); PreparedStatement stat = null; List list = new ArrayList (); try { stat = conn.prepareStatement(sql); if (params != null) { for (int i = 0; i < params.length; i++) { // stat.setString(i, params[i]); stat.setObject(i + 1, params[i]); } } ResultSet rs = stat.executeQuery(); while (rs.next()) { News news = new News(); news.setCategoryid(rs.getInt("categoryid")); news.setClicks(rs.getInt("clicks")); news.setContent(rs.getString("content")); news.setId(rs.getInt("id")); news.setPbdate(rs.getDate("pbdate")); news.setPbdeptid(rs.getInt("pbdeptid")); news.setPublisher(rs.getInt("publisher")); news.setRedate(rs.getDate("redate")); news.setReviewer(rs.getInt("reviewer")); news.setTitle(rs.getString("title")); list.add(news); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (stat != null) stat.close(); if (conn != null) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return list; } public List listUsers(String sql, Object... params) { Connection conn = getConnection(); PreparedStatement stat = null; List list = new ArrayList (); try { stat = conn.prepareStatement(sql); if (params != null) { for (int i = 0; i < params.length; i++) { // stat.setString(i, params[i]); stat.setObject(i + 1, params[i]); } } ResultSet rs = stat.executeQuery(); while (rs.next()) { Users users = new Users(); users.setAccount(rs.getString("account")); users.setDeptid(rs.getInt("deptid")); users.setEmail(rs.getString("email")); users.setId(rs.getInt("id")); users.setLevel(rs.getString("level")); users.setPassword(rs.getString("password")); users.setUsername(rs.getString("username")); list.add(users); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (stat != null) stat.close(); if (conn != null) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return list; } public List listDept(String sql, Object... params) { Connection conn = getConnection(); PreparedStatement stat = null; List list = new ArrayList (); try { stat = conn.prepareStatement(sql); if (params != null) { for (int i = 0; i < params.length; i++) { // stat.setString(i, params[i]); stat.setObject(i + 1, params[i]); } } ResultSet rs = stat.executeQuery(); while (rs.next()) { Dept dept = new Dept(); dept.setId(rs.getInt("id")); dept.setName(rs.getString("name")); dept.setLevel(rs.getString("level")); dept.setState(rs.getString("state")); dept.setSuperdept(rs.getInt("superdept")); list.add(dept); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (stat != null) stat.close(); if (conn != null) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return list; } public List listComments(String sql, Object... params) { Connection conn = getConnection(); PreparedStatement stat = null; List list = new ArrayList (); try { stat = conn.prepareStatement(sql); if (params != null) { for (int i = 0; i < params.length; i++) { // stat.setString(i, params[i]); stat.setObject(i + 1, params[i]); } } ResultSet rs = stat.executeQuery(); while (rs.next()) { Comments comments = new Comments(); comments.setCommdate(rs.getDate("commdate")); comments.setCommentator(rs.getString("commentator")); comments.setCotent(rs.getString("cotent")); comments.setId(rs.getInt("id")); comments.setNewsid(rs.getInt("newsid")); list.add(comments); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (stat != null) stat.close(); if (conn != null) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return list; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)