基于java+mysql+JDBC+tomcat+Servlet+JSP+js的学生管理系统

x33g5p2x  于2022-04-10 转载在 Java  
字(27.2k)|赞(0)|评价(0)|浏览(692)

实现流程和思路

项目视频演示

设置数据库studenttest

①、创建user表

创建项目

jsp页面

登录界面login.jsp

主界面mainPage.jsp

添加学生信息AddUser.jsp

修改学生信息change.jsp

Servlet下

登录LoginServlet

查找SearchServlet

删除DeleteServlet

修改ChangeServlet

添加addServlet

Service下

FileServic接口下

FileServiceImpl实现类下

pojo下

User学生类

Dao下

FileDao接口下 

FileDaoImpl实现类下

utils下

JDBCUtils

 实现流程和思路

创建jsp页面,然后转发到servlet下,接受jsp页面传过来的值,拿着这些值去访问service服务层接口,服务层写个实类,这个实现类去服务层的接口,然后这个服务层的实现类再去访问dao层的接口,在持久层dao中在编写一个dao层接口的实现类去实现dao层的接口,最后在dao层的的实现类去访问数据库。

图示:

 项目视频演示

学生管理系统项目演示

设置数据库studenttest

①、创建user表

其中包括设置删除的u_isdelete(0是存在,1是删除)

  1. create table user(
  2. u_name varchar(20),
  3. u_id int primary key auto_increment,
  4. u_pwd varchar(20) ,
  5. u_phone varchar(20) not null,
  6. u_role int not null,
  7. u_isdelete int not null
  8. )
  9. charset=utf8;

创建项目

项目界面总览

jsp页面

登录界面login.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  5. %>
  6. <html>
  7. <head>
  8. <base href="<%=basepath %>"/>
  9. <meta charset="utf-8"/>
  10. <title>登录界面</title>
  11. <%-- css代码--%>
  12. <style>
  13. #fame_{
  14. width: 500px;
  15. height: 400px;
  16. margin-left: 600px;
  17. margin-top: 200px;
  18. background-color: aqua;
  19. /*边框黑色 实线 2像素*/
  20. border: black solid 2px;
  21. }
  22. #inner_{
  23. margin-left: 140px;
  24. margin-top: 160px;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <h1 align="center" style="color: red">登录界面</h1>
  30. <div id="fame_" >
  31. <div id="inner_">
  32. 用户:<input type="text" id="u_id">
  33. <b> <span id="span1" style="color: crimson">${error}</span></b><br><br>
  34. 密码:<input type="password" id="u_pwd">
  35. <b> <span id="span2" style="color: crimson"></span></b><br><br>
  36. <button id="btn01">重置</button>
  37. <button id="btn02" style="margin-left: 20px">登录</button><br>
  38. <script>
  39. <%-- 这里写js代码验证账号密码值是否为空--%>
  40. window.onload=function() {
  41. document.getElementById("btn02").onclick = function () {
  42. let id_value = document.getElementById("u_id").value
  43. if (id_value == null || id_value == "") {
  44. document.getElementById("span1").innerHTML = "用户名不能为空"
  45. return;
  46. }
  47. //判断密码是否为空
  48. let pwd = document.getElementById("u_pwd").value
  49. if (pwd == null || pwd == "") {
  50. document.getElementById("span2").innerHTML = "密码不能为空"
  51. return;
  52. }
  53. //跳转到servlet
  54. window.location.href = "login?u_id=" + id_value + "&u_pwd=" + pwd;
  55. }
  56. //在次点击清空错误信息
  57. document.getElementById("u_id").onfocus = function () {
  58. document.getElementById("span1").innerText = ""
  59. }
  60. document.getElementById("u_pwd").onfocus = function () {
  61. document.getElementById("span2").innerText = ""
  62. }
  63. //清空内容
  64. document.getElementById("btn01").onclick=function () {
  65. document.getElementById("u_id").value="";
  66. document.getElementById("u_pwd").value="";
  67. }
  68. }
  69. </script>
  70. </div>
  71. </div>
  72. </body>
  73. </html>

