Javaweb门生信息管理系统(Mysql+JSP+MVC+CSS)

饭宝  金牌会员 | 2024-6-14 23:42:45 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 895|帖子 895|积分 2685

项目源码及数据库:
链接:https://pan.baidu.com/s/1ktUyxbOI9lljWr-HRTRIiQ?pwd=1024
提取码:1024
 
目录
一.项目介绍
二.运行效果
1.登录界面
2.主界面(点击学号修改门生信息)
3.增加门生界面
​编辑
三.项目目录结构
 四.代码展示
1.jsp及css代码
 ①登录界面代码(login.jsp)
②登录界面css(login.css)
③注册用户界面(register.jsp)
④注册用户界面css(index.css)
⑤修改密码界面(UpdateLoginID.jsp)
⑥注销用户界面(DeleteLoginID.jsp)
⑦登录成功主界面(index.jsp)
⑧登陆成功界面css(show.css)
⑨点击学号,修改门生信息界面(StudentInfo.jsp)
⑩增加门生信息(add.jsp)
⑪上传作业操纵(UpAndDown.jsp)
2.三层架构
①表示层Servlet
②业务逻辑层Service
③数据访问层Dao
④通用的数据库操纵(DBUtils.java)
3.JavaBean封装数据
①分页资助类(Page.java)
②封装门生信息(Student.java)
4.项目所需jar包
五.数据库表格
①登录注册表格login
②门生信息表格student1



一.项目介绍

本系统重要实现对基于Javaweb门生信息管理系统所需的各项基本功能,能够对门生信息举行增删改查等功能,并可以实现用户注册、用户登陆等功能。
数据库:Mysql
开辟工具:Eclipse
开辟情况:JDK+Tomcat

二.运行效果

1.登录界面


2.主界面(点击学号修改门生信息)


3.增加门生界面



三.项目目录结构



 四.代码展示

1.jsp及css代码

 ①登录界面代码(login.jsp)

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.         pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <link href="css/login.css" type="text/css" rel="stylesheet">
  7. <meta charset="UTF-8">
  8. <title>学生信息管理系统-登录界面</title>
  9. </head>
  10. <body>
  11.         <form action="CheckLoginServlet" method="post">
  12.                 <div class="content">
  13.                         <div class="wrap">
  14.                                 <div class="login-box">
  15.                                         <div class="login-frame">
  16.                                                 <h2>
  17.                                                         用户登录 <a href="register.jsp"> 立即注册</a>
  18.                                                 </h2>
  19.                                                 <div class=item>
  20.                                                         <input type="text" name="UID" placeholder="用户名">
  21.                                                 </div>
  22.                                                 <div class=item>
  23.                                                         <input type="password" name="upwd" placeholder="密码">
  24.                                                 </div>
  25.                                                 <input type="submit" class="login-btn" value="登 录">
  26.                                                 <div class=item1>
  27.                                                         <a href="UpdateLoginPwd.jsp"> 修改密码</a>
  28.                                                         <a        href="DeleteLoginID.jsp"> 注销用户</a>
  29.                                                 </div>
  30.                                                 <%
  31.                                                         String error = (String) request.getAttribute("error");
  32.                                                         String error0 = (String) request.getAttribute("error0");
  33.                                                         String error1 = (String) request.getAttribute("error1");
  34.                                                         String error2 = (String) request.getAttribute("error2");
  35.                                                         String error3 = (String) request.getAttribute("error3");
  36.                                                         String error4 = (String) request.getAttribute("error4");
  37.                                                         if (error != null) {
  38.                                                                 if (error.equals("loginError")) {
  39.                                                                         out.println("用户名或密码错误!登录失败!");
  40.                                                                 } else if (error.equals("nologinError")) {
  41.                                                                         response.sendRedirect("QueryStudentByPageServlet");
  42.                                                                 }
  43.                                                         }
  44.                                                         if (error0 != null) {
  45.                                                                 if (error0.equals("loginError")) {
  46.                                                                         out.println("用户名或密码错误!修改失败!");
  47.                                                                 } else if (error0.equals("nologinError")) {
  48.                                                                         if (error1 != null) {
  49.                                                                 if (error1.equals("noupdateError")) {
  50.                                                                         out.println("密码修改成功!");
  51.                                                                 }
  52.                                                                         }
  53.                                                                 }
  54.                                                         }
  55.                                                         if (error2 != null) {
  56.                                                                 if (error2.equals("loginError")) {
  57.                                                                         out.println("用户名或密码错误!注销失败!");
  58.                                                                 } else if (error2.equals("nologinError")) {
  59.                                                                         if (error3 != null) {
  60.                                                                 if (error3.equals("nodeleteError")) {
  61.                                                                         out.println("账户注销成功!");
  62.                                                                 }
  63.                                                                         }
  64.                                                                 }
  65.                                                         }
  66.                                                         if (error4 != null) {
  67.                                                                 if (error4.equals("noaddError")) {
  68.                                                                         out.println("账户注册成功!");
  69.                                                                 }
  70.                                                         }
  71.                                                         %>
  72.                                                 </div>
  73.                                         </div>
  74.                                 </div>
  75.                         </div>
  76.         </form>
  77. </body>
  78. </html>
