当前位置:   article > 正文

Javaweb学生信息管理系统(Mysql+JSP+MVC+CSS)_图书馆学生注册信息web

图书馆学生注册信息web

项目源码及数据库:

链接: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. <script type = "text/javascript">
  10. function check()
  11. {
  12. var UID = $("#UID").val();
  13. var upwd = $("#upwd").val();
  14. var upwd1 = $("#upwd1").val();
  15. if(upwd != upwd1){
  16. alert("两次输入的密码不一致,请重新输入!");
  17. return false;
  18. }
  19. return true;
  20. }
  21. $(document).ready(function(){
  22. });
  23. </script>
  24. <title>注册账号</title>
  25. </head>
  26. <body>
  27. <form action = "AddLoginIDServlet" method = "post" onsubmit = "return check()">
  28. <div class = "content">
  29. <div class = "box">
  30. <div class = "item1">
  31. <h2>用户注册</h2>
  32. </div>
  33. <div class = "item">
  34. <input type = "text" name = "UID" id = "UID" placeholder="账号"/><br/>
  35. </div>
  36. <div class = "item">
  37. <input type = "password" name = "upwd" id = "upwd" placeholder="密码"/><br/>
  38. </div>
  39. <div class = "item">
  40. <input type = "password" name = "upwd1" id = "upwd1" placeholder="确认密码"/><br/>
  41. </div>
  42. <input type = "submit" class = "btn" value = "注册"/><br/>
  43. <a href = "login.jsp">返回</a>
  44. <%
  45. String error4 = (String) request.getAttribute("error4");
  46. if (error4!= null) {
  47. if (error4.equals("addError")) {
  48. out.println("注册失败!账户名已经存在!");
  49. }
  50. }
  51. %>
  52. </div>
  53. </div>
  54. </form>
  55. </body>
  56. </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. .item{
  21. height:60px;
  22. margin-buttom:40px;
  23. }
  24. .item input{
  25. line-height:40px;
  26. width:260px;
  27. border:none;
  28. border-bottom: 1px solid #59c2c5;
  29. border-radius:3px;
  30. }
  31. .item1{
  32. font-size:15px;
  33. height:40px;
  34. }
  35. .btn{
  36. display:block;
  37. height:50px;
  38. color:#fff;
  39. background:#59c2c5;
  40. width:265px;
  41. font-size:16px;
  42. line-height:30px;
  43. text-align:center;
  44. border-radius:10px;
  45. border:none;
  46. }

