JavaWeb implements the student information management system, javaweb

Source: Internet
Author: User

JavaWeb implements the student information management system, javaweb

This article shares with you the implementation of the student information management system on javaWeb for your reference. The details are as follows:

Initial version

The initial version is not paginated. Because I have never learned the frontend, the interface is ugly. Main technologies:JSP, JavaBean, servlet, JDBCThe main page is as follows:

Logon page

Home Page

Add student

View All students

Query students

Project catalog

Database

Two tables: user table and student table. To use the DBUtils tool, you must pay attention to the name of the database table attributes and the matching of the get () and set () Methods of the JavaBean. For example, the uname in the t_user table is private String uname, getUname (), and setUname () in the JavaBean table ().

CREATE TABLE t_user( uid  CHAR(32) PRIMARY KEY, uname  VARCHAR(40) NOT NULL, upassword  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));

Small knowledge point

Login

When you log on, whether the user name or password input box is empty is determined by the js Code on the logon page. If neither of them is empty, you can query the database using the user name information. If you find a user, you can log on successfully, otherwise, you must determine whether the user name or password is incorrect. This transaction is processed at the Service layer, and the DAO layer is only responsible for finding users through user names.

UserService code:

Public class UserService {private UserDao userDao = new UserDao (); public User query (User form) throws Exception {User user User = userDao. query (form); // user not found if (user = null) {throw new Exception ("user name does not exist");} // user found, but the password does not match if (! Form. getUpassword (). equals (user. getUpassword () {throw new Exception ("Incorrect password") ;}return user ;}}

Filter

To prevent users without logon from directly accessing other pages, you need to write a filter. Put all the pages outside the logon page in a single users folder. When the user logs on successfully, save the user information to the "sessionUser" attribute of the session, the filter determines whether this attribute is null. If it is null, the user fails to log on. If it is not allowed, it is directly transferred to the logon page. If it is not empty, the user is allowed. The main code of the filter:

Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {// 1. get session // 2. determine whether a user exists in the session. If yes, release it. // 3. otherwise, go to the logon page to HttpServletRequest req = (HttpServletRequest) request; User 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

The advanced search page has four options: Student name, gender, mobile phone number, and email. The four conditions can be arranged and combined in any order. I also encountered this problem when I was writing the QT project. I used to splice SQL statements, but I didn't expect to use "where 1 = 1", which is very troublesome. The following code is classic and uses fuzzy search to make search more user-friendly.

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

Evolutionary version: Paging

Display the queried pages in a more beautiful way. Page shape: Page N/Page M homepage Previous Page 1 2 3 4 5 6 7 8 9 10 next page last page.

The paging effect is as follows:

Data required for paging:

Current page: pageCode
Total number of pages: totalPage
Total number of records: totalRecord
Number of records per page: pageSize
Current page data: beanList

PageBean

Because the paging data is always transferred back and forth between layers! We encapsulate the paging data into a javabean, which is called a paging Bean, for example, PageBean. When multi-condition query is used and page 2nd is clicked, the hyperlink on page 2nd does not have query conditions, and the query conditions are lost, therefore, we need to retain the query conditions for all links on the page! We need to save the condition as a string to the url of PageBean!
The Code is as follows:

Public class PageBean <T> {private Integer pageCode; // private Integer pageSize of the current page; // private Integer totalRecord of data size per page; // private List of total records <T> beanList; // The record on the current page is defined as a wildcard to directly use the private String url in the future; // The condition after the url when multiple conditions are combined for query // The total number of returned pages public Integer getTotalPage () {int tp = totalRecord/pageSize; return totalRecord % pageSize = 0? Tp: tp + 1;}... // get attribute, set method ...}

Paging processing at each layer

Jsp page: displays data and "Page N/Page M homepage Previous Page 1 2 3 4 5 6 7 8 9 10 next page"; transmits pageCode to servlet
Servlet: Create a PageBean object, assign values to all attributes of PageBean, and pass pageCode and pageSize to the DAO layer. Accept the PageBean object returned by DAO, save it to the request field, and return it to the page.
Service: acts as a broker and no transactions need to be processed
DAO: Get pageCode and pageSize, create a PageBean object, query the database to get totalRecord and beanList, and return PageBean.

Code for processing the page number of a jsp page:

<Center> No. $ {pb. pageCode} page/$ {pb in total. totalPage page <a href = "$ {pb. url} & pc = 1 "rel =" external nofollow "> homepage </a> <c: if test =" $ {pb. pageCode> 1} "> <a href =" $ {pb. url} & pc =$ {pb. pageCode-1} "rel =" external nofollow "> previous </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> <% -- 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> <% -- display the page number list cyclically -- %> <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>

Code for multi-condition combination query after 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 records */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 = (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 information, see management system development.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.