复制代码
②登录界面css(login.css)

  1. *{margin:0;padding:0;}
  2. a{text-decoration:none;color:#666;}
  3. a:hover{
  4.         text-decoration:underline;
  5.         color:E4393C;
  6. }
  7. html,body
  8. {
  9.         font:12px/150% Arial,Verdana;
  10. }
  11. .wrap{
  12.         width:1000px;
  13.         margin:0 auto;
  14. }
  15. .left{
  16.         float:left;
  17. }
  18. .content{
  19.         background:url(../image/login.jpg);
  20.         background-size: cover;
  21.         width:1280px;
  22.         height:559px;
  23. }
  24. .login-frame{
  25.         margin:50px 5% 50px 5%;
  26.         float:right;
  27.         padding:60px;
  28.         background:white;
  29.         background-color:rgba(255,255,255,0.9);
  30.         border-radius:25px;
  31.         order-right:1px #bdbdbd solid;
  32.         width:280px;
  33.         height:230px;
  34. }
  35. .login-frame h2{
  36.         font-size:25px;
  37.         height:40px;
  38.         margin-buttom:25px;
  39. }
  40. .login-frame h2 a{
  41.         font-size:15px;
  42.         color:#59c2c5;
  43.         padding-left:20px;
  44.         background:url(../image/icon5.jpg)no-repeat;
  45. }
  46. .login-frame .item{
  47.         height:60px;
  48.         margin-buttom:40px;
  49. }
  50. .login-frame .item input{
  51.         line-height:40px;
  52.         width:260px;
  53.         border:none;
  54.         border-bottom: 1px solid #59c2c5;
  55. }
  56. .login-btn{
  57.         display:block;
  58.         height:50px;display:block;
  59.         height:50px;
  60.         color:#fff;
  61.         background:#59c2c5;
  62.         width:265px;
  63.         font-size:16px;
  64.         line-height:30px;
  65.         text-align:center;
  66.         border-radius:10px;
  67.         border:none;
  68.         color:#fff;
  69.         background:#59c2c5;
  70.         width:265px;
  71.         font-size:16px;
  72.         line-height:30px;
  73.         text-align:center;
  74.         border-radius:10px;
  75.         border:none;
  76. }
  77. .login-frame .item1{
  78.         dislpay:flex;
  79.         justify-content: space-between;
  80.         margin-top:1 rem;
  81. }
  82. .login-frame .item1 a{
  83.         line-height:40px;
  84.         font-size:1.1rem;
  85.         margin-top:5 rem;
  86.         padding:1rem 3rem;
  87. }
复制代码
③注册用户界面(register.jsp)

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.     pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <link href="css/index.css" type="text/css" rel="stylesheet">
  7. <meta charset="UTF-8">
  8. <script type = "text/javascript" src = "js/jquery-3.6.0.js"></script>
  9.        
  10.         <script type = "text/javascript">
  11.         function check()
  12.         {
  13.                 var UID = $("#UID").val();
  14.                 var upwd = $("#upwd").val();
  15.                 var upwd1 = $("#upwd1").val();
  16.                 if(upwd != upwd1){
  17.                         alert("两次输入的密码不一致,请重新输入!");
  18.                         return false;
  19.                 }
  20.                
  21.                 return true;
  22.         }
  23.          $(document).ready(function(){
  24.          });
  25.         </script>
  26. <title>注册账号</title>
  27. </head>
  28. <body>
  29.         <form action = "AddLoginIDServlet" method = "post" onsubmit = "return check()">
  30.         <div class = "content">
  31.                 <div class = "box">
  32.                 <div class = "item1">
  33.                 <h2>用户注册</h2>
  34.                 </div>
  35.                 <div class = "item">
  36.                 <input type = "text" name = "UID" id = "UID" placeholder="账号"/><br/>
  37.                 </div>
  38.                 <div class = "item">
  39.                 <input type = "password" name = "upwd" id = "upwd" placeholder="密码"/><br/>
  40.                 </div>
  41.                 <div class = "item">
  42.                 <input type = "password" name = "upwd1" id = "upwd1" placeholder="确认密码"/><br/>
  43.                 </div>
  44.                 <input type = "submit" class = "btn" value = "注册"/><br/>
  45.                 <a href = "login.jsp">返回</a>
  46.                 <%
  47.                 String error4 = (String) request.getAttribute("error4");
  48.                 if (error4!= null) {
  49.                 if (error4.equals("addError")) {
  50.                         out.println("注册失败!账户名已经存在!");
  51.                 }
  52.         }
  53.         %>
  54.         </div>
  55.         </div>
  56.         </form>
  57.        
  58. </body>
  59. </html>
复制代码
④注册用户界面css(index.css)

  1. *{margin:0;padding:0;}
  2. html,body
  3. {
  4.         font:12px/150% Arial,Verdana;
  5. }
  6. .content{
  7.         background:url(../image/login.jpg);
  8.         background-size: cover;
  9.         width:1280px;
  10.         height:559px;
  11. }
  12. .box{
  13.         margin:60px 18% 60px 18%;
  14.         float:right;
  15.         padding:30px;
  16.         background:white;
  17.         background-color:rgba(255,255,255,0.9);
  18.         border-radius:15px;
  19.        
  20. }
  21. .item{
  22.         height:60px;
  23.         margin-buttom:40px;
  24. }
  25. .item input{
  26.         line-height:40px;
  27.         width:260px;
  28.         border:none;
  29.         border-bottom: 1px solid #59c2c5;
  30.         border-radius:3px;
  31. }
  32. .item1{
  33.         font-size:15px;
  34.         height:40px;
  35. }
  36. .btn{
  37.         display:block;
  38.         height:50px;
  39.         color:#fff;
  40.         background:#59c2c5;
  41.         width:265px;
  42.         font-size:16px;
  43.         line-height:30px;
  44.         text-align:center;
  45.         border-radius:10px;
  46.         border:none;
  47. }
复制代码
⑤修改密码界面(UpdateLoginID.jsp)

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.     pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <link href="css/index.css" type="text/css" rel="stylesheet">
  7. <meta charset="UTF-8"><script type = "text/javascript" src = "js/jquery-3.6.0.js"></script>
  8.        
  9.         <script type = "text/javascript">
  10.         function check()
  11.         {
  12.                 var upwd = $("#upwd").val();
  13.                 var upwd1 = $("#upwd1").val();
  14.                 var upwd2 = $("#upwd2").val();
  15.                 if(upwd1 != upwd2){
  16.                         alert("两次输入的密码不一致,请重新输入!");
  17.                         return false;
  18.                 }
  19.                
  20.                 return true;
  21.         }
  22.          $(document).ready(function(){
  23.          });
  24.         </script>
  25. <title>更改账号密码</title>
  26. </head>
  27. <body>
  28.         <form action = "UpdateLoginPwdServlet" method = "post" onsubmit = "return check()">
  29.         <div class = "content">
  30.         <div class = "box">
  31.         <div class= "item1"><h2>修改密码</h2></div>
  32.         <div class = item>
  33.         <input type = "text" name = "UID" id = "UID" placeholder="账号"/><br/>
  34.         </div>
  35.         <div class = item>
  36.         <input type = "password" name = "upwd" id = "upwd" placeholder="旧密码"/><br/>
  37.         </div>
  38.         <div class = item>
  39.         <input type = "password" name = "upwd1" id = "upwd1" placeholder="新密码"/><br/>
  40.         </div>
  41.         <div class = item>
  42.         <input type = "password" name = "upwd2" id = "upwd2" placeholder="确认密码"/><br/>
  43.         </div>
  44.         <input type = "submit" class = "btn" value = "提交"/><br/>
  45.         <a href = "login.jsp">返回</a>
  46.         <%
  47.         String error0 = (String) request.getAttribute("error0");
  48.         if (error0!= null) {
  49.                 if (error0.equals("loginError")) {
  50.                         out.println("用户名或密码错误,请重新输入!");
  51.                 } else if (error0.equals("nologinError")) {
  52.                         response.sendRedirect("login.jsp");
  53.                 }
  54.         }
  55.         %>
  56.         </div>
  57.         </div>
  58.         </form>
  59. </body>
  60. </html>
复制代码
⑥注销用户界面(DeleteLoginID.jsp)

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.     pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <link href="css/index.css" type="text/css" rel="stylesheet">
  7. <meta charset="UTF-8">
  8. <title>注销账号</title>
  9. </head>
  10. <body>
  11.         <form action = "DeleteLoginIDServlet" method = "post">
  12.         <div class = "content">
  13.         <div class = "box">
  14.         <div class = "item1">
  15.         <h2>注销账号</h2>
  16.         </div>
  17.         <div class = "item">
  18.         <input type = "text" name = "UID" id = "UID" placeholder="账号"/><br/>
  19.         </div>
  20.         <div class = "item">
  21.         <input type = "password" name = "upwd" id = "upwd" placeholder="密码"/><br/>
  22.         </div>
  23.         <input type = "submit" class = "btn" value = "注销"/><br/>
  24.         <a href = "login.jsp">返回</a>
  25.         <%
  26.         String error2 = (String) request.getAttribute("error2");
  27.         if (error2!= null) {
  28.         if (error2.equals("loginError")) {
  29.                         out.println("用户名或密码错误!注销失败!");
  30.                 }
  31.         }
  32.                 %>
  33.         </div>
  34.         </div>
  35.         </form>
  36. </body>
  37. </html>
复制代码
⑦登录成功主界面(index.jsp)

点击学号进入单个门生信息展示界面,可修改门生信息
点击删除删除门生信息
  1. <%@page import="student.entity.Page"%>
  2. <%@page import="java.util.List"%>
  3. <%@page import="student.entity.Student"%>
  4. <%@ page language="java" contentType="text/html; charset=UTF-8"
  5.     pageEncoding="UTF-8"%>
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9.         <script type = "text/javascript" src = "js/jquery-3.6.0.js"></script>
  10.        
  11.         <script type = "text/javascript">
  12.          $(document).ready(function(){
  13.                  $("tr:odd").css("background-color","lightgrey");
  14.          
  15.          });
  16.         </script>
  17.        
  18. <link href="css/show.css" type="text/css" rel="stylesheet">
  19. <meta charset="UTF-8">
  20. <title>学生信息列表</title>
  21. </head>
  22. <body>
  23.         <div class = "content">
  24.         <div class = "box">
  25.         <table  border="2" cellspacing="0">
  26.                 <tr>
  27.                         <th><h2>学号</h2></th>
  28.                         <th><h2>姓名</h2></th>
  29.                         <th><h2>年龄</h2></th>
  30.                         <th><h2>专业</h2></th>
  31.                         <th><h2>操作</h2></th>
  32.                 </tr>
  33.                 <%
  34.                 Page page1 = (Page)request.getAttribute("page1");
  35.                
  36.                 for(Student student:page1.getStudents()){
  37.                         %>
  38.                         <tr>
  39.                                 <td><a href = "QueryStudentBySnoServlet?sno=<%=student.getSno()%>"><%=student.getSno() %></a></td>
  40.                                 <td><%=student.getName() %></td>
  41.                                 <td><%=student.getAge() %></td>
  42.                                 <td><%=student.getDept() %></td>
  43.                                 <td><a href = "DeleteStudentServlet?sno=<%=student.getSno() %>">删除</a></td>
  44.                         </tr>
  45.                         <%                       
  46.                 }
  47.                 %>
  48.                 </table>
  49.                 <div class = item>
  50.                 <a href = "add.jsp">增加学生</a>
  51.                 </div>
  52.         <%
  53.                 if(page1.getCurrentPage()==page1.getTotalPage()-1){
  54.                 %>
  55.                 <div class = item>
  56.                 <a href = "QueryStudentByPageServlet?currentPage=0">首页</a>
  57.                 <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getCurrentPage()-1%>">上一页</a>
  58.                 </div>
  59.                 <%
  60.                 }
  61.                 else if(page1.getCurrentPage()==0){
  62.                         %>
  63.                         <div class = item>
  64.                         <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getCurrentPage()+1%>">下一页</a>
  65.                         <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getTotalPage()-1%>">尾页</a>
  66.                         </div>
  67.                         <%
  68.                 }
  69.                 else{
  70.                         %>
  71.                         <div class = item>
  72.                         <a href = "QueryStudentByPageServlet?currentPage=0">首页</a>
  73.                         <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getCurrentPage()-1%>">上一页</a>
  74.                         <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getCurrentPage()+1%>">下一页</a>
  75.                         <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getTotalPage()-1%>">尾页</a>
  76.                         </div>
  77.                         <%
  78.                 }
  79.                 %>
  80.                 <input type = "button" value = "上传作业" class = "btn"  onclick = "location = 'UpAndDown.jsp'"/><br/>
  81.                         <%
  82.         String error = (String)request.getAttribute("error");
  83.         if(error!=null){
  84.         if(error.equals("addError")){
  85.                 out.println("增加失败!");
  86.         }
  87.         else if(error.equals("noaddError")){
  88.                 out.println("增加成功!");
  89.         }
  90.         }
  91.        
  92.         String error1 = (String)request.getAttribute("error1");
  93.         if(error1!=null){
  94.         if(error1.equals("deleteError")){
  95.                 out.println("删除失败!");
  96.         }
  97.         else if(error1.equals("nodeleteError")){
  98.                 out.println("删除成功!");
  99.         }
  100.         }
  101.         String error2 = (String)request.getAttribute("error2");
  102.         if(error2!=null){
  103.         if(error2.equals("updateError")){
  104.                 out.println("修改失败!");
  105.         }
  106.         else if(error2.equals("noupdateError")){
  107.                 out.println("修改成功!");
  108.         }
  109.         }
  110.        
  111.        
  112.         String error3 = (String)request.getAttribute("error3");
  113.         if(error3!=null){
  114.         if(error3.equals("uploadError")){
  115.                 out.println("上传失败!");
  116.         }
  117.         else if(error3.equals("nouploadError")){
  118.                 out.println("上传成功!");
  119.         }
  120.         }
  121.         %>
  122.                 </div>
  123.                 </div>
  124. </body>
  125. </html>
复制代码
⑧登陆成功界面css(show.css)

  1. *{margin:0;padding:0;}
  2. html,body
  3. {
  4.         font:12px/150% Arial,Verdana;
  5. }
  6. .content{
  7.         background:url(../image/index.JPG);
  8.         background-size: cover;
  9.         width:1280px;
  10.         height:559px;
  11. }
  12. .box{
  13.         margin:60px 18% 60px 18%;
  14.         float:right;
  15.         padding:30px;
  16.         background:white;
  17.         background-color:rgba(255,255,255,0.9);
  18.         border-radius:15px;
  19.        
  20. }
  21. .item{
  22.         height:40px;
  23.        
  24.         border-radius:15px;
  25. }
  26. .item a{
  27.         background-color: lightgrey;
  28.         text-decoration: none;
  29.         font-size:15px;
  30.         color:black;
  31.         border-radius:3px;
  32. }
  33. .item1{
  34.         font-size:15px;
  35.         height:40px;
  36. }
  37. table{
  38.         width:100%;
  39.         border-collapse:collapse;
  40. }
  41. table body{
  42.         diaplay:block;
  43.         height:300px;
  44.         overflow-y: scroll;
  45. }
  46. table td{
  47.         border:1px solid #A6A6A6;
  48.         height:60px;
  49.         width:300px;
  50.         text-align: center;
  51.         font-size: 15px;
  52. }
  53. table th{
  54.         height:60px;
  55.         border:1px solid #A6A6A6;
  56. }
复制代码
⑨点击学号,修改门生信息界面(StudentInfo.jsp)

  1. <%@page import="student.entity.Student"%>
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"
  3.     pageEncoding="UTF-8"%>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="UTF-8">
  8. <link href="css/index.css" type="text/css" rel="stylesheet">
  9. <title>修改学生信息</title>
  10. </head>
  11. <body>
  12.         <%
  13.                 Student student = (Student)request.getAttribute("student");
  14.         %>               
  15.         <form action = "UpdateStudentServlet" method = "post">
  16.         <div class = "content">
  17.         <div class = "box">
  18.         <div class = "item1">
  19.         <h2>修改学生信息</h2>
  20.         </div>
  21.                 <div class = "item">
  22.                 学号:<input type = "text" name = "sno" value ="<%=student.getSno()%>" readonly = "readonly"/><br/>
  23.                 </div>
  24.                 <div class = "item">
  25.                 姓名:<input type = "text" name = "name" value ="<%=student.getName()%>"/><br/>
  26.                 </div>
  27.                 <div class = "item">
  28.                 年龄:<input type = "text" name = "age" value ="<%=student.getAge()%>"/><br/>
  29.                 </div>
  30.                 <div class = "item">
  31.                 专业:<input type = "text" name = "dept" value ="<%=student.getDept()%>"/><br/>
  32.                 </div>
  33.                 <input type = "submit" class = "btn" value = "修改"/>
  34.                 <a href = "QueryStudentByPageServlet">返回</a>
  35.                 </div>
  36.                 </div>
  37.         </form>
  38. </body>
  39. </html>
复制代码
⑩增加门生信息(add.jsp)

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.     pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <link href="css/index.css" type="text/css" rel="stylesheet">
  8. <script type = "text/javascript" src = "js/jquery-3.6.0.js"></script>
  9.        
  10.         <script type = "text/javascript">
  11.         function check()
  12.         {
  13.                 var sname = $("#sname").val();
  14.                 var sno = $("#sno").val();
  15.                 var sage = $("#sage").val();
  16.                 var sdept = $("#sdept").val();
  17.                 if(!(sno>1&&sno<2000)){
  18.                         alert("学号有误!必须是1-2000");
  19.                         return false;
  20.                 }
  21.                 if(!(sname.length>1&&sname.length<5)){
  22.                         alert("姓名有误!必须是2-4位");
  23.                         return false;
  24.                 }
  25.                 if(!(sage>1&&sage<100)){
  26.                         alert("年龄有误!必须是1-100");
  27.                         return false;
  28.                 }
  29.                 return true;
  30.         }
  31.          $(document).ready(function(){
  32.          });
  33.         </script>
  34.        
  35. <title>增加学生信息</title>
  36. </head>
  37. <body>
  38.         <form action = "AddStudentServlet" method = "post"  onsubmit = "return check()">
  39.         <div class = "content">
  40.         <div class = "box">
  41.         <div class = "item2">
  42.         <h2>增加学生信息</h2>
  43.         </div>
  44.         <div class = "item">
  45.         <input type = "text" name = "name" id = "sname" placeholder="姓名"/><br/>
  46.         </div>
  47.         <div class = "item">
  48.         <input type = "text" name = "sno" id = "sno" placeholder="学号"/><br/>
  49.         </div>
  50.         <div class = "item">
  51.         <input type = "text" name = "age" id = "sage" placeholder="年龄"/><br/>
  52.         </div>
  53.         <div class = "item">
  54.         <input type = "text" name = "dept" id = "sdept" placeholder="专业"/><br/>
  55.         </div>
  56.         <input type = "submit" class = "btn" value = "增加"/><br/>
  57.         <a href = "QueryStudentByPageServlet">返回</a>
  58.         </div>
  59.         </div>
  60.         </form>
  61. </body>
  62. </html>
复制代码
⑪上传作业操纵(UpAndDown.jsp)

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.     pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <link href="css/index.css" type="text/css" rel="stylesheet">
  8. <title>上传作业</title>
  9. </head>
  10. <body>
  11.         <form action ="UploadServlet" method = "post" enctype = "multipart/form-data">
  12.         <div class = "content">
  13.         <div class = "box">
  14.         <div class = "item2">
  15.         <h2>上传作业</h2>
  16.         </div>
  17.         <div class = "item">
  18.         <input type = "text" name="sno" placeholder="学号"/><br/>
  19.         </div>
  20.         <div class = "item">
  21.         <input type = "text" name = "name" placeholder="姓名"/><br/>
  22.         </div>
  23.         <input type = "file" name = "spiature"/>
  24.         <br/>
  25.         <br/>
  26.         <input type = "submit" class = "btn" value ="上传"/>
  27.         <a href = "QueryStudentByPageServlet">返回</a>
  28.         </div>
  29.         </div>
  30.         </form>
  31. </body>
  32. </html>
复制代码
2.三层架构

①表示层Servlet

查抄登录的用户名和密码是否匹配(CheckLoginServlet.java)
  1. package student.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import student.service.IStudentService;
  8. import student.service.impl.StudentServiceImpl;
  9. public class CheckLoginServlet extends HttpServlet {
  10.        
  11.         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12.                 request.setCharacterEncoding("utf-8");
  13.                 String ID = request.getParameter("UID");
  14.                 String pwd = request.getParameter("upwd");
  15.                 IStudentService service = new StudentServiceImpl();
  16.                 boolean result = service.checkLoginID(ID,pwd);
  17.                 response.setContentType("text/html;charest=UTF-8");
  18.                 response.setCharacterEncoding("utf-8");
  19.                 if(!result) {
  20.                         request.setAttribute("error", "loginError");
  21.                 }else {
  22.                         request.setAttribute("error", "nologinError");
  23.                 }
  24.                 request.getRequestDispatcher("login.jsp").forward(request, response);
  25.         }
  26.         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  27.                 // TODO Auto-generated method stub
  28.                 doGet(request, response);
  29.         }
  30. }