主界面mainPage.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  5. %>
  6. <%--JSTL语法,可以使用user.getName()方法--%>
  7. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  8. <html>
  9. <head>
  10. <title>主界面</title>
  11. <style>
  12. #head_{
  13. height: 100px;
  14. width: 100%;
  15. background-color: darkcyan;
  16. }
  17. #personFile{
  18. width: 120px;
  19. height: 50px;
  20. padding-top: 25px;
  21. margin-left: 90%;
  22. }
  23. #search{
  24. height: 70px;
  25. width: 100%;
  26. background-color: aqua;
  27. }
  28. #inner_s{
  29. /*width: 200px;
  30. height: 40px;
  31. padding-right: 70%;
  32. padding-top: 50%;*/
  33. width: 30%;
  34. padding-top:25px;
  35. padding-left: 45%;
  36. }
  37. #table{
  38. margin-left: 30%;
  39. margin-top: 30px;
  40. }
  41. td{
  42. text-align: center;
  43. height: 20px;
  44. width: 150px;
  45. border: darkcyan 2px solid;
  46. padding:6px;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div id="head_">
  52. <h2>
  53. <div id="personFile" >
  54. 名字:<span style="color: red">${user.getU_name()}</span><br>
  55. 编号:<span style="color: red">${user.getU_id()}</span>
  56. </div>
  57. </h2>
  58. </div>
  59. <div id="search">
  60. <div id="inner_s">
  61. <input type="text" style="font-size: 20px; height: 26px;width: 190px " id="fileId">&nbsp;&nbsp;
  62. <button style="font-size: 18px; height: 28px;" id="cx"margin-left: 30px>查询</button>
  63. <button style="font-size: 18px; height: 28px;" id="addUser" margin-left: 100px>添加</button>
  64. <button style="font-size: 18px; height: 28px;" id="deleteUser">删除</button>
  65. <button style="font-size: 18px; height: 28px;" id="changeUser">修改</button>
  66. <b> <span style="color: crimson">${tip}</span></b>
  67. </div>
  68. </div>
  69. <script>
  70. //提交
  71. document.getElementById("cx").onclick=function(){
  72. let v = document.getElementById('fileId').value;
  73. //访问服务器searchServlet
  74. window.location.href = "searchServlet?v=" + v+"&id=${user.getU_name()}";
  75. }
  76. //删除
  77. document.getElementById("deleteUser").onclick=function () {
  78. let v =document.getElementById('fileId').value;
  79. window.location.href = "deleteServlet?v="+v+"&id=${user.getU_name()}";
  80. }
  81. // 添加跳转到AddUser.jsp界面
  82. document.getElementById("addUser").onclick=function () {
  83. window.location = "AddUser.jsp";
  84. }
  85. //修改,跳转到change.jsp
  86. document.getElementById("changeUser").onclick=function () {
  87. let v =document.getElementById('fileId').value;
  88. window.location.href = "change.jsp";
  89. }
  90. </script>
  91. <div>
  92. <table id="table" style="height: 30px;width: 700px;border: black 1px solid;border-collapse:collapse;">
  93. <tr style="font-weight: bold" >
  94. <td>学生id</td>
  95. <td>学生名字</td>
  96. <td>学生电话</td>
  97. <td>学生成绩</td>
  98. <td>学生品行</td>
  99. </tr>
  100. <%--jstl语法遍历,var是一个指指针--%>
  101. <c:forEach items="${arr}" var="item">
  102. <tr>
  103. <td>${item.getU_id()}</td>
  104. <td>${item.getU_name()}</td>
  105. <td>${item.getU_phone()}</td>
  106. <td style="color: deeppink"> 优秀</td>
  107. <td style="color: red">良好</td>
  108. </td>
  109. </tr>
  110. </c:forEach>
  111. </table>
  112. </div>
  113. <div>
  114. </div>
  115. </body>
  116. </html>

添加学生信息AddUser.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  5. %>
  6. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  7. <html>
  8. <head>
  9. <base href="<%=basepath %>"/>
  10. <meta charset="utf-8"/>
  11. <title>添加信息界面</title>
  12. </head>
  13. <style>
  14. #div1{
  15. width: 400px;
  16. height: 300px;
  17. margin-left: 600px;
  18. margin-top: 200px;
  19. background-color:deepskyblue;
  20. /*边框黑色 实线 2像素*/
  21. border: black solid 2px;
  22. }
  23. </style>
  24. <body>
  25. <h2 align="center" >添加学生信息</h2><br><br>
  26. <div id="div1" >
  27. <tr> 学生id:<input type="text" id="u_id" >
  28. <span id="span01" style="color: red"></span> <br><br></tr>
  29. <tr> 姓名:<input type="text" id="u_name" ><br><br></tr>
  30. <tr> 密码:<input type="password" id="u_pwd"><br><br></tr>
  31. <tr> 电话号码:<input type="text" id="u_phone"><br><br></tr>
  32. <button id="btn3">提交</button>
  33. <script>
  34. document.getElementById("btn3").onclick=function(){
  35. //获取填写的数据
  36. let u_id=document.getElementById("u_id").value;
  37. let u_name=document.getElementById("u_name").value;
  38. let u_pwd=document.getElementById("u_pwd").value;
  39. let u_phone=document.getElementById("u_phone").value;
  40. if(u_id==null||u_id==""||u_name==null||u_name==""){
  41. document.getElementById("span01").innerText="id或名字为为空"
  42. }else{
  43. //把数据发送到后端,发送到servlet的addUser路径下
  44. window.location.href="addUser?u_id="+u_id+"&u_pwd="+u_pwd+"&u_name="+u_name+"&u_phone="+u_phone+"&id=${user.getU_name()}";
  45. }
  46. }
  47. document.getElementById("u_id").onfocus=function () {
  48. document.getElementById("span01").innerText=""
  49. }
  50. </script>
  51. </div>
  52. </body>
  53. </html>

