当前位置:   article > 正文

JavaWeb搭建学生管理系统(手把手)_javaweb学生信息管理增删改查

javaweb学生信息管理增删改查

本人的博客地址www.aogu181.top

本文章仅供参考,适合新手搭建JavaWeb,学习此文章来打打基础还是可以的,如果有错误或者写的不好的地方,请多多指教。

最后本项目只是提供一个框架和思路,对前端界面不做美化

目录

开发工具与环境

工具包

 功能说明

项目结构

操作步骤

1.创建数据库

(一)创建数据库

(二)创建数据表

 (三)插入数据

 2.创建JavaWeb界面

(一)登入界面

(二)主页界面

(三)增加学生界面

(四)修改界面

(五)查询界面

3.创建Java类实现功能

(一)创建对象类

(二)创建数据访问层(Dao层)

(三)创建servlet

(四)配置过滤器

源码


开发工具与环境

1.IntelliJ IDEA  2021.2.2

2.MySQL   8.0.20

3.jdk 1.8.0_144

4.Tomcat

工具包

因为项目需要连接数据库,所以需要一个连接数据库的jar包

本项目用的是mysql8.0所以jar对应的就是8.0版本,如图所示,需要的自取jar包,提取码:miek

 不同数据库版本对应的jar包是不一样的,具体jar包的下载地址:Jar包下载,怎么下载这里就不多介绍了

 功能说明

1.登入功能

2.增

3.删

4.改

5.查

项目结构

项目结构如图所示:

编译器的不同具体也不完全相同,但大致一样就行

src下创建com.公司名. xxx  的形式

bean包下放需要操作的对象

dao包下面放对需要操作对象的操作,例如增删改查

filter包下放过滤器,一般是放编码过滤器和权限过滤器

servlet包下放servlet对象

private包下放需要权限的页面

lib包下放需要导入的库(jar包)

web.xml是配置文件

操作步骤

1.创建数据库

(一)创建数据库

create database rg56;

(二)创建数据表

        这里设置了 id 为主键且不为空,其他设置根据自己的需求更改

  1. create table stuno(
  2. id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  3. name CHAR(30),
  4. password CHAR(30),
  5. );

最终如下图所示:

 (三)插入数据

  1. INSERT INTO stuno(id, name,password)
  2. VALUES (1,'小方','123456');

结果如下图所示,如需插入多个可自行选择,

 2.创建JavaWeb界面

完整项目放在最后,注意事项和解释全放在代码段里面了

(一)登入界面

 代码如下:

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: HARD
  4. Date: 2021/12/12
  5. Time: 16:20
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>登入界面</title>
  12. </head>
  13. <body>
  14. <%--如果 condition() 函数返回为true时才提交表单--%>
  15. <form action="checkLogin.jsp" method="post" onsubmit="return condition()">
  16. <%--这里的 name 和 id 可以随便取名字,但最好和数据库的字段保持一致--%>
  17. 学号:<input type="text" name="id" id="id"><br>
  18. 密码:<input type="password" name="password" id="password"><br>
  19. <input type="submit" value="登入">
  20. </form>
  21. <script>
  22. function condition() {
  23. var id_1 = document.getElementById("id").value;//获取id为id的值
  24. var pwd_2 = document.getElementById("password").value;//获取id为password的值;
  25. if(id_1==""){
  26. alert("学号不能为空!");
  27. return false;
  28. }
  29. if(pwd_2==""){
  30. alert("密码不能为空!");
  31. return false;
  32. }
  33. return true;
  34. }
  35. </script>
  36. </body>
  37. </html>

(二)主页界面

 代码如下:

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: HARD
  4. Date: 2021/12/12
  5. Time: 16:18
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%@ page import="com.bean.Student" %>
  10. <%@ page import="com.dao.StudentDao" %>
  11. <%@ page import="java.util.List" %>
  12. <%@ page import="java.util.Iterator" %>
  13. <html>
  14. <head>
  15. <title>学生信息</title>
  16. </head>
  17. <body>
  18. <table border="2px" align="center" cellspacing="0">
  19. <tr>
  20. <td>学号</td>
  21. <td>姓名</td>
  22. <td>密码</td>
  23. <td width="200px"><a href="add.jsp">增加学生</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="searchStudent.jsp">查询学生</a></td>
  24. </tr>
  25. <%
  26. List<Student> list = StudentDao.getList();
  27. Iterator<Student> iter = list.iterator();
  28. while (iter.hasNext()) {
  29. Student student = iter.next();
  30. %>
  31. <tr>
  32. <td width="75px"><%=student.getId()%>
  33. </td>
  34. <td width="75px"><%=student.getName()%>
  35. </td>
  36. <td width="75px"><%=student.getPassword()%>
  37. </td>
  38. <td width="120px">
  39. <%--将 id 参数传过去 --%>
  40. <a href="delete.jsp?id=<%=student.getId() %>">删除</a>&nbsp;&nbsp;
  41. <%--
  42. 因为修改是要先 获取学号 得到全部信息
  43. 在修改所以传了一个参数 ?id=<%=student.getId()%>
  44. --%>
  45. <a href="updateStudent.jsp?id=<%=student.getId()%>">修改</a>
  46. </td>
  47. </tr>
  48. <tr>
  49. </tr>
  50. <%
  51. }
  52. %>
  53. </table>
  54. </body>
  55. </html>