复制代码
注册账户表示层(AddLoginIDServlet.java)
  1. package student.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import student.service.IStudentService;
  8. import student.service.impl.StudentServiceImpl;
  9. /**
  10. * Servlet implementation class AddLoginIDServlet
  11. */
  12. public class AddLoginIDServlet extends HttpServlet {
  13.         private static final long serialVersionUID = 1L;
  14.        
  15.         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16.                 request.setCharacterEncoding("utf-8");
  17.                 String ID = request.getParameter("UID");
  18.                 String pwd = request.getParameter("upwd");
  19.                 IStudentService studentService = new StudentServiceImpl();
  20.                 boolean result = studentService.addLoginID(ID,pwd);
  21.                
  22.                 response.setContentType("text/html;charest=UTF-8");
  23.                 response.setCharacterEncoding("utf-8");
  24.                 if(!result) {
  25.                         request.setAttribute("error4", "addError");
  26.                         request.getRequestDispatcher("register.jsp").forward(request, response);
  27.                 }else {
  28.                         request.setAttribute("error4", "noaddError");
  29.                         request.getRequestDispatcher("login.jsp").forward(request, response);
  30.                 }
  31.                
  32.         }
  33.         /**
  34.          * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  35.          */
  36.         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37.                 // TODO Auto-generated method stub
  38.                 doGet(request, response);
  39.         }
  40. }
