This article shares with you the JavaWeb implementation of student information management system for your reference. The specific content is as follows
Initial Edition
There are no additional pages for the initial version. Because I haven't learned much about the front-end, the interface is ugly. Main technologies: JSP, JavaBean, servlet, JDBC main pages are as follows:
Login page
Home page
Add students
View all students
Inquiry of students
Project Directory
database
Two tables, user table and student table. In order to use the DBUtils tool, be sure to pay attention to the matching of the attribute naming of the database table and the get() and set() methods of JavaBean. For example, the uname in the t_user table is: private String uname, getUname(), setUname() in JavaBean.
CREATE TABLE t_user( uid CHAR(32) PRIMARY KEY, uname VARCHAR(40) NOT NULL, uppassword VARCHAR(40) NOT NULL);
CREATE TABLE t_student( sid CHAR(32) PRIMARY KEY, sname VARCHAR(40) NOT NULL, gender VARCHAR(6) NOT NULL, birthday CHAR(10), tellphone VARCHAR(15) NOT NULL, email VARCHAR(40), description VARCHAR(500));
A little knowledge point
Log in
When logging in, whether the user name or password input box is empty is judged by the js code of the login page. When neither is empty, query the database through the user name information. If the user is found, login will be successfully logged in, otherwise it is necessary to determine whether it is a user name or a password error. This transaction is processed at the Service layer, and the DAO layer is only responsible for finding users through usernames.
UserService code:
public class UserService { private UserDao userDao = new UserDao(); public User query(User form) throws Exception{ User user = userDao.query(form); //User not found if(user == null){ throw new Exception("User name does not exist"); } //The user was found, but the password did not match if(!form.getUpassword().equals(user.getUpassword()))){ throw new Exception("Password error"); } return user; }}Filter filter
In order to prevent users who are not logged in from being able to directly access other pages, a filter is needed. Place all pages outside the login page separately in a users folder. When the user logs in successfully, save the user's information in the session's "sessionUser" property. The filter determines whether this property is empty. If it is empty, it means that the user login is unsuccessful and will not release it. Go directly to the login page. If it is not empty, release it. The main code of the filter:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //1. Get session //2. Determine whether there is a user in the session. If so, release //3. Otherwise, go to the login page HttpServletRequest req = (HttpServletRequest)request; User user = (User)req.getSession().getAttribute("sessionUser"); if(user != null){ chain.doFilter(request, response); }else{ HttpServletResponse resp = (HttpServletResponse)response; resp.sendRedirect(req.getContextPath() + "/index.jsp"); } }Multi-condition combination query
On the Advanced Search page, there are four options to choose: student name, gender, mobile phone number, and email address. The four conditions can be arranged and combined arbitrarily. I encountered this problem before when writing QT projects. I used splicing SQL statements at that time, but I didn't expect that using "where 1=1" would be very troublesome. The following code is classic and uses fuzzy queries to make the search more humane.
public List<Student> query(Student s){ try{ StringBuilder sql = new StringBuilder("SELECT * FROM t_student WHERE 1=1"); List<Object> params = new ArrayList<Object>(); if(s.getSname() != null && !s.getSname().trim().isEmpty()){ sql.append(" and sname like ?"); params.add("%" + s.getSname() + "%"); } if(s.getGender() != null && !s.getGender().trim().isEmpty()){ sql.append(" and gender=?"); params.add(s.getGender()); } if(s.getTellphone() != null && !s.getTellphone().trim().isEmpty()){ sql.append(" and tellphone like ?"); params.add("%" + s.getTellphone() + "%"); } if(s.getEmail() != null && !s.getEmail().trim().isEmpty()){ sql.append(" and email like ?"); params.add("%" + s.getEmail() + "%"); } return qr.query(sql.toString(), new BeanListHandler<Student>(Student.class), params.toArray()); }catch (Exception e) { throw new RuntimeException(e); }}Evolution Edition: Pagination
Display the query pages to make them more beautiful. The page is like: Page N/Total M Home Page Previous Page 1 2 3 4 5 6 7 8 9 10 Next Page Last Page.
The pagination effect is as follows:
Data required for pagination:
Current page: pageCode
Total Pages: totalPage
Total record count: totalRecord
Number of records per page: pageSize
Current page data: beanList
PageBean
Since these paged data must always be passed back and forth between layers! We encapsulate these paged data into a javabean, which is called a paged Bean, such as PageBean. When using multi-condition query and clicking on page 2, the hyperlink on page 2 does not have query conditions, and the query conditions will be lost, so we need to keep the query conditions on all links on the page! We want to save the condition as a string to the url of the PageBean!
The code is as follows:
public class PageBean <T>{ private Integer pageCode;//current page number private Integer pageSize;//Data size per page private Integer totalRecord;//Total records private List<T> beanList;//The record of the current page is defined as a generic. In order to use private String url directly in the future;//The conditions after url are combined in multiple conditions//Return the total number of pages public Integer getTotalPage(){ int tp = totalRecord/pageSize; return totalRecord%pageSize==0 ? tp : tp+1; } ...//The get, set method of the attribute...}Processing of paging in each layer
jsp page: display display data and "Page N/Total M pages Previous page 1 2 3 4 5 6 7 8 9 10 Next page Last page"; pass pageCode to servlet
servlet: Create a PageBean object, assign values to all attributes of PageBean, and then pass pageCode and pageSize to the DAO layer; accept the PageBean object returned by DAO, save it to the request field, and return to the page
service: acts as a middleman, no transactions need to be processed
DAO: Get pageCode and pageSize, create PageBean object, query the database to get totalRecord and beanList, and return PageBean.
The code for jsp page processing page number:
<center>Page${pb.pageCode } Page/Total${pb.totalPage } Page<a href="${pb.url }&pc=1" rel="external nofollow" > Home Page</a><c:if test="${pb.pageCode > 1 }"><a href="${pb.url }&pc=${pb.pageCode-1 }" rel="external nofollow" > Previous page</a></c:if><!-- Page number table calculation begin end--><c:choose> <c:when test="${pb.totalPage<=10 }"> <c:set var="begin" value="1"></c:set> <c:set var="end" value="${pb.totalPage }"></c:set> </c:when> <c:otherwise> <%-- Calculation formula--%> <c:set var="begin" value="${pb.pageCode-5 }"></c:set> <c:set var="end" value="${pb.pageCode+4 }"></c:set> <%--- Header overflow--%> <c:if test="${begin<1 }"> <c:set var="begin" value="1"></c:set> <c:set var="end" value="10"></c:set> </c:if> <%-- Tail overflow--%> <c:if test="${end> pb.totalPage}"> <c:set var="begin" value="${pb.totalPage-9 }"></c:set> <c:set var="end" value="${pb.totalPage }"></c:set> </c:if> </c:otherwise></c:choose><%-- Looping page number list--%><c:forEach var="i" begin="${begin }" end="${end }"> <c:choose> <c:when test="${i eq pb.pageCode }"> [${i }] </c:when> <c:otherwise> <a href="${pb.url }&pc=${i }">[${i }]</a> </c:otherwise> </c:choose> </c:forEach><c:if test="${pb.pageCode < pb.totalPage }"><a href="${pb.url }&pc=${pb.pageCode+1 }">Next page</a></c:if><a href="${pb.url }&pc=${pb.totalPage }">Last page</a></center>Multi-condition combination query code after adding pagination
public PageBean<Student> query(Student s,int pc,int ps){ try{ PageBean<Student> pb = new PageBean<Student>(); pb.setPageCode(pc); pb.setPageSize(ps); /* * Query the total record*/ StringBuilder numSql = new StringBuilder("SELECT COUNT(*) FROM t_student"); StringBuilder whereSql = new StringBuilder("WHERE 1=1"); List<Object> params = new ArrayList<Object>(); if(s.getSname() != null && !s.getSname().trim().isEmpty()){ whereSql.append(" and sname like ?"); params.add("%" + s.getSname() + "%"); } if(s.getGender() != null && !s.getGender().trim().isEmpty()){ whereSql.append(" and gender=?"); params.add(s.getGender()); } if(s.getTellphone() != null && !s.getTellphone().trim().isEmpty()){ whereSql.append(" and tellphone like ?"); params.add("%" + s.getTellphone() + "%"); } if(s.getEmail() != null && !s.getEmail().trim().isEmpty()){ whereSql.append(" and email like ?"); params.add("%" + s.getEmail() + "%"); } Number number = (Number)qr.query(numSql.append(whereSql).toString(), new ScalarHandler(),params.toArray()); int totalRecord = number.intValue(); pb.setTotalRecord(totalRecord); /* * Get the beanList result set*/ StringBuilder sql = new StringBuilder("SELECT * FROM t_student"); StringBuilder limitSql = new StringBuilder(" limit ?,?"); params.add((pc-1)*ps); params.add(ps); List<Student> students = qr.query(sql.append(whereSql).append(limitSql).toString(), new BeanListHandler<Student>(Student.class),params.toArray()); pb.setBeanList(students); return pb; }catch (Exception e) { throw new RuntimeException(e); }}For more learning materials, please pay attention to the special topic "Management System Development".
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.