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
The sidebar is recommended
- Spring IOC container loading process
- [thinking] the difference between singleton mode and static method - object-oriented programming
- Hadoop environment setup (MySQL environment configuration)
- 10 minutes, using node JS creates a real-time early warning system for bad weather!
- Git tool
- Force deduction algorithm - 92 Reverse linked list II
- What is the sub problem of dynamic programming?
- C / C + +: static keyword summary
- Idea does not have the artifacts option when configuring Tomcat
- Anaconda can't open it
guess what you like
-
I don't know how to start this
-
Matlab simulation of transportation optimization algorithm based on PSO
-
MySQL slow log optimization
-
[Vue] as the window is stretched (larger, smaller, wider and higher), the text will not be displayed
-
Popular Linux distributions for embedded computing
-
Suzhou computer research
-
After installing SSL Certificate in Windows + tomcat, the domain name request is not successful. Please answer!!
-
Implementation time output and greetings of jQuery instance
-
The 72 year old uncle became popular. Wu Jing and Guo fan made his story into a film, which made countless dreamers blush
-
How to save computer research
Random recommended
- Springboot implements excel import and export, which is easy to use, and poi can be thrown away
- The final examination subjects of a class are mathematical programming, and the scores are sorted and output from high to low
- Two pronged approach, Tsinghua Professor Pro code JDK and hotspot source code notes, one-time learning to understand
- C + + recursive knapsack problem
- The use of GIT and GitHub and the latest git tutorial are easy to understand -- Video notes of crazy God speaking
- PostgreSQL statement query
- Ignition database test
- Context didn't understand why he got a high salary?, Nginxfair principle
- Bootstrap switch switch control user's guide, springcloud actual combat video
- A list that contains only strings. What other search methods can be used except sequential search
- [matlab path planning] multi ant colony algorithm grid map path planning [including GUI source code 650]
- [matlab path planning] improved genetic algorithm grid map path planning [including source code phase 525]
- Iinternet network path management system
- Appium settings app is not running after 5000ms
- Reactnative foundation - 07 (background image, status bar, statusbar)
- Reactnative foundation - 04 (custom rpx)
- If you want an embedded database (H2, hsql or Derby), please put it on the classpath
- When using stm32g070 Hal library, if you want to write to flash, you must perform an erase. If you don't let it, you can't write continuously.
- Linux checks where the software is installed and what files are installed
- SQL statement fuzzy query and time interval filtering
- 69. Sqrt (x) (c + + problem solving version with vs runnable source program)
- Fresh students are about to graduate. Do you choose Java development or big data?
- Java project: OA management system (java + SSM + bootstrap + MySQL + JSP)
- Titanic passenger survival prediction
- Vectorization of deep learning formula
- Configuration and use of private image warehouse of microservice architect docker
- Relearn JavaScript events
- For someone, delete return 1 and return 0
- How does Java dynamically obtain what type of data is passed? It is used to judge whether the data is the same, dynamic data type
- How does the database cow optimize SQL?
- [data structure] chain structure of binary tree (pre order traversal) (middle order traversal) (post order traversal) (sequence traversal)
- Webpack packaging optimization solution
- 5. Operation element
- Detailed explanation of red and black trees
- redhat7. 9 install database 19C
- Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)
- Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path
- How to solve the run-time exception of test times
- Detailed explanation of k8s management tool kubectl
- Android system view memory command