javaWeb實現學生資訊管理系統,javaweb

來源:互聯網
上載者:User

javaWeb實現學生資訊管理系統,javaweb

本文為大家分享了javaWeb實現學生資訊管理系統,供大家參考,具體內容如下

初始版

初始版是沒有加分頁的。因為沒怎麼學過前端,介面很醜陋。主要技術:JSP,JavaBean,servlet,JDBC主要頁面如下:

登入頁面

首頁

新增學生

查看所有學生

查詢學生

工程目錄

資料庫

兩個表,user表和student表。為了使用DBUtils工具,一定要注意資料庫表的屬性的命名和JavaBean的get(),set() 方法的匹配。比如t_user表裡的uname,在JavaBean中是:private String uname,getUname(),setUname()。

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

小知識點

登入

登入時,使用者名稱或密碼輸入框是否為空白由登入頁面的js代碼判斷,二者都不為空白時,通過使用者名稱資訊查詢資料庫,如果找到使用者則成功登入,否則要判斷是使用者名稱錯誤還是密碼錯誤。這個事務在Service層處理,DAO層只負責通過使用者名稱尋找使用者。

UserService代碼:

public class UserService { private UserDao userDao = new UserDao(); public User query(User form) throws Exception{  User user = userDao.query(form);  //沒找到使用者  if(user == null){   throw new Exception("使用者名稱不存在");  }  //找到使用者了,但密碼不匹配  if(!form.getUpassword().equals(user.getUpassword())){   throw new Exception("密碼錯誤");  }  return user; }}

Filter過濾器

為了防止沒有登入的使用者也能直接存取其他頁面,需要寫一個過濾器。將登入頁面之外的所有頁面單獨放在一個users檔案夾下,當使用者登入成功時,將該使用者的資訊儲存到session的“sessionUser”屬性中,過濾器判斷這個屬性是否為空白,如果是空說明使用者登入不成功,不允許存取,直接轉到登入頁面,非空則允許存取。過濾器的主要代碼:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)    throws IOException, ServletException {  //1.得到session  //2.判斷session中是否有使用者,如果有,允許存取  //3.否則轉到登入頁面  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");  }   }

多條件組合查詢

在進階搜尋頁面,有四個選項可以選擇:學生姓名,性別,手機號,郵箱。四個條件可以任意排列組合。之前寫QT項目時也遇到過這個問題,當時用的就是拼接SQL語句,但是沒想到用“where 1=1”,很費事。下面的代碼很經典,並且用到了模糊查詢,使搜尋更人性化。

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

進化版:分頁

將查詢出來的分頁顯示,更加美觀。分頁形如:第N頁/共M頁 首頁 上一頁 1 2 3 4 5 6 7 8 9 10 下一頁 尾頁。

分頁效果如下:

分頁需要的資料:

當前頁:pageCode
總頁數:totalPage
總記錄數:totalRecord
每頁記錄數:pageSize
當前頁資料:beanList

PageBean

由於這些分頁資料總要在各層之間來回的傳遞!我們把這些分頁資料封裝到一個javabean中,它就叫分頁Bean,例如:PageBean。當使用多條件查詢後,再點擊第2 頁時,第2頁上的超連結沒有查詢條件,會丟失查詢條件,所以我們需要在頁面上的所有連結都要保留查詢條件!我們要把條件以一個字串的形式儲存到PageBean的url中!
代碼如下:

public class PageBean <T>{ private Integer pageCode;//當前頁碼 private Integer pageSize;//每頁資料大小 private Integer totalRecord;//總記錄 private List<T> beanList;//當前頁的記錄,定義成泛型為了以後直接用 private String url;//多條件組合查詢時url後的條件 //返回總頁數 public Integer getTotalPage(){  int tp = totalRecord/pageSize;  return totalRecord%pageSize==0 ? tp : tp+1; } ...//屬性的get,set方法 ...}

分頁在各層中的處理

jsp頁面:顯示顯示資料和“第N頁/共M頁 首頁 上一頁 1 2 3 4 5 6 7 8 9 10 下一頁 尾頁”;給servlet傳遞pageCode
servlet:建立PageBean對象,給PageBean所有的屬性賦值,然後給DAO層傳遞pageCode和pageSize;接受DAO返回的PageBean對象,儲存到request域,返回給頁面
service:充當中間商的角色,沒有事務需要處理
DAO:得到pageCode和pageSize,建立PageBean對象,查詢資料庫得到totalRecord和beanList,返回PageBean。

jsp頁面處理頁碼的代碼:

<center>第${pb.pageCode }頁/共${pb.totalPage }頁<a href="${pb.url }&pc=1" rel="external nofollow" > 首頁</a><c:if test="${pb.pageCode > 1 }"><a href="${pb.url }&pc=${pb.pageCode-1 }" rel="external nofollow" >上一頁</a></c:if><!-- 頁碼錶 計算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>  <%-- 計算公式 --%>  <c:set var="begin" value="${pb.pageCode-5 }"></c:set>  <c:set var="end" value="${pb.pageCode+4 }"></c:set>  <%--- 頭溢出 --%>  <c:if test="${begin<1 }">   <c:set var="begin" value="1"></c:set>   <c:set var="end" value="10"></c:set>  </c:if>  <%-- 尾溢出 --%>  <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><%-- 迴圈顯示頁碼列表 --%><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 }">下一頁</a></c:if><a href="${pb.url }&pc=${pb.totalPage }">尾頁</a></center>

加完分頁之後的多條件組合查詢代碼

public PageBean<Student> query(Student s,int pc,int ps){ try{  PageBean<Student> pb = new PageBean<Student>();  pb.setPageCode(pc);  pb.setPageSize(ps);  /*   * 查詢出總記錄   */  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);  /*   * 得到beanList結果集   */  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); }}

更多學習資料請關注專題《管理系統開發》。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.