JavaWeb-简单学生信息管理系统的实现-Jsp+Servlet+MySql

关注评论留邮箱,直接发给你源码,不用自己复制!!!

关注评论留邮箱,直接发给你源码,不用自己复制!!!

关注评论留邮箱,直接发给你源码,不用自己复制!!!

运行截图:

JavaWeb-简单学生信息管理系统的实现-Jsp+Servlet+MySql_第1张图片

 

JavaWeb-简单学生信息管理系统的实现-Jsp+Servlet+MySql_第2张图片

项目文件结构:

JavaWeb-简单学生信息管理系统的实现-Jsp+Servlet+MySql_第3张图片

获取 数据库连接类:

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @ClassName: DbConnect
 * @Author: Leo
 * @Description:
 * @Date: 2019/3/27 21:36
 */
public class DbConnect {
    public static Connection connection;
    public static String url = "jdbc:mysql://localhost/stumanage?user=root&password=root"
            + "&useUnicode=true&characterEncoding=utf-8&useSSL=true";

    public static Connection getConnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}

对应学生的实体类:

package entity;


public class Student {

    private long id;
    private String name;
    private String sex;
    private String specialty;
    private String grade;

    public Student() {
    }

    public Student(long id, String name, String sex, String specialty, String grade) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.specialty = specialty;
        this.grade = grade;
    }

    public Student(String name, String sex, String specialty, String grade) {
        this.name = name;
        this.sex = sex;
        this.specialty = specialty;
        this.grade = grade;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }


    public String getSpecialty() {
        return specialty;
    }

    public void setSpecialty(String specialty) {
        this.specialty = specialty;
    }


    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

}

登录界面jsp:

<%--
  Created by IntelliJ IDEA.
  User: 24234
  Date: 2019/3/27
  Time: 19:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>


    学生管理系统
    
    
    


用户名
密  码

登录servlet:(查询所有学生信息显示到主页面jsp)

package servlet;

import db.DbConnect;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName: ${NAME}
 * @Author: Leo
 * @Description:
 * @Date: 2019/3/28 20:29
 */