⑤修改密码界面(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. <script type = "text/javascript">
  9. function check()
  10. {
  11. var upwd = $("#upwd").val();
  12. var upwd1 = $("#upwd1").val();
  13. var upwd2 = $("#upwd2").val();
  14. if(upwd1 != upwd2){
  15. alert("两次输入的密码不一致,请重新输入!");
  16. return false;
  17. }
  18. return true;
  19. }
  20. $(document).ready(function(){
  21. });
  22. </script>
  23. <title>更改账号密码</title>
  24. </head>
  25. <body>
  26. <form action = "UpdateLoginPwdServlet" method = "post" onsubmit = "return check()">
  27. <div class = "content">
  28. <div class = "box">
  29. <div class= "item1"><h2>修改密码</h2></div>
  30. <div class = item>
  31. <input type = "text" name = "UID" id = "UID" placeholder="账号"/><br/>
  32. </div>
  33. <div class = item>
  34. <input type = "password" name = "upwd" id = "upwd" placeholder="旧密码"/><br/>
  35. </div>
  36. <div class = item>
  37. <input type = "password" name = "upwd1" id = "upwd1" placeholder="新密码"/><br/>
  38. </div>
  39. <div class = item>
  40. <input type = "password" name = "upwd2" id = "upwd2" placeholder="确认密码"/><br/>
  41. </div>
  42. <input type = "submit" class = "btn" value = "提交"/><br/>
  43. <a href = "login.jsp">返回</a>
  44. <%
  45. String error0 = (String) request.getAttribute("error0");
  46. if (error0!= null) {
  47. if (error0.equals("loginError")) {
  48. out.println("用户名或密码错误,请重新输入!");
  49. } else if (error0.equals("nologinError")) {
  50. response.sendRedirect("login.jsp");
  51. }
  52. }
  53. %>
  54. </div>
  55. </div>
  56. </form>
  57. </body>
  58. </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. <script type = "text/javascript">
  11. $(document).ready(function(){
  12. $("tr:odd").css("background-color","lightgrey");
  13. });
  14. </script>
  15. <link href="css/show.css" type="text/css" rel="stylesheet">
  16. <meta charset="UTF-8">
  17. <title>学生信息列表</title>
  18. </head>
  19. <body>
  20. <div class = "content">
  21. <div class = "box">
  22. <table border="2" cellspacing="0">
  23. <tr>
  24. <th><h2>学号</h2></th>
  25. <th><h2>姓名</h2></th>
  26. <th><h2>年龄</h2></th>
  27. <th><h2>专业</h2></th>
  28. <th><h2>操作</h2></th>
  29. </tr>
  30. <%
  31. Page page1 = (Page)request.getAttribute("page1");
  32. for(Student student:page1.getStudents()){
  33. %>
  34. <tr>
  35. <td><a href = "QueryStudentBySnoServlet?sno=<%=student.getSno()%>"><%=student.getSno() %></a></td>
  36. <td><%=student.getName() %></td>
  37. <td><%=student.getAge() %></td>
  38. <td><%=student.getDept() %></td>
  39. <td><a href = "DeleteStudentServlet?sno=<%=student.getSno() %>">删除</a></td>
  40. </tr>
  41. <%
  42. }
  43. %>
  44. </table>
  45. <div class = item>
  46. <a href = "add.jsp">增加学生</a>
  47. </div>
  48. <%
  49. if(page1.getCurrentPage()==page1.getTotalPage()-1){
  50. %>
  51. <div class = item>
  52. <a href = "QueryStudentByPageServlet?currentPage=0">首页</a>
  53. <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getCurrentPage()-1%>">上一页</a>
  54. </div>
  55. <%
  56. }
  57. else if(page1.getCurrentPage()==0){
  58. %>
  59. <div class = item>
  60. <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getCurrentPage()+1%>">下一页</a>
  61. <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getTotalPage()-1%>">尾页</a>
  62. </div>
  63. <%
  64. }
  65. else{
  66. %>
  67. <div class = item>
  68. <a href = "QueryStudentByPageServlet?currentPage=0">首页</a>
  69. <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getCurrentPage()-1%>">上一页</a>
  70. <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getCurrentPage()+1%>">下一页</a>
  71. <a href = "QueryStudentByPageServlet?currentPage=<%=page1.getTotalPage()-1%>">尾页</a>
  72. </div>
  73. <%
  74. }
  75. %>
  76. <input type = "button" value = "上传作业" class = "btn" onclick = "location = 'UpAndDown.jsp'"/><br/>
  77. <%
  78. String error = (String)request.getAttribute("error");
  79. if(error!=null){
  80. if(error.equals("addError")){
  81. out.println("增加失败!");
  82. }
  83. else if(error.equals("noaddError")){
  84. out.println("增加成功!");
  85. }
  86. }
  87. String error1 = (String)request.getAttribute("error1");
  88. if(error1!=null){
  89. if(error1.equals("deleteError")){
  90. out.println("删除失败!");
  91. }
  92. else if(error1.equals("nodeleteError")){
  93. out.println("删除成功!");
  94. }
  95. }
  96. String error2 = (String)request.getAttribute("error2");
  97. if(error2!=null){
  98. if(error2.equals("updateError")){
  99. out.println("修改失败!");
  100. }
  101. else if(error2.equals("noupdateError")){
  102. out.println("修改成功!");
  103. }
  104. }
  105. String error3 = (String)request.getAttribute("error3");
  106. if(error3!=null){
  107. if(error3.equals("uploadError")){
  108. out.println("上传失败!");
  109. }
  110. else if(error3.equals("nouploadError")){
  111. out.println("上传成功!");
  112. }
  113. }
  114. %>
  115. </div>
  116. </div>
  117. </body>
  118. </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. .item{
  21. height:40px;
  22. border-radius:15px;
  23. }
  24. .item a{
  25. background-color: lightgrey;
  26. text-decoration: none;
  27. font-size:15px;
  28. color:black;
  29. border-radius:3px;
  30. }
  31. .item1{
  32. font-size:15px;
  33. height:40px;
  34. }
  35. table{
  36. width:100%;
  37. border-collapse:collapse;
  38. }
  39. table body{
  40. diaplay:block;
  41. height:300px;
  42. overflow-y: scroll;
  43. }
  44. table td{
  45. border:1px solid #A6A6A6;
  46. height:60px;
  47. width:300px;
  48. text-align: center;
  49. font-size: 15px;
  50. }
  51. table th{
  52. height:60px;
  53. border:1px solid #A6A6A6;
  54. }

⑨点击学号,修改学生信息界面(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. <script type = "text/javascript">
  10. function check()
  11. {
  12. var sname = $("#sname").val();
  13. var sno = $("#sno").val();
  14. var sage = $("#sage").val();
  15. var sdept = $("#sdept").val();
  16. if(!(sno>1&&sno<2000)){
  17. alert("学号有误!必须是1-2000");
  18. return false;
  19. }
  20. if(!(sname.length>1&&sname.length<5)){
  21. alert("姓名有误!必须是2-4位");
  22. return false;
  23. }
  24. if(!(sage>1&&sage<100)){
  25. alert("年龄有误!必须是1-100");
  26. return false;
  27. }
  28. return true;
  29. }
  30. $(document).ready(function(){
  31. });
  32. </script>
  33. <title>增加学生信息</title>
  34. </head>
  35. <body>
  36. <form action = "AddStudentServlet" method = "post" onsubmit = "return check()">
  37. <div class = "content">
  38. <div class = "box">
  39. <div class = "item2">
  40. <h2>增加学生信息</h2>
  41. </div>
  42. <div class = "item">
  43. <input type = "text" name = "name" id = "sname" placeholder="姓名"/><br/>
  44. </div>
  45. <div class = "item">
  46. <input type = "text" name = "sno" id = "sno" placeholder="学号"/><br/>
  47. </div>
  48. <div class = "item">
  49. <input type = "text" name = "age" id = "sage" placeholder="年龄"/><br/>
  50. </div>
  51. <div class = "item">
  52. <input type = "text" name = "dept" id = "sdept" placeholder="专业"/><br/>
  53. </div>
  54. <input type = "submit" class = "btn" value = "增加"/><br/>
  55. <a href = "QueryStudentByPageServlet">返回</a>
  56. </div>
  57. </div>
  58. </form>
  59. </body>
  60. </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. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. request.setCharacterEncoding("utf-8");
  12. String ID = request.getParameter("UID");
  13. String pwd = request.getParameter("upwd");
  14. IStudentService service = new StudentServiceImpl();
  15. boolean result = service.checkLoginID(ID,pwd);
  16. response.setContentType("text/html;charest=UTF-8");
  17. response.setCharacterEncoding("utf-8");
  18. if(!result) {
  19. request.setAttribute("error", "loginError");
  20. }else {
  21. request.setAttribute("error", "nologinError");
  22. }
  23. request.getRequestDispatcher("login.jsp").forward(request, response);
  24. }
  25. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  26. // TODO Auto-generated method stub
  27. doGet(request, response);
  28. }
  29. }