修改学生信息change.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  5. %>
  6. <html>
  7. <head>
  8. <base href="<%=basepath %>"/>
  9. <meta charset="utf-8"/>
  10. <title>修改界面</title>
  11. </head>
  12. <style>
  13. #div1{
  14. width: 400px;
  15. height: 300px;
  16. margin-left: 600px;
  17. margin-top: 200px;
  18. background-color:deepskyblue;
  19. /*边框黑色 实线 2像素*/
  20. border: black solid 2px;
  21. }
  22. </style>
  23. <body>
  24. <h2 align="center" >修改学生信息</h2><br><br>
  25. <div id="div1" >
  26. 修改的学生id:<input type="text" id="u_id" >
  27. <span id="span01" style="color: red"></span> <br><br>
  28. 姓名:<input type="text" id="u_name" ><br><br>
  29. 密码:<input type="password" id="u_pwd"><br><br>
  30. 电话号码:<input type="text" id="u_phone"><br><br>
  31. <button id="btn3">提交</button>
  32. <script>
  33. //获取填写数据
  34. document.getElementById("btn3").onclick=function(){
  35. let u_id=document.getElementById("u_id").value;
  36. let u_name=document.getElementById("u_name").value;
  37. let u_pwd=document.getElementById("u_pwd").value;
  38. let u_phone=document.getElementById("u_phone").value;
  39. //判断非空
  40. if(u_id==null||u_id==""||u_name==null||u_name==""){
  41. document.getElementById("span01").innerText="id或名字为为空"
  42. }else{
  43. //不为空就转发到后端
  44. window.location.href="changeUser?u_id="+u_id+"&u_pwd="+u_pwd+"&u_name="+u_name+"&u_phone="+u_phone+"&id=${user.getU_name()}";
  45. }
  46. }
  47. //再次点击清除错误信息提示
  48. document.getElementById("u_id").onfocus=function () {
  49. document.getElementById("span01").innerText=""
  50. }
  51. </script>
  52. </div>
  53. </body>
  54. </html>

Servlet下