@WebServlet(name = "loginServer")
public class loginServer extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String admin = req.getParameter("admin");
        String pwd = req.getParameter("pwd");
        if (admin.equals("admin") && pwd.equals("123")) {
            Connection connection = DbConnect.getConnection();
            String sql = "SELECT * FROM STUDENT";
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                resultSet = preparedStatement.executeQuery();
                HttpSession httpSession = req.getSession();
//                设置session有效时间为两小时
                httpSession.setMaxInactiveInterval(7200);
                httpSession.setAttribute("resultSet", resultSet);
                resp.sendRedirect("loginAction.jsp");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            resp.sendRedirect("index.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

主页面jsp:

<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/3/28
  Time: 14:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<% ResultSet resultSet = (ResultSet) session.getAttribute("resultSet");%>


    学生管理系统
    


<% while (resultSet.next()) { %> <%}%>
学生信息
姓名 性别 专业 年级 操作
<%=resultSet.getString("name")%> <%=resultSet.getString("sex")%> <%=resultSet.getString("specialty")%> <%=resultSet.getString("grade")%> ">修改 " onclick="return confirm('确定删除?')">删除
添加

根据id查询数据库记录:

package servlet;

import db.DbConnect;
import entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName: ${NAME}
 * @Author: Leo
 * @Description:
 * @Date: 2019/3/29 15:20
 */
@WebServlet(name = "selectServlet")
public class selectServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String sql = "SELECT * FROM STUDENT WHERE ID=?";
        Connection connection = DbConnect.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultset = null;
        Student student = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, Integer.parseInt(id));
            resultset = preparedStatement.executeQuery();
            while (resultset.next()) {
                String name = resultset.getString("name");
                String sex = resultset.getString("sex");
                String specialty = resultset.getString("specialty");
                String grade = resultset.getString("grade");
                student = new Student(Integer.parseInt(id), name, sex, specialty, grade);
            }
            HttpSession httpSession = request.getSession();
            httpSession.setAttribute("student", student);
            response.sendRedirect("stuEdit.jsp");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

 修改学生信息jsp:

<%@ page import="entity.Student" %><%--
  Created by IntelliJ IDEA.
  User: 24234
  Date: 2019/3/29
  Time: 14:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>


    修改学生信息
    


<%Student student = (Student) session.getAttribute("student");%>
<% } else { %> <%}%>
修改学生信息
姓名
性别 <%if (student.getSex().equals("男")) {%>
专业
年级 <% if (student.getGrade().equals("大一")) { %> <%} else if (student.getGrade().equals("大二")) {%> <%} else if (student.getGrade().equals("大三")) {%> <%} else if (student.getGrade().equals("大四")) {%> <%}%>

修改学生信息servlet:

package servlet;

import db.DbConnect;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName: ${NAME}
 * @Author: Leo
 * @Description:
 * @Date: 2019/3/29 15:44
 */
@WebServlet(name = "updateServlet")
public class updateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String specialty = request.getParameter("specialty");
        String grade = request.getParameter("grade");
        Connection connection = DbConnect.getConnection();
        String sql = "UPDATE STUDENT SET name=? , sex=? , specialty=? , grade=? where id=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(5, Integer.parseInt(id));
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, sex);
            preparedStatement.setString(3, specialty);
            preparedStatement.setString(4, grade);
            int i = preparedStatement.executeUpdate();
            HttpSession httpSession = request.getSession();
            if (i == 1) {
                String selectAll = "SELECT * FROM STUDENT";
                preparedStatement = connection.prepareStatement(selectAll);
                resultSet = preparedStatement.executeQuery();
                httpSession.setMaxInactiveInterval(7200);
                httpSession.setAttribute("resultSet", resultSet);
                response.sendRedirect("loginAction.jsp");
            } else {
                httpSession.setAttribute("message", "修改失败!");
                response.sendRedirect("error.jsp");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

添加学生信息jsp:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/3/28
  Time: 14:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>


    添加学生信息
    


添加学生信息
姓名
性别
专业
年级

添加学生信息servlet:

package servlet;

import db.DbConnect;
import entity.Student;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName: ${NAME}
 * @Author: Leo
 * @Description:
 * @Date: 2019/3/28 20:24
 */
public class addServlet extends javax.servlet.http.HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String specialty = request.getParameter("specialty");
        String grade = request.getParameter("grade");
        Student student = new Student(name, sex, specialty, grade);
        Connection connection = DbConnect.getConnection();
        String sql = "INSERT INTO student(name,sex,specialty,grade) values(?,?,?,?)";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, student.getName());
            preparedStatement.setString(2, student.getSex());
            preparedStatement.setString(3, student.getSpecialty());
            preparedStatement.setString(4, student.getGrade());
            int i = preparedStatement.executeUpdate();
            HttpSession httpSession = request.getSession();
            if (i == 1) {
                String selectSql = "SELECT * FROM STUDENT";
                preparedStatement = connection.prepareStatement(selectSql);
                resultSet = preparedStatement.executeQuery();
                httpSession.setMaxInactiveInterval(7200);
                httpSession.setAttribute("resultSet", resultSet);
                response.sendRedirect("loginAction.jsp");
            } else {
                httpSession.setAttribute("message", "添加失败!");
                response.sendRedirect("error.jsp");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

删除学生信息servlet:

package servlet;

import db.DbConnect;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName: ${NAME}
 * @Author: Leo
 * @Description:
 * @Date: 2019/3/29 16:05
 */
@WebServlet(name = "deleteServlet")
public class deleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String sql = "DELETE FROM STUDENT WHERE ID=?";
        Connection connection = DbConnect.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        HttpSession httpSession = request.getSession();
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, Integer.parseInt(id));
            int i = preparedStatement.executeUpdate();
            if (i == 1) {
                String select = "SELECT * FROM STUDENT";
                preparedStatement = connection.prepareStatement(select);
                resultSet = preparedStatement.executeQuery();
                httpSession.setMaxInactiveInterval(7200);
                httpSession.setAttribute("resultSet", resultSet);
                response.sendRedirect("loginAction.jsp");
            } else {
                httpSession.setAttribute("message", "删除失败!");
                response.sendRedirect("error.jsp");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

错误页面:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/3/28
  Time: 15:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>


    Error


<%
    String message = (String) session.getAttribute("message");
    out.print(message);
%>


web.xml




    Archetype Created Web Application
    
        addServlet
        servlet.addServlet
    
    
        loginServer
        servlet.loginServer
    
    
        selectServlet
        servlet.selectServlet
    
    
        updateServlet
        servlet.updateServlet
    
    
        deleteServlet
        servlet.deleteServlet
    

    
        addServlet
        /addServlet
    
    
        loginServer
        /loginServer
    
    
        selectServlet
        /selectServlet
    
    
        updateServlet
        /updateServlet
    
    
        deleteServlet
        /deleteServlet
    


 

你可能感兴趣的:(JavaWeb)