注册账户表示层(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. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. request.setCharacterEncoding("utf-8");
  16. String ID = request.getParameter("UID");
  17. String pwd = request.getParameter("upwd");
  18. IStudentService studentService = new StudentServiceImpl();
  19. boolean result = studentService.addLoginID(ID,pwd);
  20. response.setContentType("text/html;charest=UTF-8");
  21. response.setCharacterEncoding("utf-8");
  22. if(!result) {
  23. request.setAttribute("error4", "addError");
  24. request.getRequestDispatcher("register.jsp").forward(request, response);
  25. }else {
  26. request.setAttribute("error4", "noaddError");
  27. request.getRequestDispatcher("login.jsp").forward(request, response);
  28. }
  29. }
  30. /**
  31. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  32. */
  33. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  34. // TODO Auto-generated method stub
  35. doGet(request, response);
  36. }
  37. }

修改密码表示层(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. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. request.setCharacterEncoding("utf-8");
  16. String ID = request.getParameter("UID");
  17. String pwd = request.getParameter("upwd");
  18. String pwd1 = request.getParameter("upwd1");
  19. IStudentService service = new StudentServiceImpl();
  20. boolean result = service.checkLoginID(ID,pwd);
  21. response.setContentType("text/html;charest=UTF-8");
  22. response.setCharacterEncoding("utf-8");
  23. if(!result) {
  24. request.setAttribute("error0", "loginError");
  25. request.getRequestDispatcher("UpdateLoginPwd.jsp").forward(request, response);
  26. }else {
  27. request.setAttribute("error0", "nologinError");
  28. boolean result1 = service.updateLoginPwd(ID,pwd1);
  29. if(!result1) {
  30. request.getRequestDispatcher("UpdateLoginPwd.jsp").forward(request, response);
  31. }else {
  32. request.setAttribute("error1", "noupdateError");
  33. request.getRequestDispatcher("login.jsp").forward(request, response);
  34. }
  35. }
  36. }
  37. /**
  38. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  39. */
  40. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  41. // TODO Auto-generated method stub
  42. doGet(request, response);
  43. }
  44. }