登录LoginServlet

  1. package com.StudentTest.Servlet;
  2. import com.StudentTest.pojo.User;
  3. import com.StudentTest.service.FileService;
  4. import com.StudentTest.service.FileServiceImpl;
  5. import com.StudentTest.service.LoginService;
  6. import com.StudentTest.service.LoginServiceImpl;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.annotation.WebServlet;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. @WebServlet("/login")
  15. public class LoginServlet extends HttpServlet {
  16. @Override
  17. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. String u_id=null,u_pwd=null;
  19. User u = new User();
  20. try{//非数字异常判断
  21. u_id = req.getParameter("u_id");
  22. u_pwd = req.getParameter("u_pwd");
  23. u.setU_id(Integer.valueOf(u_id));
  24. u.setU_pwd(u_pwd);
  25. }catch (Exception e){
  26. req.setAttribute("error", "你输入的不是数字");
  27. //错误就回到主界面
  28. req.getRequestDispatcher("login.jsp").forward(req, resp);
  29. }
  30. //实现登录服务层的业务逻辑层,从服务层service到持久层dao
  31. LoginService ls = new LoginServiceImpl();
  32. //返回user这样可以看到登录的用户是谁
  33. /**
  34. * 如果user为空说明账号密码不一致,跳转到登录界面
  35. * 不为空说明账号密码一致,跳转到主界面
  36. */
  37. User user = null;
  38. try {
  39. user = ls.loginService(u);
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. if (user != null) {
  44. FileService fs=new FileServiceImpl();
  45. //查看所有数据
  46. ArrayList<User> arrUser=fs.getAllStudent();
  47. //将这些数据转发到前端
  48. req.setAttribute("arr",arrUser);
  49. req.setAttribute("user", user);
  50. //跳转到主界面
  51. req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
  52. } else {
  53. req.setAttribute("error", "密码不匹配");
  54. req.getRequestDispatcher("login.jsp").forward(req, resp);
  55. }
  56. }
  57. }

查找SearchServlet

  1. package com.StudentTest.Servlet;
  2. import com.StudentTest.pojo.User;
  3. import com.StudentTest.service.FileService;
  4. import com.StudentTest.service.FileServiceImpl;
  5. import com.StudentTest.service.LoginService;
  6. import com.StudentTest.service.LoginServiceImpl;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.annotation.WebServlet;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. @WebServlet("/login")
  15. public class LoginServlet extends HttpServlet {
  16. @Override
  17. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. String u_id=null,u_pwd=null;
  19. User u = new User();
  20. try{//非数字异常判断
  21. u_id = req.getParameter("u_id");
  22. u_pwd = req.getParameter("u_pwd");
  23. u.setU_id(Integer.valueOf(u_id));
  24. u.setU_pwd(u_pwd);
  25. }catch (Exception e){
  26. req.setAttribute("error", "你输入的不是数字");
  27. //错误就回到主界面
  28. req.getRequestDispatcher("login.jsp").forward(req, resp);
  29. }
  30. //实现登录服务层的业务逻辑层,从服务层service到持久层dao
  31. LoginService ls = new LoginServiceImpl();
  32. //返回user这样可以看到登录的用户是谁
  33. /**
  34. * 如果user为空说明账号密码不一致,跳转到登录界面
  35. * 不为空说明账号密码一致,跳转到主界面
  36. */
  37. User user = null;
  38. try {
  39. user = ls.loginService(u);
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. if (user != null) {
  44. FileService fs=new FileServiceImpl();
  45. //查看所有数据
  46. ArrayList<User> arrUser=fs.getAllStudent();
  47. //将这些数据转发到前端
  48. req.setAttribute("arr",arrUser);
  49. req.setAttribute("user", user);
  50. //跳转到主界面
  51. req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
  52. } else {
  53. req.setAttribute("error", "密码不匹配");
  54. req.getRequestDispatcher("login.jsp").forward(req, resp);
  55. }
  56. }
  57. }

删除DeleteServlet

  1. package com.StudentTest.Servlet;
  2. import com.StudentTest.pojo.User;
  3. import com.StudentTest.service.FileService;
  4. import com.StudentTest.service.FileServiceImpl;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. @WebServlet("/deleteServlet")
  13. public class DeleteServlet extends HttpServlet {
  14. @Override
  15. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  16. req.setCharacterEncoding("UTF-8");
  17. String del=req.getParameter("v");
  18. //获取登录人信息
  19. String adm=req.getParameter("id");
  20. //访问service层,在service层创建对应的方法
  21. FileService fs=new FileServiceImpl();
  22. ArrayList<User> list=fs.getAllStudent();
  23. //获取登录人员信息
  24. User u = fs.getAdmint(adm);
  25. //service层调用方法,转到dao层执行sql语句
  26. boolean user=fs.delUser(del);
  27. String tip="";
  28. if(user){
  29. tip="删除成功";
  30. }else {
  31. tip="删除失败";
  32. }
  33. //发送到前端
  34. req.setAttribute("user", u);
  35. req.setAttribute("arr", list);
  36. req.setAttribute("tip", tip);
  37. req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
  38. }
  39. }

