current position:Home>Java project: simple message board (java + SSM + MySQL + JSP)

Java project: simple message board (java + SSM + MySQL + JSP)

2022-01-27 05:07:15 qq_ one billion three hundred and thirty-four million six hundr

The main functions are : Student record management 、 Class message management 、 Personal information management 、 Class album management .

Running environment :mysql5.7 tomcat7.0/8.5 eclipse navicat jdk1.8

 

 

 

 

User control layer :

@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", " Login failed , Account password does not match ");
            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.  To visit dao ,  See if the login is satisfied .

            Users Users = usersDao.userlogin(userName, password);

            // 3.  in the light of dao Return result of , To respond to 
            if (Users != null) {


                request.getSession().setAttribute("usersLogin", Users);
                CategoryDao categoryDao = new CategoryDao();
                List<Category> categoryList = categoryDao.queryAll();
                request.setAttribute("categoryList", categoryList.stream().filter(x -> x.getState().equals("1")).collect(Collectors.toList()));
                return "index";


            } else {
                request.setAttribute("error", " Wrong user name or password !");
                return "login";


            }
        } else {
            request.setAttribute("error", " Login failed , The verification code is incorrect !");
            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", " Registration failed , The password does not match the confirmation password !");
            return "reg";
        } else {

            boolean isSuccess = usersDao.isReg(account);

            if (!isSuccess) {

                request.setAttribute("error", " Registration failed , The user name already exists !");
                return "reg";

            } else {
                usersDao.reg(username, account, password, email);
                request.setAttribute("error", " Registered successfully !");

                return "reg";
            }
        }

    }

    @RequestMapping("/listforadmin")
    public String listforadmin() {


            List<Users> list = usersDao.getUsers();
            request.setAttribute("list", list);
            return "listusers";

    }
    @RequestMapping("/del")
    public String del(Integer id) {

        usersDao.del(id);

        List<Users> list = usersDao.getUsers();
        request.setAttribute("list", list);
        return "listusers";

    }
}

Database connection tools :


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;
		}

	}

	/**
	 * ��ɾ��ͨ��
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	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 List<Category> listCategory(String sql, Object... params) {
		Connection conn = getConnection();
		PreparedStatement stat = null;
		List<Category> list = new ArrayList<Category>();
		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<News> listNews(String sql, Object... params) {
		Connection conn = getConnection();
		PreparedStatement stat = null;
		List<News> list = new ArrayList<News>();
		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()) {
				/**
				 * CREATE TABLE `news` ( `id` int(11) NOT NULL AUTO_INCREMENT,
				 * `title` varchar(100) NOT NULL, `categoryid` int(11) DEFAULT
				 * NULL, `publisher` int(11) DEFAULT NULL, `pbdeptid` int(11)
				 * DEFAULT NULL, `pbdate` datetime DEFAULT NULL, `reviewer`
				 * int(11) DEFAULT NULL, `redate` datetime DEFAULT NULL,
				 * `clicks` int(11) NOT NULL DEFAULT '0', `content` text,
				 * PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
				 */
				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<Users> listUsers(String sql, Object... params) {
		Connection conn = getConnection();
		PreparedStatement stat = null;
		List<Users> list = new ArrayList<Users>();
		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();
				/**
				 * CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT,
				 * `username` varchar(20) NOT NULL, `account` varchar(20) NOT
				 * NULL, `password` varchar(10) NOT NULL, `email` varchar(50)
				 * DEFAULT NULL, `level` varchar(10) DEFAULT NULL, `deptid`
				 * int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT
				 * CHARSET=utf8
				 */
				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<Dept> listDept(String sql, Object... params) {
		Connection conn = getConnection();
		PreparedStatement stat = null;
		List<Dept> list = new ArrayList<Dept>();
		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();
				/**
				 * CREATE TABLE `dept` ( `id` int(11) NOT NULL AUTO_INCREMENT,
				 * `name` varchar(50) NOT NULL, `superdept` int(11) DEFAULT
				 * NULL, `level` varchar(20) DEFAULT NULL, `state` char(1)
				 * DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT
				 * CHARSET=utf8
				 */
				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<Comments> listComments(String sql, Object... params) {
		Connection conn = getConnection();
		PreparedStatement stat = null;
		List<Comments> list = new ArrayList<Comments>();
		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();
				/**
				 * CREATE TABLE `comments` ( `id` int(11) NOT NULL
				 * AUTO_INCREMENT, `commentator` varchar(20) NOT NULL, `newsid`
				 * int(11) DEFAULT NULL, `commdate` datetime NOT NULL, `cotent`
				 * text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT
				 * CHARSET=utf8
				 */
				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;
	}

}

copyright notice
author[qq_ one billion three hundred and thirty-four million six hundr],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270507140952.html

Random recommended