注销用户表示层(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. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. request.setCharacterEncoding("utf-8");
  16. String ID = request.getParameter("UID");
  17. String pwd = request.getParameter("upwd");
  18. IStudentService service = new StudentServiceImpl();
  19. boolean result = service.checkLoginID(ID,pwd);
  20. boolean result1 = service.deleteLoginID(ID);
  21. response.setContentType("text/html;charest=UTF-8");
  22. response.setCharacterEncoding("utf-8");
  23. if(!result) {
  24. request.setAttribute("error2", "loginError");
  25. request.getRequestDispatcher("DeleteLoginID.jsp").forward(request, response);
  26. }else {
  27. request.setAttribute("error2", "nologinError");
  28. if(!result1) {
  29. request.setAttribute("error3", "deleteError");
  30. request.getRequestDispatcher("DeleteLoginID.jsp").forward(request, response);
  31. }else {
  32. request.setAttribute("error3", "nodeleteError");
  33. request.getRequestDispatcher("login.jsp").forward(request, response);
  34. }
  35. }
  36. }
  37. /**
  38. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  39. */
  40. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  41. // TODO Auto-generated method stub
  42. doGet(request, response);
  43. }
  44. }

分页显示(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. Page page = new Page();
  21. String cPage = request.getParameter("currentPage");
  22. if(cPage == null) {
  23. cPage = "0";
  24. }
  25. int currentPage = Integer.parseInt(cPage);
  26. page.setCurrentPage(currentPage);
  27. int totalCount = studentService.getTotalCount();
  28. page.setTotalCount(totalCount);
  29. int pageSize = 4;
  30. page.setPageSize(pageSize);
  31. List<Student> students = studentService.queryStudentsByPage(currentPage, pageSize);
  32. page.setStudents(students);
  33. request.setAttribute("page1", page);
  34. request.getRequestDispatcher("index.jsp").forward(request, response);
  35. }
  36. /**
  37. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  38. */
  39. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  40. // TODO Auto-generated method stub
  41. doGet(request, response);
  42. }
  43. }

按学号查询学生信息(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. private static final long serialVersionUID = 1L;
  15. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16. request.setCharacterEncoding("UTF-8");
  17. int sno = Integer.parseInt(request.getParameter("sno"));
  18. IStudentService service = new StudentServiceImpl();
  19. Student student = service.queryStudentBySno(sno);
  20. request.setAttribute("student", student);//将查询到的request信息放在request域中
  21. request.getRequestDispatcher("StudentInfo.jsp").forward(request,response);
  22. }
  23. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24. doGet(request, response);
  25. }
  26. }