修改ChangeServlet

  1. package com.StudentTest.Servlet;
  2. import com.StudentTest.pojo.User;
  3. import com.StudentTest.service.FileService;
  4. import com.StudentTest.service.FileServiceImpl;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. @WebServlet("/changeUser")
  13. public class ChangeServlet extends HttpServlet {
  14. @Override
  15. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  16. req.setCharacterEncoding("UTF-8");
  17. String id=req.getParameter("u_id");
  18. String pwd=req.getParameter("u_pwd");
  19. String phone=req.getParameter("u_phone");
  20. String name=req.getParameter("u_name");
  21. FileService fs=new FileServiceImpl();
  22. User user=new User(name,Integer.valueOf(id),pwd,phone);
  23. String adm=req.getParameter("id");
  24. //获取登录人员信息
  25. User u = fs.getAdmint(adm);
  26. System.out.println(u);
  27. boolean flag=fs.changeUser( user);
  28. ArrayList<User> list=fs.getAllStudent();
  29. String tip="";
  30. if (flag){
  31. tip="修改成功";
  32. }else{
  33. tip="修改失败";
  34. }
  35. //发送到前端
  36. req.setAttribute("tip",tip);
  37. req.setAttribute("user", u);
  38. req.setAttribute("arr", list);
  39. req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
  40. }
  41. }

添加addServlet

  1. package com.StudentTest.Servlet;
  2. import com.StudentTest.pojo.User;
  3. import com.StudentTest.service.FileService;
  4. import com.StudentTest.service.FileServiceImpl;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. @WebServlet("/addUser")
  13. public class AddServlet extends HttpServlet {
  14. @Override
  15. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  16. req.setCharacterEncoding("UTF-8");
  17. String id=req.getParameter("u_id");
  18. String pwd=req.getParameter("u_pwd");
  19. String phone=req.getParameter("u_phone");
  20. String name=req.getParameter("u_name");
  21. FileService fs=new FileServiceImpl();
  22. User user=new User(name,Integer.valueOf(id),pwd,phone);
  23. String adm=req.getParameter("id");
  24. //获取登录人员信息
  25. User u = fs.getAdmint(adm);
  26. System.out.println(u);
  27. boolean flag=fs.addUser( user);
  28. ArrayList<User> list=fs.getAllStudent();
  29. String tip="";
  30. if (flag){
  31. tip="添加成功";
  32. }else{
  33. tip="添加失败";
  34. }
  35. //发送到前端
  36. req.setAttribute("tip",tip);
  37. req.setAttribute("user", u);
  38. req.setAttribute("arr", list);
  39. req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
  40. }
  41. }

Service下

FileServic接口下

  1. package com.StudentTest.service;
  2. import com.StudentTest.pojo.User;
  3. import java.util.ArrayList;
  4. public interface FileService {
  5. ArrayList<User> getAllStudent();
  6. User searchIdName(String v);
  7. User getAdmint(String id);
  8. boolean delUser(String del);
  9. boolean addUser(User user);
  10. boolean changeUser(User user);
  11. User loginService(User u) throws Exception;
  12. }