复制代码
修改密码表示层(UpdateLoginPwdServlet.java)
  1. package student.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import student.service.IStudentService;
  8. import student.service.impl.StudentServiceImpl;
  9. /**
  10. * Servlet implementation class UpdateLoginPwdServlet
  11. */
  12. public class UpdateLoginPwdServlet extends HttpServlet {
  13.         private static final long serialVersionUID = 1L;
  14.       
  15.    
  16.         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  17.                 request.setCharacterEncoding("utf-8");
  18.                 String ID = request.getParameter("UID");
  19.                 String pwd = request.getParameter("upwd");
  20.                 String pwd1 = request.getParameter("upwd1");
  21.                 IStudentService service = new StudentServiceImpl();
  22.                 boolean result = service.checkLoginID(ID,pwd);
  23.                 response.setContentType("text/html;charest=UTF-8");
  24.                 response.setCharacterEncoding("utf-8");
  25.                 if(!result) {
  26.                         request.setAttribute("error0", "loginError");
  27.                         request.getRequestDispatcher("UpdateLoginPwd.jsp").forward(request, response);
  28.                 }else {
  29.                         request.setAttribute("error0", "nologinError");
  30.                         boolean result1 = service.updateLoginPwd(ID,pwd1);
  31.                         if(!result1) {
  32.                                 request.getRequestDispatcher("UpdateLoginPwd.jsp").forward(request, response);
  33.                         }else {
  34.                                 request.setAttribute("error1", "noupdateError");
  35.                                 request.getRequestDispatcher("login.jsp").forward(request, response);
  36.                         }
  37.                 }
  38.         }
  39.         /**
  40.          * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  41.          */
  42.         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  43.                 // TODO Auto-generated method stub
  44.                 doGet(request, response);
  45.         }
  46. }
复制代码
注销用户表示层(DeleteLoginIDServlet.java)
  1. package student.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import student.service.IStudentService;
  8. import student.service.impl.StudentServiceImpl;
  9. /**
  10. * Servlet implementation class DeleteLoginIDServlet
  11. */
  12. public class DeleteLoginIDServlet extends HttpServlet {
  13.         private static final long serialVersionUID = 1L;
  14.    
  15.         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16.                 request.setCharacterEncoding("utf-8");
  17.                 String ID = request.getParameter("UID");
  18.                 String pwd = request.getParameter("upwd");
  19.                 IStudentService service = new StudentServiceImpl();
  20.                 boolean result = service.checkLoginID(ID,pwd);
  21.                 boolean result1 = service.deleteLoginID(ID);
  22.                 response.setContentType("text/html;charest=UTF-8");
  23.                 response.setCharacterEncoding("utf-8");
  24.                 if(!result) {
  25.                         request.setAttribute("error2", "loginError");
  26.                         request.getRequestDispatcher("DeleteLoginID.jsp").forward(request, response);
  27.                 }else {
  28.                         request.setAttribute("error2", "nologinError");
  29.                         if(!result1) {
  30.                                 request.setAttribute("error3", "deleteError");
  31.                                 request.getRequestDispatcher("DeleteLoginID.jsp").forward(request, response);
  32.                         }else {
  33.                                 request.setAttribute("error3", "nodeleteError");
  34.                                 request.getRequestDispatcher("login.jsp").forward(request, response);
  35.                 }
  36.                 }
  37.         }
  38.         /**
  39.          * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  40.          */
  41.         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  42.                 // TODO Auto-generated method stub
  43.                 doGet(request, response);
  44.         }
  45. }