增加学生信息(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. IStudentService studentService = new StudentServiceImpl();
  23. boolean result = studentService.addStudent(student);
  24. response.setContentType("text/html;charest=UTF-8");
  25. response.setCharacterEncoding("utf-8");
  26. if(!result) {
  27. request.setAttribute("error", "addError");
  28. }else {
  29. request.setAttribute("error", "noaddError");
  30. }
  31. request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response);
  32. }
  33. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  34. doGet(request, response);
  35. }
  36. }

删除学生信息(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. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. request.setCharacterEncoding("utf-8");
  15. int sno = Integer.parseInt(request.getParameter("sno"));
  16. IStudentService service = new StudentServiceImpl();
  17. boolean result = service.deleteStudentBySno(sno);
  18. response.setContentType("text/html;charest=UTF-8");
  19. response.setCharacterEncoding("utf-8");
  20. if(!result) {
  21. request.setAttribute("error1", "deleteError");
  22. }else {
  23. request.setAttribute("error1", "nodeleteError");
  24. }
  25. request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response);
  26. }
  27. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  28. // TODO Auto-generated method stub
  29. doGet(request, response);
  30. }
  31. }

修改学生信息表示层(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. System.out.println(sno);
  23. IStudentService service = new StudentServiceImpl();
  24. boolean result = service.updateStudentBySno(sno, student);
  25. response.setContentType("text/html;charest=UTF-8");
  26. response.setCharacterEncoding("utf-8");
  27. if(!result) {
  28. request.setAttribute("error2", "updateError");
  29. }else {
  30. request.setAttribute("error2", "noupdateError");
  31. }
  32. request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response);
  33. }
  34. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  35. doGet(request, response);
  36. }
  37. }

上传作业(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. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  20. request.setCharacterEncoding("UTF-8");
  21. response.setCharacterEncoding("UTF-8");
  22. response.setContentType("text/html; charset=UTF-8");
  23. String name = null;
  24. int sno = -1;
  25. boolean isMutipart = ServletFileUpload.isMultipartContent(request);
  26. if(isMutipart) {
  27. DiskFileItemFactory factory = new DiskFileItemFactory();
  28. ServletFileUpload upload = new ServletFileUpload(factory);
  29. factory.setRepository(new File("D:\\uploadtemp"));
  30. List<FileItem> items = null;
  31. try {
  32. items = upload.parseRequest(request);
  33. } catch (FileUploadException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. }
  37. Iterator<FileItem> iter = items.iterator();
  38. while(iter.hasNext()) {
  39. FileItem item= iter.next();
  40. String fileName = item.getName();
  41. String itemName = item.getFieldName();
  42. if(item.isFormField()) {
  43. if(itemName.equals("name")){
  44. name = item.getString("utf-8");
  45. }else if(itemName.equals("sno")) {
  46. sno =Integer.parseInt(item.getString("utf-8"));
  47. }else {
  48. }
  49. }else {
  50. //定义上传路径:指定上传的位置
  51. String path = "D:\\upload";
  52. File file = new File(path,fileName);
  53. try {
  54. item.write(file);
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60. IStudentService studentService = new StudentServiceImpl();
  61. boolean result = studentService.upLoadWork(sno,name);
  62. System.out.println(name+sno);
  63. System.out.println(result);
  64. if(!result) {
  65. request.setAttribute("error3", "uploadError");
  66. request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response);
  67. }else{
  68. request.setAttribute("error3", "nouploadError");
  69. request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response);
  70. }
  71. }
  72. }
  73. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  74. // TODO Auto-generated method stub
  75. doGet(request, response);
  76. }
  77. }

②业务逻辑层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. public boolean updateStudentBySno(int sno, Student student) ;
  13. //增加学生信息
  14. public boolean addStudent(Student student) ;
  15. //查询总数据
  16. public int getTotalCount();
  17. //分页
  18. public List<Student> queryStudentsByPage(int current, int pageSize);
  19. //检查登陆账户和密码
  20. public boolean checkLoginID(String ID,String pwd);
  21. //注册账户
  22. public boolean addLoginID(String ID, String pwd);
  23. //更改密码
  24. public boolean updateLoginPwd(String ID,String pwd1);
  25. //注销账号
  26. public boolean deleteLoginID(String ID);
  27. //判断ID是否存在
  28. public boolean IDExist(String ID);
  29. //判断上传作业输入的学生信息是否存在
  30. public boolean upLoadWork(int sno, String name);
  31. }