FileServiceImpl实现类下

  1. package com.StudentTest.service;
  2. import com.StudentTest.Dao.FileDao;
  3. import com.StudentTest.Dao.FileDaoImpl;
  4. import com.StudentTest.pojo.User;
  5. import java.util.ArrayList;
  6. public class FileServiceImpl implements FileService {
  7. FileDao fd= new FileDaoImpl();
  8. @Override
  9. public ArrayList<User> getAllStudent() {
  10. /**
  11. * 访问持久层
  12. */
  13. return fd.getAllStudent();
  14. }
  15. @Override
  16. public User searchIdName(String v) {
  17. return fd.searchIdName( v);
  18. }
  19. @Override
  20. public User getAdmint(String id) {
  21. return fd.getAdmint(id);
  22. }
  23. @Override
  24. public boolean delUser(String del) {
  25. return fd.delUser(del );
  26. }
  27. @Override
  28. public boolean addUser(User user) {
  29. return fd.addUser(user);
  30. }
  31. @Override
  32. public boolean changeUser(User user) {
  33. return fd.changeUser(user);
  34. }
  35. @Override
  36. public User loginService(User u) throws Exception {
  37. return fd.loginDao( u);
  38. }
  39. }

pojo下

User学生类

  1. package com.StudentTest.pojo;
  2. public class User {
  3. private String u_name;
  4. private int u_id;
  5. private String u_pwd;
  6. private String u_phone;
  7. private int u_role;
  8. private int u_isdelete;
  9. public User() {
  10. }
  11. public User(String u_name, int u_id, String u_pwd, String u_phone) {
  12. this.u_name = u_name;
  13. this.u_id = u_id;
  14. this.u_pwd = u_pwd;
  15. this.u_phone = u_phone;
  16. }
  17. public User(String u_name, int u_id, String u_pwd, String u_phone, int u_role, int u_isdelete) {
  18. this.u_name = u_name;
  19. this.u_id = u_id;
  20. this.u_pwd = u_pwd;
  21. this.u_phone = u_phone;
  22. this.u_role = u_role;
  23. this.u_isdelete = u_isdelete;
  24. }
  25. public String getU_name() {
  26. return u_name;
  27. }
  28. public void setU_name(String u_name) {
  29. this.u_name = u_name;
  30. }
  31. public int getU_id() {
  32. return u_id;
  33. }
  34. public void setU_id(int u_id) {
  35. this.u_id = u_id;
  36. }
  37. public String getU_pwd() {
  38. return u_pwd;
  39. }
  40. public void setU_pwd(String u_pwd) {
  41. this.u_pwd = u_pwd;
  42. }
  43. public String getU_phone() {
  44. return u_phone;
  45. }
  46. public void setU_phone(String u_phone) {
  47. this.u_phone = u_phone;
  48. }
  49. public int getU_role() {
  50. return u_role;
  51. }
  52. public void setU_role(int u_role) {
  53. this.u_role = u_role;
  54. }
  55. public int getU_isdelete() {
  56. return u_isdelete;
  57. }
  58. public void setU_isdelete(int u_isdelete) {
  59. this.u_isdelete = u_isdelete;
  60. }
  61. @Override
  62. public String toString() {
  63. return "User{" +
  64. "u_name='" + u_name + '\'' +
  65. ", u_id=" + u_id +
  66. ", u_pwd='" + u_pwd + '\'' +
  67. ", u_phone='" + u_phone + '\'' +
  68. ", u_role=" + u_role +
  69. ", u_isdelete=" + u_isdelete +
  70. '}';
  71. }
  72. }

Dao下

FileDao接口下 

  1. package com.StudentTest.Dao;
  2. import com.StudentTest.pojo.User;
  3. import java.util.ArrayList;
  4. public interface FileDao {
  5. ArrayList<User> getAllStudent();
  6. User searchIdName(String v);
  7. User getAdmint(String id);
  8. boolean delUser(String del);
  9. boolean addUser(User user);
  10. boolean changeUser(User user);
  11. User loginDao(User u) throws Exception;
  12. }