复制代码
分页显示(QueryStudentByPageServlet.java)
  1. package student.servlet;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import student.entity.Page;
  9. import student.entity.Student;
  10. import student.service.IStudentService;
  11. import student.service.impl.StudentServiceImpl;
  12. public class QueryStudentByPageServlet extends HttpServlet {
  13.         private static final long serialVersionUID = 1L;
  14.     public QueryStudentByPageServlet() {
  15.         super();
  16.     }
  17.         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  18.                 IStudentService studentService = new StudentServiceImpl();
  19.                 int count = studentService.getTotalCount();
  20.                
  21.                 Page page = new Page();
  22.                
  23.                
  24.                 String cPage = request.getParameter("currentPage");
  25.                
  26.                 if(cPage == null) {
  27.                         cPage = "0";
  28.                 }
  29.                 int currentPage = Integer.parseInt(cPage);
  30.                 page.setCurrentPage(currentPage);
  31.                 int totalCount = studentService.getTotalCount();
  32.                 page.setTotalCount(totalCount);
  33.                 int pageSize = 4;
  34.                
  35.                 page.setPageSize(pageSize);
  36.                
  37.                 List<Student> students = studentService.queryStudentsByPage(currentPage, pageSize);
  38.                
  39.                 page.setStudents(students);
  40.                 request.setAttribute("page1", page);
  41.                 request.getRequestDispatcher("index.jsp").forward(request, response);
  42.         }
  43.         /**
  44.          * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  45.          */
  46.         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  47.                 // TODO Auto-generated method stub
  48.                 doGet(request, response);
  49.         }
  50. }
复制代码
按学号查询门生信息(QueryStudentBySnoServlet.java)
  1. package student.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import student.entity.Student;
  8. import student.service.IStudentService;
  9. import student.service.impl.StudentServiceImpl;
  10. public class QueryStudentBySnoServlet extends HttpServlet {
  11.        
  12.         /**
  13.          *
  14.          */
  15.         private static final long serialVersionUID = 1L;
  16.         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  17.         request.setCharacterEncoding("UTF-8");
  18.         int sno = Integer.parseInt(request.getParameter("sno"));
  19.         IStudentService service = new StudentServiceImpl();
  20.         Student student = service.queryStudentBySno(sno);
  21.         request.setAttribute("student", student);//将查询到的request信息放在request域中
  22.         request.getRequestDispatcher("StudentInfo.jsp").forward(request,response);
  23.         }
  24.         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25.                 doGet(request, response);
  26.         }
  27. }
复制代码
增加门生信息(AddStudentServlet.java)
  1. package student.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import student.entity.Student;
  8. import student.service.IStudentService;
  9. import student.service.impl.StudentServiceImpl;
  10. public class AddStudentServlet extends HttpServlet {
  11.         /**
  12.          *
  13.          */
  14.         private static final long serialVersionUID = 1L;
  15.         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16.                 request.setCharacterEncoding("utf-8");
  17.                 String name = request.getParameter("name");
  18.                 int sno = Integer.parseInt(request.getParameter("sno"));
  19.                 int age = Integer.parseInt(request.getParameter("age"));
  20.                 String dept = request.getParameter("dept");
  21.                 Student student = new Student(name, sno, age, dept);
  22.                
  23.                 IStudentService studentService = new StudentServiceImpl();
  24.                 boolean result = studentService.addStudent(student);
  25.                
  26.                 response.setContentType("text/html;charest=UTF-8");
  27.                 response.setCharacterEncoding("utf-8");
  28.                 if(!result) {
  29.                         request.setAttribute("error", "addError");
  30.                 }else {
  31.                         request.setAttribute("error", "noaddError");
  32.                 }
  33.                 request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response);
  34.         }
  35.         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  36.                 doGet(request, response);
  37.         }
  38. }
复制代码
删除门生信息(DeleteStudentServlet.java)
  1. package student.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import student.service.IStudentService;
  8. import student.service.impl.StudentServiceImpl;
  9. /**
  10. * Servlet implementation class DeleteStudentServlet
  11. */
  12. public class DeleteStudentServlet extends HttpServlet {
  13.        
  14.         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15.                 request.setCharacterEncoding("utf-8");
  16.                 int sno = Integer.parseInt(request.getParameter("sno"));
  17.                 IStudentService service = new StudentServiceImpl();
  18.                 boolean result = service.deleteStudentBySno(sno);
  19.                 response.setContentType("text/html;charest=UTF-8");
  20.                 response.setCharacterEncoding("utf-8");
  21.                 if(!result) {
  22.                         request.setAttribute("error1", "deleteError");
  23.                 }else {
  24.                         request.setAttribute("error1", "nodeleteError");
  25.                 }
  26.                 request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response);
  27.         }
  28.         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  29.                 // TODO Auto-generated method stub
  30.                 doGet(request, response);
  31.         }
  32. }
复制代码
修改门生信息表示层(UpdateStudentServlet.java)
  1. package student.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import student.entity.Student;
  8. import student.service.IStudentService;
  9. import student.service.impl.StudentServiceImpl;
  10. public class UpdateStudentServlet extends HttpServlet {
  11.         /**
  12.          *
  13.          */
  14.         private static final long serialVersionUID = 1L;
  15.         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16.                 request.setCharacterEncoding("UTF-8");
  17.                 String name = request.getParameter("name");
  18.                 int sno = Integer.parseInt(request.getParameter("sno"));
  19.                 int age = Integer.parseInt(request.getParameter("age"));
  20.                 String dept = request.getParameter("dept");
  21.                 Student student = new Student(name, age, dept);
  22.                
  23.                 System.out.println(sno);
  24.                
  25.                 IStudentService service = new StudentServiceImpl();
  26.                 boolean result = service.updateStudentBySno(sno, student);
  27.                
  28.                 response.setContentType("text/html;charest=UTF-8");
  29.                 response.setCharacterEncoding("utf-8");
  30.                 if(!result) {
  31.                         request.setAttribute("error2", "updateError");
  32.                 }else {
  33.                         request.setAttribute("error2", "noupdateError");
  34.                 }
  35.                 request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response);
  36.         }
  37.                
  38.         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  39.                 doGet(request, response);
  40.         }
  41. }