接口的实现类(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. public boolean deleteStudentBySno(int sno) {
  21. if(studentDao.isExist(sno)) {
  22. return studentDao.deleteStudentBySno(sno);
  23. }
  24. return false;
  25. }
  26. //更改学生信息
  27. public boolean updateStudentBySno(int sno, Student student) {
  28. return studentDao.updateStudentBySno(sno, student);
  29. }
  30. //增加学生信息
  31. public boolean addStudent(Student student) {
  32. if(!studentDao.isExist(student.getSno())) {
  33. studentDao.addStudent(student);
  34. return true;
  35. }else {
  36. System.out.println("学号重复!");
  37. return false;
  38. }
  39. }
  40. //查询总条数
  41. @Override
  42. public int getTotalCount() {
  43. return studentDao.getTotalCount();
  44. }
  45. //查询当前页的数据集合
  46. @Override
  47. public List<Student> queryStudentsByPage(int current, int pageSize) {
  48. return studentDao.queryStudentByPage(current, pageSize);
  49. }
  50. @Override
  51. public boolean checkLoginID(String ID, String pwd) {
  52. return studentDao.checkLoginID(ID, pwd);
  53. }
  54. @Override
  55. public boolean addLoginID(String ID, String pwd) {
  56. return studentDao.addLoginID(ID,pwd);
  57. }
  58. @Override
  59. public boolean updateLoginPwd(String ID, String pwd1) {
  60. return studentDao.updateLoginPwd(ID,pwd1);
  61. }
  62. @Override
  63. public boolean deleteLoginID(String ID) {
  64. return studentDao.deleteLoginID(ID);
  65. }
  66. @Override
  67. public boolean IDExist(String ID) {
  68. return studentDao.IDExist(ID);
  69. }
  70. @Override
  71. public boolean upLoadWork(int sno, String name) {
  72. return studentDao.upLoadWork(sno,name);
  73. }
  74. }

③数据访问层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. public boolean isExist(int sno) ;
  10. //增加学生信息
  11. public boolean addStudent(Student student);
  12. //删除学生信息
  13. public boolean deleteStudentBySno(int sno);
  14. //根据sno找到要修改的学生,然后再进行修改
  15. public boolean updateStudentBySno(int sno,Student student);
  16. //根据学号查询学生信息
  17. public Student queryStudentBySno(int sno);
  18. //查询总数据数
  19. public int getTotalCount();
  20. //currentPage:当前页(页码)pageSize:页面大小(每页显示的数据条数)
  21. public List<Student> queryStudentByPage(int currentPage,int pageSize);
  22. public boolean checkLoginID(String ID,String pwd);
  23. public boolean addLoginID(String ID, String pwd);
  24. public boolean deleteLoginID(String ID);
  25. public boolean IDExist(String ID);
  26. public boolean upLoadWork(int sno, String name);
  27. }

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