FileDaoImpl实现类下

  1. package com.StudentTest.Dao;
  2. import com.StudentTest.pojo.User;
  3. import com.utils.JDBCUtils;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. public class FileDaoImpl implements FileDao {
  10. @Override
  11. public User loginDao(User u) throws SQLException, ClassNotFoundException {
  12. /**在这写数据库的内容
  13. * 获取链接对象,获取sql语句
  14. */
  15. int id=u.getU_id();
  16. String pwd=u.getU_pwd();
  17. // //访问数据库
  18. // User user=new User("慧宝",1001,"520","15216123240",1,0);
  19. Connection connection= JDBCUtils.getConnection();
  20. PreparedStatement pre =null;
  21. ResultSet res =null;
  22. String sql="select *from user where u_id="+id+" and u_pwd='"+pwd+"'";
  23. try{
  24. pre= connection.prepareStatement(sql);
  25. res=pre.executeQuery();
  26. User user=new User();
  27. while(res.next()){
  28. user.setU_id(res.getInt("u_id"));
  29. user.setU_name(res.getString("u_name"));
  30. user.setU_pwd(res.getString("u_pwd"));
  31. user.setU_phone(res.getString("u_phone"));
  32. user.setU_role(res.getInt("u_role"));
  33. user.setU_isdelete(res.getInt("u_isdelete"));
  34. return user;
  35. }
  36. } catch (SQLException s){
  37. s.printStackTrace();
  38. }
  39. return null;
  40. }
  41. //获取所有学生信息
  42. @Override
  43. public ArrayList<User> getAllStudent() {
  44. ArrayList<User> arr=new ArrayList<>();
  45. Connection connection= null;
  46. try {
  47. connection = JDBCUtils.getConnection();
  48. } catch (Exception e) {
  49. }
  50. PreparedStatement pre =null;
  51. ResultSet res =null;
  52. //u_role为1表示普通用户,为0为管理员,u_isdelete为0表示还在,为1表示已删除
  53. String sql="select *from user where u_role=1 and u_isdelete=0 ";
  54. try{
  55. pre= connection.prepareStatement(sql);
  56. res=pre.executeQuery();
  57. while(res.next()){
  58. User user=new User();
  59. user.setU_id(res.getInt("u_id"));
  60. user.setU_name(res.getString("u_name"));
  61. user.setU_pwd(res.getString("u_pwd"));
  62. user.setU_phone(res.getString("u_phone"));
  63. user.setU_role(res.getInt("u_role"));
  64. user.setU_isdelete(res.getInt("u_isdelete"));
  65. arr.add(user);
  66. }
  67. return arr;
  68. } catch (SQLException s){
  69. s.printStackTrace();
  70. }
  71. return null;
  72. }
  73. //找指定学生
  74. @Override
  75. public User searchIdName(String v) {
  76. /**
  77. * sql
  78. */
  79. Connection connection= null;
  80. try {
  81. connection = JDBCUtils.getConnection();
  82. } catch (Exception e) {
  83. }
  84. PreparedStatement pre =null;
  85. ResultSet res =null;
  86. //u_role为1表示普通用户,为0为管理员,u_isdelete为0表示还在,为1表示已删除
  87. String sql="select *from user where u_id ='"+v+"'or+ u_name ='"+v+"' ";
  88. try{
  89. pre= connection.prepareStatement(sql);
  90. res=pre.executeQuery();
  91. while(res.next()){
  92. User user=new User();
  93. user.setU_id(res.getInt("u_id"));
  94. user.setU_name(res.getString("u_name"));
  95. user.setU_pwd(res.getString("u_pwd"));
  96. user.setU_phone(res.getString("u_phone"));
  97. user.setU_role(res.getInt("u_role"));
  98. user.setU_isdelete(res.getInt("u_isdelete"));
  99. return user;
  100. }
  101. } catch (SQLException s){
  102. s.printStackTrace();
  103. }
  104. return null;
  105. }
  106. //获取登录人员信息
  107. @Override
  108. public User getAdmint(String id) {
  109. Connection connection= null;
  110. try {
  111. connection = JDBCUtils.getConnection();
  112. } catch (Exception e) {
  113. }
  114. PreparedStatement pre =null;
  115. ResultSet res =null;
  116. String sql="select *from user where u_id ='"+id+"'or+ u_name ='"+id+"' ";
  117. try{
  118. pre= connection.prepareStatement(sql);
  119. res=pre.executeQuery();
  120. while(res.next()){
  121. User user=new User();
  122. user.setU_id(res.getInt("u_id"));
  123. user.setU_name(res.getString("u_name"));
  124. return user;
  125. }
  126. } catch (SQLException s){
  127. s.printStackTrace();
  128. }
  129. return null;
  130. }
  131. //删除信息
  132. @Override
  133. public boolean delUser(String del) {
  134. Connection connection= null;
  135. try {
  136. connection = JDBCUtils.getConnection();
  137. } catch (Exception e) {
  138. }
  139. PreparedStatement pre =null;
  140. ResultSet ress=null;
  141. int res=0;
  142. String sql="delete from user where u_name ='"+del+"'or + u_id ='"+del+"' ";
  143. try{
  144. pre= connection.prepareStatement(sql);
  145. res=pre.executeUpdate();
  146. if(res>0)return true;
  147. } catch (SQLException s){
  148. s.printStackTrace();
  149. }
  150. return false;
  151. }
  152. //添加学生
  153. @Override
  154. public boolean addUser(User user) {
  155. Connection connection= null;
  156. try {
  157. connection = JDBCUtils.getConnection();
  158. } catch (Exception e) {
  159. }
  160. PreparedStatement pre =null;
  161. ResultSet ress=null;
  162. String name=user.getU_name();
  163. int id=user.getU_id();
  164. String pwd=user.getU_pwd();
  165. String phone=user.getU_phone();
  166. int res=0;
  167. //"insert into user values('"+user.getU_name()+"','"+user.getU_id()+"','"+user.getU_pwd()+"','"+user.getU_phone()+"')"
  168. String sql="insert into user(u_name,u_id,u_pwd,u_phone)\n" +
  169. "values('"+name+"','"+id+"','"+pwd+"','"+phone+"')";
  170. try{
  171. pre= connection.prepareStatement(sql);
  172. res=pre.executeUpdate();
  173. System.out.println(res);
  174. if(res>=0)return true;
  175. } catch (SQLException s){
  176. s.printStackTrace();
  177. }
  178. return false;
  179. }
  180. //修改信息
  181. @Override
  182. public boolean changeUser(User user) {
  183. Connection connection= null;
  184. try {
  185. connection = JDBCUtils.getConnection();
  186. } catch (Exception e) {
  187. }
  188. PreparedStatement pre =null;
  189. ResultSet ress=null;
  190. String name=user.getU_name();
  191. int id=user.getU_id();
  192. String pwd=user.getU_pwd();
  193. String phone=user.getU_phone();
  194. int res=0,res1=0;
  195. String sql1="delete from user where u_id="+id;
  196. try{
  197. pre= connection.prepareStatement(sql1);
  198. res=pre.executeUpdate();
  199. if (res>0){
  200. String sql="insert into user(u_name,u_id,u_pwd,u_phone)\n" +
  201. "values('"+name+"','"+id+"','"+pwd+"','"+phone+"')";
  202. pre= connection.prepareStatement(sql);
  203. res1=pre.executeUpdate();
  204. }
  205. System.out.println(res);
  206. if(res1>0)return true;
  207. } catch (SQLException s){
  208. s.printStackTrace();
  209. }
  210. return false;
  211. }
  212. }