复制代码
上传作业(UploadServlet.java)
  1. package student.servlet;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import org.apache.commons.fileupload.FileItem;
  11. import org.apache.commons.fileupload.FileUploadBase;
  12. import org.apache.commons.fileupload.FileUploadException;
  13. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  14. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  15. import student.service.IStudentService;
  16. import student.service.impl.StudentServiceImpl;
  17. public class UploadServlet extends HttpServlet {
  18.         private static final long serialVersionUID = 1L;
  19.    
  20.         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  21.                 request.setCharacterEncoding("UTF-8");
  22.                 response.setCharacterEncoding("UTF-8");
  23.                 response.setContentType("text/html; charset=UTF-8");
  24.                 String name = null;
  25.                 int sno = -1;
  26.                
  27.                 boolean isMutipart = ServletFileUpload.isMultipartContent(request);
  28.                 if(isMutipart) {
  29.                         DiskFileItemFactory factory = new DiskFileItemFactory();
  30.                         ServletFileUpload upload = new ServletFileUpload(factory);
  31.                         factory.setRepository(new File("D:\\uploadtemp"));
  32.                         List<FileItem> items = null;
  33.                                 try {
  34.                                         items = upload.parseRequest(request);
  35.                                 } catch (FileUploadException e) {
  36.                                         // TODO Auto-generated catch block
  37.                                         e.printStackTrace();
  38.                                 }
  39.                         Iterator<FileItem> iter = items.iterator();
  40.                         while(iter.hasNext()) {
  41.                                 FileItem item= iter.next();
  42.                                 String fileName = item.getName();
  43.                                 String itemName = item.getFieldName();
  44.                                 if(item.isFormField()) {
  45.                                         if(itemName.equals("name")){
  46.                                                 name = item.getString("utf-8");
  47.                                         }else if(itemName.equals("sno")) {
  48.                                                 sno =Integer.parseInt(item.getString("utf-8"));
  49.                                         }else {
  50.                                                
  51.                                         }
  52.                                 }else {
  53.                                         //定义上传路径:指定上传的位置
  54.                                         String path = "D:\\upload";
  55.                                         File file = new File(path,fileName);
  56.                                                 try {
  57.                                                         item.write(file);
  58.                                                         } catch (Exception e) {
  59.                                                         e.printStackTrace();
  60.                                                         }
  61.                         }
  62.                 }
  63.                 IStudentService studentService = new StudentServiceImpl();
  64.                 boolean result = studentService.upLoadWork(sno,name);
  65.                
  66.                 System.out.println(name+sno);
  67.                 System.out.println(result);
  68.                 if(!result) {
  69.                         request.setAttribute("error3", "uploadError");
  70.                         request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response);
  71.                        
  72.                 }else{
  73.                         request.setAttribute("error3", "nouploadError");
  74.                         request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response);
  75.                        
  76.                 }
  77.                 }
  78.         }
  79.        
  80.         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  81.                 // TODO Auto-generated method stub
  82.                 doGet(request, response);
  83.         }
  84. }
复制代码

②业务逻辑层Service

Service接口(IStudentService.java)
  1. package student.service;
  2. import java.util.List;
  3. import student.entity.Student;
  4. public interface IStudentService {
  5.         //查询全部学生信息
  6.         public List<Student> queryAllStudents();
  7.         //按照学号查询学生信息
  8.         public Student queryStudentBySno(int sno);
  9.         //删除学生信息
  10.         public boolean deleteStudentBySno(int sno) ;
  11.        
  12.         //更改学生信息
  13.         public boolean updateStudentBySno(int sno, Student student) ;
  14.        
  15.         //增加学生信息
  16.         public boolean addStudent(Student student) ;
  17.        
  18.         //查询总数据
  19.         public int getTotalCount();
  20.         //分页
  21.         public List<Student> queryStudentsByPage(int current, int pageSize);
  22.         //检查登陆账户和密码
  23.         public boolean checkLoginID(String ID,String pwd);
  24.         //注册账户
  25.         public boolean addLoginID(String ID, String pwd);
  26.         //更改密码
  27.         public boolean updateLoginPwd(String ID,String pwd1);
  28.         //注销账号
  29.         public boolean deleteLoginID(String ID);
  30.         //判断ID是否存在
  31.         public boolean IDExist(String ID);
  32.         //判断上传作业输入的学生信息是否存在
  33.         public boolean upLoadWork(int sno, String name);
  34. }
复制代码
接口的实现类(StudentServiceImpl.java)
  1. package student.service.impl;
  2. import java.util.List;
  3. import student.dao.IStudentDao;
  4. import student.dao.impl.StudentDaoImpl;
  5. import student.entity.Student;
  6. import student.service.IStudentService;
  7. import student.util.DBUtil;
  8. //业务逻辑层:逻辑性的增删改查(增:查+增),对dao层进行的组装
  9. public class StudentServiceImpl implements IStudentService{
  10.         IStudentDao studentDao = new StudentDaoImpl();
  11.         //查询全部学生信息
  12.         public List<Student> queryAllStudents(){
  13.                 return studentDao.queryAllStudents();
  14.         }
  15.         //按照学号查询学生信息
  16.         public Student queryStudentBySno(int sno) {
  17.                 return studentDao.queryStudentBySno(sno);
  18.                
  19.         }
  20.         //删除学生信息
  21.         public boolean deleteStudentBySno(int sno) {
  22.                 if(studentDao.isExist(sno)) {
  23.                         return studentDao.deleteStudentBySno(sno);
  24.                 }
  25.                         return false;
  26.         }
  27.        
  28.        
  29.         //更改学生信息
  30.         public boolean updateStudentBySno(int sno, Student student) {
  31.                         return studentDao.updateStudentBySno(sno, student);
  32.         }
  33.        
  34.        
  35.         //增加学生信息
  36.         public boolean addStudent(Student student) {
  37.                 if(!studentDao.isExist(student.getSno())) {
  38.                         studentDao.addStudent(student);
  39.                         return true;
  40.                 }else {
  41.                         System.out.println("学号重复!");
  42.                         return false;
  43.                 }
  44.         }
  45.        
  46.         //查询总条数
  47.         @Override
  48.         public int getTotalCount() {
  49.                 return studentDao.getTotalCount();
  50.         }
  51.         //查询当前页的数据集合
  52.         @Override
  53.         public List<Student> queryStudentsByPage(int current, int pageSize) {
  54.                 return studentDao.queryStudentByPage(current, pageSize);
  55.         }
  56.        
  57.        
  58.         @Override
  59.         public boolean checkLoginID(String ID, String pwd) {
  60.                 return studentDao.checkLoginID(ID, pwd);
  61.         }
  62.         @Override
  63.         public boolean addLoginID(String ID, String pwd) {
  64.                 return studentDao.addLoginID(ID,pwd);
  65.                                        
  66.         }
  67.         @Override
  68.         public boolean updateLoginPwd(String ID, String pwd1) {
  69.                 return studentDao.updateLoginPwd(ID,pwd1);
  70.         }
  71.         @Override
  72.         public boolean deleteLoginID(String ID) {
  73.                 return studentDao.deleteLoginID(ID);
  74.         }
  75.         @Override
  76.         public boolean IDExist(String ID) {
  77.                 return studentDao.IDExist(ID);
  78.         }
  79.         @Override
  80.         public boolean upLoadWork(int sno, String name) {
  81.                 return studentDao.upLoadWork(sno,name);
  82.         }
  83.        
  84.        
  85. }
复制代码
③数据访问层Dao

接口(IStudentDao.java)
  1. package student.dao;
  2. import java.util.List;
  3. import student.entity.Student;
  4. public interface IStudentDao {
  5.         public boolean updateLoginPwd(String ID,String pwd1);
  6.         //查询全部学生信息
  7.         public List<Student> queryAllStudents();
  8.        
  9.        
  10.         //判断此人是否存在
  11.         public boolean isExist(int sno) ;
  12.        
  13.         //增加学生信息
  14.         public boolean addStudent(Student student);
  15.         //删除学生信息
  16.         public boolean deleteStudentBySno(int sno);
  17.        
  18.         //根据sno找到要修改的学生,然后再进行修改
  19.         public boolean  updateStudentBySno(int sno,Student student);
  20.         //根据学号查询学生信息
  21.         public Student queryStudentBySno(int sno);
  22.        
  23.         //查询总数据数
  24.         public int getTotalCount();
  25.         //currentPage:当前页(页码)pageSize:页面大小(每页显示的数据条数)
  26.         public List<Student> queryStudentByPage(int currentPage,int pageSize);
  27.        
  28.         public boolean checkLoginID(String ID,String pwd);
  29.         public boolean addLoginID(String ID, String pwd);
  30.         public boolean deleteLoginID(String ID);
  31.         public boolean IDExist(String ID);
  32.         public boolean upLoadWork(int sno, String name);
  33. }