④通用的数据库操作(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. public static int getTotalCount(String sql){
  22. int count = -1;
  23. try {
  24. pstmt = createPreParedStatement(sql,null);
  25. rs = pstmt.executeQuery();
  26. if(rs.next()) {
  27. count = rs.getInt(1);
  28. }
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. }catch (Exception e) {
  32. e.printStackTrace();
  33. }finally {
  34. closeAll(rs,pstmt,connection);
  35. }
  36. return count;
  37. }
  38. //增删改
  39. public static boolean executeUpdate(String sql,Object[] params) {
  40. try {
  41. pstmt = createPreParedStatement(sql,params);
  42. int count = pstmt.executeUpdate();
  43. System.out.println(count);
  44. if(count>0) {
  45. return true;
  46. }
  47. else {
  48. return false;
  49. }
  50. } catch (ClassNotFoundException e) {
  51. e.printStackTrace();
  52. return false;
  53. } catch(SQLException e) {
  54. e.printStackTrace();
  55. return false;
  56. }catch(Exception e) {
  57. e.printStackTrace();
  58. return false;
  59. }
  60. finally {
  61. closeAll(null,pstmt,connection);
  62. }
  63. }
  64. public static void closeAll(ResultSet rs,Statement stmt,Connection connection){
  65. try{
  66. if(rs!=null)rs.close();
  67. if(pstmt!=null)pstmt.close();
  68. if(connection!=null)connection.close();
  69. }catch(SQLException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. public static PreparedStatement createPreParedStatement(String sql,Object[] params) throws ClassNotFoundException, SQLException {
  74. pstmt = getConnection().prepareStatement(sql);
  75. if(params!=null) {
  76. for(int i = 0;i<params.length;i++) {
  77. pstmt.setObject(i+1, params[i]);
  78. }
  79. }
  80. return pstmt;
  81. }
  82. public static Connection getConnection() throws ClassNotFoundException, SQLException {
  83. Class.forName(JDBC_DRIVER);
  84. return DriverManager.getConnection(URL,UserName,Pwd);
  85. }
  86. //通用的查
  87. public static ResultSet executeQuery(String sql,Object[] params){
  88. List<Student> students = new ArrayList<>();
  89. Student student = null;
  90. try {
  91. pstmt = createPreParedStatement(sql,params);
  92. rs = pstmt.executeQuery();
  93. return rs;
  94. } catch(SQLException e) {
  95. e.printStackTrace();
  96. return null;
  97. }catch(Exception e) {
  98. e.printStackTrace();
  99. return null;
  100. }
  101. }
  102. }

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. public Page(int currentPage, int pageSize, int totalCount, int totalPage, List<Student> students) {
  13. this.currentPage = currentPage;
  14. this.pageSize = pageSize;
  15. this.totalCount = totalCount;
  16. this.totalPage = totalPage;
  17. this.students = students;
  18. }
  19. public int getCurrentPage() {
  20. return currentPage;
  21. }
  22. public void setCurrentPage(int currentPage) {
  23. this.currentPage = currentPage;
  24. }
  25. public int getPageSize() {
  26. return pageSize;
  27. }
  28. public void setPageSize(int pageSize) {
  29. this.pageSize = pageSize;
  30. this.totalPage = this.totalCount%this.pageSize==0?this.totalCount/this.pageSize:this.totalCount/this.pageSize+1;
  31. }
  32. public int getTotalCount() {
  33. return totalCount;
  34. }
  35. public void setTotalCount(int totalCount) {
  36. this.totalCount = totalCount;
  37. }
  38. public int getTotalPage() {
  39. return totalPage;
  40. }
  41. public List<Student> getStudents() {
  42. return students;
  43. }
  44. public void setStudents(List<Student> students) {
  45. this.students = students;
  46. }
  47. }

②封装学生信息(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. public Student(int sno) {
  8. this.sno = sno;
  9. }
  10. public Student() {
  11. }
  12. public Student(String name, int age, String dept) {
  13. this.name = name;
  14. this.age = age;
  15. this.dept = dept;
  16. }
  17. public Student(String name, int sno, int age, String dept) {
  18. this.name = name;
  19. this.sno = sno;
  20. this.age = age;
  21. this.dept = dept;
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public int getSno() {
  30. return sno;
  31. }
  32. public void setSno(int sno) {
  33. this.sno = sno;
  34. }
  35. public int getAge() {
  36. return age;
  37. }
  38. public void setAge(int age) {
  39. this.age = age;
  40. }
  41. public String getDept() {
  42. return dept;
  43. }
  44. public void setDept(String dept) {
  45. this.dept = dept;
  46. }
  47. public String toString() {
  48. return this.getSno()+"-"+this.getName()+"-"+this.getAge()+"-"+this.getDept();
  49. }
  50. }

4.项目所需jar包

项目需要3个jar包,前两个jar包属于文件上传所需,最后一个为连接数据库的jar包

下载官网地址:https://mvnrepository.com/
 

 

五.数据库表格

本人用的是mysql数据库,直接在mysql数据库中新建表格

登录注册表格login

②学生信息表格student1

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/701464
推荐阅读
相关标签
  

闽ICP备14008679号