utils下

JDBCUtils

  1. package com.utils;
  2. import java.sql.*;
  3. public class JDBCUtils {
  4. public static Connection getConnection() throws SQLException, ClassNotFoundException {
  5. Class.forName("com.mysql.cj.jdbc.Driver");
  6. String url="jdbc:mysql://localhost:3306/studenttest?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
  7. String username="root";
  8. String password="123456";
  9. Connection conn= DriverManager.getConnection(url,username,password);
  10. return conn;
  11. }
  12. public static void release(Statement stmt,Connection conn){
  13. if(stmt!=null){
  14. try{
  15. stmt.close();
  16. }catch(SQLException e){
  17. e.printStackTrace();
  18. }
  19. stmt=null;
  20. }
  21. if(conn!=null){
  22. try{
  23. conn.close();
  24. }catch (SQLException e){
  25. e.printStackTrace();
  26. }
  27. conn=null;
  28. }
  29. }
  30. public static void release(ResultSet rs,Statement stmt,Connection conn){
  31. if(rs!=null){
  32. try{
  33. rs.close();
  34. }catch (SQLException e){
  35. e.printStackTrace();
  36. }
  37. rs=null;
  38. }
  39. release(stmt,conn);
  40. }
  41. }

还有写web-inf下lib下的jar包

相关文章