(三)增加学生界面

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: HARD
  4. Date: 2021/12/12
  5. Time: 20:35
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>添加学生</title>
  12. </head>
  13. <body>
  14. <form action="addCheck.jsp" method="post" onsubmit="return condition()">
  15. 学号:<input type="text" id="id" name="id"><br>
  16. 姓名:<input type="text" id="name" name="name"><br>
  17. 密码:<input type="password" id="password" name="password"><br>
  18. <input type="submit" value="提交">
  19. <input type="reset" value="重置">
  20. </form>
  21. <script>
  22. function condition() {
  23. var id_1 = document.getElementById("id").value;//获取id为id的值
  24. var name_2 = document.getElementById("name").value;//获取id为name的值
  25. var pwd_3 = document.getElementById("password").value;//获取id为password的值;
  26. if(id_1==""){
  27. alert("学号不能为空!");
  28. return false;
  29. }
  30. if(name_2==""){
  31. alert("姓名不能为空!");
  32. return false;
  33. }
  34. if(pwd_3==""){
  35. alert("密码不能为空!");
  36. return false;
  37. }
  38. return true;
  39. }
  40. </script>
  41. </body>
  42. </html>

(四)修改界面

 代码如下:

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: HARD
  4. Date: 2021/12/12
  5. Time: 21:05
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%@ page import="com.dao.StudentDao" %>
  10. <%@ page import="com.bean.Student" %>
  11. <html>
  12. <head>
  13. <title>修改学生信息</title>
  14. </head>
  15. <body>
  16. <%
  17. int id = Integer.parseInt(request.getParameter("id"));//获取学号
  18. Student stu = StudentDao.getStudent(id);//根据学号获取完整的对象
  19. %>
  20. <form action="updateCheck.jsp?id=<%=id%>" method="post">
  21. 学号: <input type="text" name="id" value="<%=stu.getId()%>"><br>
  22. 姓名: <input type="text" name="name" value="<%=stu.getName()%>"><br>
  23. 密码: <input type="text" name="password" value="<%=stu.getPassword()%>"><br>
  24. <input type="submit" value="修改">
  25. <input type="reset" value="重置">
  26. </form>
  27. </body>
  28. </html>

(五)查询界面

 代码如下:

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: HARD
  4. Date: 2021/12/12
  5. Time: 20:42
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%@ page import="com.bean.Student" %>
  10. <%@ page import="com.dao.StudentDao" %>
  11. <html>
  12. <head>
  13. <title>检查添加学生信息</title>
  14. </head>
  15. <body>
  16. <%
  17. request.setCharacterEncoding("UTF-8");
  18. //获取来自 add.jsp 的表单
  19. int id = Integer.parseInt(request.getParameter("id")) ;
  20. String name = request.getParameter("name");
  21. String password = request.getParameter("password");
  22. //创建 student 对象
  23. Student student = new Student();
  24. student.setId(id);
  25. student.setName(name);
  26. student.setPassword(password);
  27. StudentDao.add(student);
  28. //添加完成就返回查看页面
  29. response.sendRedirect("index.jsp");
  30. %>
  31. </body>
  32. </html>

3.创建Java类实现功能

(一)创建对象类