复制代码
接口的实现类(StudentDaoImpl.java)
  1. package student.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import student.dao.IStudentDao;
  10. import student.entity.Student;
  11. import student.util.DBUtil;
  12. public class StudentDaoImpl implements IStudentDao{
  13.        
  14.         private final String URL = "jdbc:mysql://localhost:3306/STUDENT?useSSL=false&serverTimezone=UTC";
  15.         private final String UserName = "root";
  16.         private final String Pwd = "123456";
  17.         String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
  18.         //查询全部学生信息
  19.         public List<Student> queryAllStudents(){
  20.                 PreparedStatement pstmt = null;
  21.                 Student student = null;
  22.                 List<Student> students = new ArrayList<>();
  23.                 ResultSet rs = null;
  24.                 try {
  25.                         String sql = "select * from student1";
  26.                         rs = DBUtil.executeQuery(sql, null);
  27.                         while(rs.next()) {
  28.                                 int sno= rs.getInt("sno");
  29.                                 String name = rs.getString("name");
  30.                                 int age = rs.getInt("age");
  31.                                 String dept = rs.getString("dept");
  32.                                 student = new Student(name, sno, age, dept);
  33.                                 students.add(student);
  34.                         }
  35.                         return students;
  36.                        
  37.                 } catch(Exception e) {
  38.                         e.printStackTrace();
  39.                         return null;
  40.                 }
  41.                 finally {
  42.                         DBUtil.closeAll(rs, pstmt, DBUtil.connection);
  43.         }
  44.         }
  45.        
  46.        
  47.         //判断此人是否存在
  48.         public boolean isExist(int sno) {
  49.                 return queryStudentBySno(sno) == null? false:true;
  50.         }
  51.        
  52.         //增加学生信息
  53.         public boolean addStudent(Student student) {
  54.                 String sql = "insert into student1(name,sno,age,dept) values(?,?,?,?)";
  55.                 Object[] params = {student.getName(),student.getSno(),student.getAge(),student.getDept()};
  56.                 return DBUtil.executeUpdate(sql, params);
  57.         }
  58.         //删除学生信息
  59.         public boolean deleteStudentBySno(int sno) {
  60.                 String sql = "delete from student1 where sno =?";
  61.                 Object[] params = {sno};
  62.                 return DBUtil.executeUpdate(sql, params);
  63.         }
  64.        
  65.         //根据sno找到要修改的学生,然后再进行修改
  66.         public boolean  updateStudentBySno(int sno,Student student) {
  67.                 String sql = "update student1 set name =?,age=?,dept=? where sno=?";
  68.                 Object[] params = {student.getName(),student.getAge(),student.getDept(),sno};
  69.                
  70.                 return DBUtil.executeUpdate(sql, params);
  71.                
  72.         }
  73.         //根据学号查询学生信息
  74.         public Student queryStudentBySno(int sno){
  75.                 PreparedStatement pstmt = null;
  76.                 Student student = null;
  77.                 Connection connection = null;
  78.                 ResultSet rs = null;
  79.                 try {
  80.                         Class.forName(JDBC_DRIVER);
  81.                         connection = DriverManager.getConnection(URL,UserName,Pwd);
  82.                         String sql = "select * from student1 where sno = ?";
  83.                         pstmt = connection.prepareStatement(sql);
  84.                         pstmt.setInt(1, sno);
  85.                         rs = pstmt.executeQuery();
  86.                         if(rs.next()) {
  87.                                 int no= rs.getInt("sno");
  88.                                 String name = rs.getString("name");
  89.                                 int age = rs.getInt("age");
  90.                                 String dept = rs.getString("dept");
  91.                                 student = new Student(name, no, age, dept);
  92.                         }
  93.                         return student;
  94.                        
  95.                 } catch (ClassNotFoundException e) {
  96.                         // TODO Auto-generated catch block
  97.                         e.printStackTrace();
  98.                         return null;
  99.                 } catch(SQLException e) {
  100.                         e.printStackTrace();
  101.                         return null;
  102.                 }catch(Exception e) {
  103.                         e.printStackTrace();
  104.                         return null;
  105.                 }
  106.                 finally {
  107.                         DBUtil.closeAll(rs, pstmt, DBUtil.connection);
  108.                                 }
  109.         }
  110.         @Override
  111.         public int getTotalCount() {//查询总数据数
  112.                 String sql = "select count(1) from student1";
  113.                 return DBUtil.getTotalCount(sql);
  114.         }
  115.         @Override
  116.         public List<Student> queryStudentByPage(int currentPage, int pageSize) {
  117.                 String sql = "select * from student1 order by sno asc limit ?,?";
  118.                 Object[] params = {currentPage*pageSize,pageSize};
  119.                
  120.                 List<Student> students = new ArrayList<>();
  121.                 ResultSet rs = DBUtil.executeQuery(sql, params);
  122.                 try {
  123.                         while(rs.next()) {
  124.                                 Student student = new Student(rs.getString("name"),rs.getInt("sno"),rs.getInt("age"),rs.getString("dept"));
  125.                                 students.add(student);
  126.                         }
  127.                 } catch (SQLException e) {
  128.                         e.printStackTrace();
  129.                 }catch (Exception e) {
  130.                         e.printStackTrace();
  131.                 }
  132.                 return students;
  133.         }
  134.         @Override
  135.         public boolean checkLoginID(String ID, String pwd){
  136.                 int count = 0;
  137.                 String sql = "select * from login where ID=? and pwd=?";
  138.                 Object[] params = {ID,pwd};
  139.                 ResultSet rs = DBUtil.executeQuery(sql, params);
  140.                 try {
  141.                         while(rs.next()) {
  142.                                 count++;
  143.                         }
  144.                         if(count>0)
  145.                                 return true;
  146.                         else
  147.                                 return false;
  148.                 } catch (SQLException e) {
  149.                         e.printStackTrace();
  150.                         }
  151.                                 return false;
  152.         }
  153.         @Override
  154.         public boolean addLoginID(String ID, String pwd) {
  155.                 // TODO Auto-generated method stub
  156.                 String sql = "insert into login(ID,pwd) values(?,?)";
  157.                 Object[] params = {ID,pwd};
  158.                 return DBUtil.executeUpdate(sql, params);
  159.         }
  160.         @Override
  161.         public boolean updateLoginPwd(String ID, String pwd1) {
  162.                 String sql = "update login set pwd =? where ID=?";
  163.                 Object[] params = {pwd1,ID};
  164.                 return DBUtil.executeUpdate(sql, params);
  165.         }
  166.         @Override
  167.         public boolean deleteLoginID(String ID) {
  168.                 String sql = "delete from login where ID =?";
  169.                 Object[] params = {ID};
  170.                 return DBUtil.executeUpdate(sql, params);
  171.         }
  172.         @Override
  173.         public boolean IDExist(String ID) {
  174.                 String sql = "select *from login where ID = ?";
  175.                                 Object[] params = {ID};
  176.                 return DBUtil.executeUpdate(sql, params);
  177.         }
  178.         @Override
  179.         public boolean upLoadWork(int sno, String name) {
  180.                 int count = 0;
  181.                 String sql = "select *from student1 where sno = ? and name = ?";
  182.                 Object[] params = {sno,name};
  183.                 ResultSet rs = DBUtil.executeQuery(sql, params);
  184.                 try {
  185.                         while(rs.next()) {
  186.                                 count++;
  187.                         }
  188.                         if(count>0)
  189.                                 return true;
  190.                         else
  191.                                 return false;
  192.                 } catch (SQLException e) {
  193.                         e.printStackTrace();
  194.                         }
  195.                                 return false;
  196.         }
  197. }
复制代码
④通用的数据库操纵(DBUtils.java)

  1. package student.util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import student.entity.Student;
  11. //通用的数据库操作方法
  12. public class DBUtil {
  13.         private static final String URL = "jdbc:mysql://localhost:3306/STUDENT?useSSL=false&serverTimezone=UTC";
  14.         private static final String UserName = "root";
  15.         private static final String Pwd = "123456";
  16.         private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
  17.         public static Connection connection = null;
  18.         public static PreparedStatement pstmt = null;
  19.         public static ResultSet rs = null;
  20.        
  21.         //查询总数
  22.         public static int getTotalCount(String sql){
  23.                 int count = -1;
  24.                 try {
  25.                         pstmt = createPreParedStatement(sql,null);
  26.                         rs = pstmt.executeQuery();
  27.                         if(rs.next()) {
  28.                                 count = rs.getInt(1);
  29.                         }
  30.                 } catch (SQLException e) {
  31.                         e.printStackTrace();
  32.                 }catch (Exception e) {
  33.                         e.printStackTrace();
  34.                 }finally {
  35.                         closeAll(rs,pstmt,connection);
  36.                 }
  37.                 return count;
  38.         }
  39.        
  40.         //增删改
  41.         public static boolean executeUpdate(String sql,Object[] params) {
  42.                 try {
  43.                         pstmt = createPreParedStatement(sql,params);
  44.                         int count = pstmt.executeUpdate();
  45.                        
  46.                         System.out.println(count);
  47.                         if(count>0) {
  48.                                 return true;
  49.                         }
  50.                         else {
  51.                                 return false;
  52.                         }
  53.                 } catch (ClassNotFoundException e) {
  54.                         e.printStackTrace();
  55.                         return false;
  56.                 } catch(SQLException e) {
  57.                         e.printStackTrace();
  58.                         return false;
  59.                 }catch(Exception e) {
  60.                         e.printStackTrace();
  61.                         return false;
  62.                 }
  63.                 finally {
  64.                         closeAll(null,pstmt,connection);
  65.         }
  66.         }
  67.         public static void closeAll(ResultSet rs,Statement stmt,Connection connection){
  68.                 try{
  69.                         if(rs!=null)rs.close();
  70.                         if(pstmt!=null)pstmt.close();
  71.                         if(connection!=null)connection.close();
  72.                
  73.                 }catch(SQLException e) {
  74.                         e.printStackTrace();
  75.                 }
  76.         }
  77.        
  78.         public static PreparedStatement createPreParedStatement(String sql,Object[] params) throws ClassNotFoundException, SQLException {
  79.                         pstmt = getConnection().prepareStatement(sql);
  80.                         if(params!=null) {
  81.                         for(int i = 0;i<params.length;i++) {
  82.                                 pstmt.setObject(i+1, params[i]);
  83.                         }
  84.                         }
  85.                         return pstmt;
  86.         }
  87.        
  88.         public static Connection getConnection() throws ClassNotFoundException, SQLException {
  89.                 Class.forName(JDBC_DRIVER);
  90.                 return DriverManager.getConnection(URL,UserName,Pwd);
  91.         }
  92.         //通用的查
  93.         public static ResultSet executeQuery(String sql,Object[] params){
  94.                 List<Student> students = new ArrayList<>();
  95.                 Student student = null;
  96.                
  97.                 try {
  98.                         pstmt = createPreParedStatement(sql,params);
  99.                         rs = pstmt.executeQuery();
  100.                        
  101.                         return rs;
  102.                        
  103.                 } catch(SQLException e) {
  104.                         e.printStackTrace();
  105.                         return null;
  106.                 }catch(Exception e) {
  107.                         e.printStackTrace();
  108.                         return null;
  109.                 }
  110.                
  111.         }
  112.         }
复制代码
3.JavaBean封装数据

①分页资助类(Page.java)

  1. package student.entity;
  2. import java.util.List;
  3. //分页帮助类
  4. public class Page {
  5.         private int currentPage;
  6.         private int pageSize;
  7.         private int totalCount;
  8.         private int totalPage;
  9.         private List<Student> students;
  10.         public Page() {
  11.                
  12.         }
  13.         public Page(int currentPage, int pageSize, int totalCount, int totalPage, List<Student> students) {
  14.                 this.currentPage = currentPage;
  15.                 this.pageSize = pageSize;
  16.                 this.totalCount = totalCount;
  17.                 this.totalPage = totalPage;
  18.                 this.students = students;
  19.         }
  20.         public int getCurrentPage() {
  21.                 return currentPage;
  22.         }
  23.         public void setCurrentPage(int currentPage) {
  24.                 this.currentPage = currentPage;
  25.         }
  26.         public int getPageSize() {
  27.                 return pageSize;
  28.         }
  29.        
  30.         public void setPageSize(int pageSize) {
  31.                 this.pageSize = pageSize;
  32.                 this.totalPage = this.totalCount%this.pageSize==0?this.totalCount/this.pageSize:this.totalCount/this.pageSize+1;
  33.         }
  34.         public int getTotalCount() {
  35.                 return totalCount;
  36.         }
  37.         public void setTotalCount(int totalCount) {
  38.                 this.totalCount = totalCount;
  39.         }
  40.         public int getTotalPage() {
  41.                 return totalPage;
  42.         }
  43.        
  44.        
  45.         public List<Student> getStudents() {
  46.                 return students;
  47.         }
  48.         public void setStudents(List<Student> students) {
  49.                 this.students = students;
  50.         }
  51. }
复制代码
②封装门生信息(Student.java)

  1. package student.entity;
  2. public class Student {
  3.         private String name;
  4.         private int sno;
  5.         private int age;
  6.         private String dept;
  7.        
  8.         public Student(int sno) {
  9.                 this.sno = sno;
  10.         }
  11.        
  12.         public Student() {
  13.                
  14.         }
  15.         public Student(String name, int age, String dept) {
  16.                 this.name = name;
  17.                 this.age = age;
  18.                 this.dept = dept;
  19.         }
  20.         public Student(String name, int sno, int age, String dept) {
  21.                 this.name = name;
  22.                 this.sno = sno;
  23.                 this.age = age;
  24.                 this.dept = dept;
  25.         }
  26.        
  27.         public String getName() {
  28.                 return name;
  29.         }
  30.         public void setName(String name) {
  31.                 this.name = name;
  32.         }
  33.         public int getSno() {
  34.                 return sno;
  35.         }
  36.         public void setSno(int sno) {
  37.                 this.sno = sno;
  38.         }
  39.         public int getAge() {
  40.                 return age;
  41.         }
  42.         public void setAge(int age) {
  43.                 this.age = age;
  44.         }
  45.         public String getDept() {
  46.                 return dept;
  47.         }
  48.         public void setDept(String dept) {
  49.                 this.dept = dept;
  50.         }
  51.         public String toString() {
  52.                 return this.getSno()+"-"+this.getName()+"-"+this.getAge()+"-"+this.getDept();
  53.                
  54.         }
  55. }
复制代码
4.项目所需jar包

   项目需要3个jar包,前两个jar包属于文件上传所需,最后一个为连接数据库的jar包
  下载官网地址:https://mvnrepository.com/
 
  




 



五.数据库表格

本人用的是mysql数据库,直接在mysql数据库中新建表格
①登录注册表格login


②门生信息表格student1




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

饭宝

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表