代码如下:

  1. package com.bean;
  2. public class Student {
  3. private int id;
  4. private String password;
  5. private String name;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getPassword() {
  13. return password;
  14. }
  15. public void setPassword(String password) {
  16. this.password = password;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. }

(二)创建数据访问层(Dao层)

        1.创建连接数据库操作对象类,BaseDao.java

  1. package com.dao;
  2. import java.sql.*;
  3. /*
  4. * 连接数据库
  5. *
  6. * */
  7. public class BaseDao {
  8. static{
  9. try {
  10. Class.forName("com.mysql.jdbc.Driver");
  11. } catch (ClassNotFoundException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. public static Connection getConnection(){
  16. Connection conn = null;
  17. try {
  18. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rg56?useUnicode=yes&characterEncoding=utf8", "root", "131488");
  19. } catch (SQLException e) {
  20. e.printStackTrace();
  21. }
  22. return conn;
  23. }
  24. public static void closeAll(ResultSet rs,PreparedStatement pStmt,Connection conn){
  25. if(rs != null){
  26. try {
  27. rs.close();
  28. } catch (SQLException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. if(pStmt != null){
  33. try {
  34. pStmt.close();
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. if(conn != null){
  40. try {
  41. conn.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. }

(二)创建操作对象类

  1. package com.dao;
  2. import com.bean.Student;
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. /*
  7. * 用来对学生进行操作
  8. * */
  9. public class StudentDao {
  10. //获取学生信息列表
  11. public static List<Student> getList() {
  12. Connection conn = null;
  13. PreparedStatement stmt = null;
  14. ResultSet rs = null;
  15. List<Student> list = new ArrayList<>();
  16. try {
  17. conn = BaseDao.getConnection();
  18. stmt = conn.prepareStatement("SELECT * FROM stuno");
  19. rs = stmt.executeQuery();
  20. while (rs.next()) {
  21. Student stu = new Student();
  22. stu.setId(rs.getInt(1));
  23. stu.setName(rs.getString(2));
  24. stu.setPassword(rs.getString(3));
  25. list.add(stu);
  26. }
  27. } catch (SQLException e) {
  28. e.printStackTrace();
  29. } finally {
  30. BaseDao.closeAll(rs, stmt, conn);
  31. }
  32. return list;
  33. }
  34. //增加学生信息
  35. public static void add(Student stu) {
  36. Connection con = null;
  37. PreparedStatement pStmt = null;
  38. ResultSet rs = null;
  39. try {
  40. con = BaseDao.getConnection();
  41. pStmt = con.prepareStatement("insert into stuno(id,name,password) values(?,?,?)");
  42. pStmt.setInt(1, stu.getId());
  43. pStmt.setString(2, stu.getName());
  44. pStmt.setString(3, stu.getPassword());
  45. pStmt.executeUpdate();//更新数据
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. //根据学号删除学生信息
  51. public static void delete(int id) {
  52. Connection con = null;
  53. PreparedStatement pStmt = null;
  54. try {
  55. con = BaseDao.getConnection();
  56. pStmt = con.prepareStatement("delete from stuno where id=?");
  57. pStmt.setInt(1, id);
  58. pStmt.executeUpdate();
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. //获取单个学生对象
  64. public static Student getStudent(int id) {
  65. Student s = new Student();
  66. Connection conn = null;
  67. PreparedStatement stmt = null;
  68. ResultSet rs = null;
  69. try {
  70. conn = BaseDao.getConnection();
  71. stmt = conn.prepareStatement("select * from stuno where id=?");
  72. stmt.setInt(1, id);
  73. rs = stmt.executeQuery();
  74. if (rs.next()) {
  75. s.setId(rs.getInt("id"));
  76. s.setName(rs.getString("name"));
  77. s.setPassword(rs.getString("password"));
  78. }
  79. } catch (SQLException e) {
  80. e.printStackTrace();
  81. } finally {
  82. BaseDao.closeAll(rs, stmt, conn);
  83. }
  84. return s;
  85. }
  86. //修改功能
  87. public static void updateStudent(Student student) {
  88. Connection conn = null;
  89. PreparedStatement stmt = null;
  90. ResultSet rs = null;
  91. try {
  92. conn = BaseDao.getConnection();
  93. String sql = "UPDATE stuno SET id=?,name=?,password=? where id=?";
  94. stmt = conn.prepareStatement(sql);
  95. stmt.setInt(1, student.getId());
  96. stmt.setString(2, student.getName());
  97. stmt.setString(3, student.getPassword());
  98. stmt.setInt(4, student.getId());
  99. stmt.executeUpdate();
  100. } catch (SQLException e) {
  101. e.printStackTrace();
  102. } finally {
  103. BaseDao.closeAll(rs, stmt, conn);
  104. }
  105. }
  106. //查询功能,根据模糊查询学号返回所有学生信息
  107. public static List<Student> getStudentList(int id) {
  108. Connection conn = null;
  109. PreparedStatement stmt = null;
  110. ResultSet rs = null;
  111. List<Student> allStudent = new ArrayList<>();
  112. try {
  113. conn = BaseDao.getConnection();
  114. stmt = conn.prepareStatement("select * from stuno where id like ?");
  115. stmt.setString(1, "%" + id + "%");
  116. rs = stmt.executeQuery();
  117. while (rs.next()) {
  118. Student stu = new Student();
  119. stu.setId(rs.getInt(1));
  120. stu.setName(rs.getString(2));
  121. stu.setPassword(rs.getString(3));
  122. allStudent.add(stu);
  123. }
  124. } catch (SQLException e) {
  125. e.printStackTrace();
  126. } finally {
  127. BaseDao.closeAll(rs, stmt, conn);
  128. }
  129. return allStudent;
  130. }
  131. }

(三)创建servlet

我只把查找功能交给了servlet,可根据自己需求添加

  1. package com.sevlet;
  2. import com.dao.StudentDao;
  3. import com.bean.Student;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebFilter;
  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.io.PrintWriter;
  12. import java.util.List;
  13. /**
  14. * /search 是注解也可以在 web.xml中配置servlet
  15. *
  16. */
  17. @WebServlet("/search")
  18. public class SearchSevlet extends HttpServlet {
  19. @Override
  20. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  21. req.setCharacterEncoding("UTF-8");
  22. resp.setContentType("text/html;charset=UTF-8");
  23. PrintWriter out = resp.getWriter();
  24. int id = Integer.parseInt(req.getParameter("id"));
  25. List<Student> list = StudentDao.getStudentList(id);
  26. req.setAttribute("list", list);
  27. req.getRequestDispatcher("searchStudent.jsp").forward(req, resp);
  28. super.doPost(req, resp);
  29. }
  30. }

(四)配置过滤器

1.配置字符码过滤器

  1. package com.filter;
  2. import java.io.IOException;
  3. import javax.servlet.Filter;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.FilterConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRequest;
  8. import javax.servlet.ServletResponse;
  9. public class EncodingFilter implements Filter {
  10. private static String encoding; // 定义变量接收初始化的值
  11. public void destroy() {
  12. }
  13. public void doFilter(ServletRequest request, ServletResponse response,
  14. FilterChain chain) throws IOException, ServletException {
  15. // 设置字符编码链锁
  16. request.setCharacterEncoding(encoding);
  17. response.setCharacterEncoding(encoding);
  18. chain.doFilter(request, response);
  19. }
  20. // 初始化
  21. public void init(FilterConfig config) throws ServletException {
  22. // 接收web.xml配置文件中的初始参数
  23. encoding = config.getInitParameter("CharsetEncoding");
  24. }
  25. }

创建完了还需要在web.xml中配置

  1. <filter>
  2. <filter-name>charsetEncodingFilter</filter-name>
  3. <filter-class>com.filter.EncodingFilter</filter-class>
  4. <init-param>
  5. <param-name>CharsetEncoding</param-name>
  6. <param-value>UTF-8</param-value>
  7. </init-param>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>charsetEncodingFilter</filter-name>
  11. <url-pattern>/*</url-pattern>
  12. </filter-mapping>

(2)创建权限过滤器

  1. package com.filter;
  2. import javax.servlet.*;
  3. import javax.servlet.annotation.WebFilter;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import javax.servlet.http.HttpSession;
  7. import java.io.IOException;
  8. public class PrivateFilter implements Filter {
  9. private FilterConfig filterConfig;
  10. @Override
  11. public void init(FilterConfig filterConfig) throws ServletException {
  12. this.filterConfig = filterConfig;
  13. }
  14. @Override
  15. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  16. HttpServletRequest request = (HttpServletRequest) servletRequest;
  17. HttpServletResponse response = (HttpServletResponse) servletResponse;
  18. HttpSession session = request.getSession();
  19. //这里的 name 是登入成功后在登入成功界面加一个 session.setAttribute("name","xxx");
  20. String name = (String) session.getAttribute("name");
  21. if (name == null) {
  22. if (request.getRequestURI().indexOf("../firstLogin.jsp") > -1) {
  23. filterChain.doFilter(servletRequest, servletResponse);
  24. } else {
  25. response.sendRedirect("../firstLogin.jsp");
  26. }
  27. } else {
  28. request.getRequestDispatcher("index.jsp").forward(request,response);
  29. return;
  30. }
  31. }
  32. @Override
  33. public void destroy() {
  34. }
  35. }

同样需要在web.xml中配置

  1. <filter>
  2. <filter-name>PrivateFilter</filter-name>
  3. <filter-class>com.filter.PrivateFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>PrivateFilter</filter-name>
  7. <!-- 需要过滤的路径-->
  8. <url-pattern>/private/*</url-pattern>
  9. </filter-mapping>

源码

最后大致就大功完成了,感谢支持。喜欢了可以帮忙点个赞哦

最后附上我自己学习时做的班费管理系统班费管理系统

本篇文章的源码也附上链接:源码
提取码:hx38

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

闽ICP备14008679号