一、应用名称:STD学生管理系统(Javaweb)
二、使用工具:MySQLr数据库,Eclipse软件
三、实现的功能:学生信息的增删改查、条件查询、模糊查询、精确查询等
四、步骤
1、在MySQL数据库新建student表
2、在Eclipse下新建web项目,并导入必要的jar包,下图是项目结构。
3、前端部分
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@page import="dao.UserDao,bean.User"%> <%@page import="java.util.ArrayList"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script> <title>学生管理系统</title> </head> <script type="text/javascript"> function add() { location.href = "add.jsp"; } </script> <style> td { text-align: center; } .form-control { width: 90%; } </style> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <% UserDao dao=new UserDao(); if(request.getAttribute("list") == null){ ArrayList<User> list = dao.getall(); request.setAttribute("list",list); } %> <table class="table table-hover table-striped table-bordered table-sm" id="resultshow"> <tr> <td colspan="6"> <h1>学生管理系统</h1> </td> </tr> <tr> <td colspan="3"> <button type="button" class="btn btn-info" onclick="add()">添加信息</button> </td> <td colspan="4" align="center"> <form action="Select" method="post"> <table> <tr> <td><input type="text" class="form-control" name="content"></td> <td><select name="way" class="form-control"> <option value="">--请选择查询方式--</option> <option value="1">学号</option> <option value="2">姓名</option> <option value="3">性别</option> <option value="4">住址</option> </select></td> <td > <button type="submit" class="btn btn-info">查询</button> </td> </tr> </table> </form> </td> </tr> <tr> <td>学号</td> <td>姓名</td> <td>性别</td> <td>出生年月</td> <td>住址</td> <td>操作</td> </tr> <c:forEach items="${list}" var="item"> <tr> <td>${item.sno}</td> <td>${item.sname}</td> <td>${item.ssex}</td> <td>${item.sbirthday}</td> <td>${item.saddress}</td> <td><a href="Delete?sno=${item.sno}" onclick = "return del()">删除</a> <a href="update?sno=${item.sno}">修改</a></td> </tr> </c:forEach> </table> </body> <script> //删除确认 function del() { if (!window.confirm('是否要删除数据??')) return false; } </script> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>修改学生信息</title> </head> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <style> .container { width: 100%; height: 800px; position: relative; background: url("img/2.png") no-repeat center/cover; } .login { width: 500px; height: auto; background-color: white; position: absolute; top: 100px; left: 350px; border-radius: 8px; } label { float: left; width: 100px; margin-top: 7px; margin-right: 5px; } .form-control { width: 60%; } .logtip { padding-top: 20px; padding-bottom: 20px; border-bottom: 2px solid red; text-align: center; } .form-group { margin-left: 40px; margin-top: 40px; } .btn { height: 50px; width: 100px; float: left; border-radius: 10px; } .logbtn { margin-right: 20px; } .btnbag { margin-left: 140px; margin-right: 140px; height: 50px; overflow: hidden; margin-top: 30px; margin-bottom: 40px; } </style> <body> <div class="container"> <div class="login"> <h2 class="logtip">修改</h2> <form action="updatesave" method="post" onsubmit="return check()"> <div class="form-group"> <label for="sno">学号</label> <input type="text" class="form-control" id="sno" name="sno" value="${bean.sno}"> </div> <div class="form-group"> <label for="sname">姓名</label> <input type="text" class="form-control" id="sname" name="sname" value="${bean.sname}"> </div> <div class="form-group"> <label for="ssex">性别</label> <input type="text" class="form-control" id="ssex" name="ssex" value="${bean.ssex}"> </div> <div class="form-group"> <label for="sbirthday">出生年月</label> <input type="date" class="form-control" id="sbirthday" name="sbirthday" value="${bean.sbirthday}"> </div> <div class="form-group"> <label for="saddress">住址</label> <input type="text" class="form-control" id="saddress" name="saddress" value="${bean.saddress}"> </div> <div class="btnbag"> <input type="submit" class="btn btn-primary logbtn" value="确定"> <input type="reset" class="btn btn-primary mangbtn" value="重置"> </div> </form> </div> </div> </body> <script type="text/javascript"> function check() { var sno = document.getElementById("sno").value; var sname = document.getElementById("sname").value; var ssex = document.getElementById("ssex").value; if ((sno == "") || (sname == "")) { alert("学号/姓名必填!"); return false; } return true; } </script> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加学生信息</title> </head> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <style> .container { width: 100%; height: 730px; position: relative; background: url("img/2.png") no-repeat center/cover; } .login { width: 500px; height: auto; background-color: white; position: absolute; top: 100px; left: 350px; border-radius: 8px; } label { float: left; width: 100px; margin-top: 7px; margin-right: 5px; } .form-control { width: 60%; } .logtip { padding-top: 25px; padding-bottom: 20px; border-bottom: 2px solid red; text-align: center; } .form-group { margin-left: 40px; margin-top: 25px; } .btn { height: 50px; width: 100px; float: left; border-radius: 10px; } .btnbag { margin-left: 200px; margin-right: 200px; height: 50px; overflow: hidden; margin-top: 30px; margin-bottom: 40px; } </style> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div class="container"> <div class="login"> <h2 class="logtip">注册</h2> <form action="addServlet" method="post" onsubmit="return check()"> <div class="form-group"> <label for="username">学号</label> <input type="text" class="form-control" id="sno" name="sno"> </div> <div class="form-group"> <label for="password">姓名</label> <input type="text" class="form-control" id="sname" name="sname"> </div> <div class="form-group"> <label for="name">性别</label> <input type="text" class="form-control" id="ssex" name="ssex" placeholder="男/女"> </div> <div class="form-group"> <label for="password">出生年月</label> <input type="date" class="form-control" id="sbirthday" name="sbirthday"> </div> <div class="form-group"> <label for="password">住址</label> <input type="text" class="form-control" id="saddress" name="saddress"> </div> <div class="btnbag"> <input type="submit" class="btn btn-primary logbtn" value="确定"> </div> </form> </div> </div> </body> <script type="text/javascript"> function check() { var sno = document.getElementById("sno").value; var sname = document.getElementById("sname").value; var ssex = document.getElementById("ssex").value; if ((sno == "") || (sname == "")) { alert("学号/姓名必填!"); return false; } return true; } </script> </html>
4、后台部分
①数据库连接工具
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { private static final String db_url="jdbc:mysql://localhost:3306/webtest?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true"; public static String db_user = "root"; public static String db_pass = "123456"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * �ر����� * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
②实体类
package bean; public class User { private String sno; private String sname; private String ssex; private String sbirthday; private String saddress; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public String getSbirthday() { return sbirthday; } public void setSbirthday(String sbirthday) { this.sbirthday = sbirthday; } public String getSaddress() { return saddress; } public void setSaddress(String saddress) { this.saddress = saddress; } public User() { super(); // TODO Auto-generated constructor stub } }
③servlet
package servlet; import java.io.IOException; 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 bean.User; import dao.UserDao; /** * Servlet implementation class AddServlet */ @WebServlet("/addServlet") public class AddServlet extends HttpServlet { private static final long serialVersionUID = 1L; UserDao dao=new UserDao(); /** * @see HttpServlet#HttpServlet() */ public AddServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String sno = request.getParameter("sno"); String sname = request.getParameter("sname"); String ssex = request.getParameter("ssex"); String sbirthday = request.getParameter("sbirthday"); String saddress = request.getParameter("saddress"); User bean=new User(); bean.setSno(sno); bean.setSname(sname); bean.setSsex(ssex); bean.setSbirthday(sbirthday); bean.setSaddress(saddress); int result=dao.adduser(bean); if(result==1) { request.setAttribute("message", "添加成功"); request.getRequestDispatcher("index.jsp").forward(request, response); }else { request.setAttribute("message", "添加失败"); request.getRequestDispatcher("add.jsp").forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package servlet; import java.io.IOException; 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 bean.User; import dao.UserDao; /** * Servlet implementation class Update */ @WebServlet("/update") public class Update extends HttpServlet { private static final long serialVersionUID = 1L; UserDao dao=new UserDao(); /** * @see HttpServlet#HttpServlet() */ public Update() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String sno = request.getParameter("sno"); User bean=null; bean=dao.searchbysno(sno); request.setAttribute("bean", bean); request.getRequestDispatcher("update.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package servlet; import java.io.IOException; 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 bean.User; import dao.UserDao; /** * Servlet implementation class UpdateSave */ @WebServlet("/updatesave") public class UpdateSave extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UpdateSave() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String sno = request.getParameter("sno"); String sname = request.getParameter("sname"); String ssex = request.getParameter("ssex"); String sbirthday = request.getParameter("sbirthday"); String saddress = request.getParameter("saddress"); User bean=new User(); bean.setSno(sno); bean.setSname(sname); bean.setSsex(ssex); bean.setSbirthday(sbirthday); bean.setSaddress(saddress); UserDao dao=new UserDao(); int result=dao.update(bean); if(result==1) { request.setAttribute("message", "修改成功"); request.getRequestDispatcher("index.jsp").forward(request, response); }else { request.setAttribute("message", "修改失败"); request.getRequestDispatcher("update?sno=sno").forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package servlet; import java.io.IOException; 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 dao.UserDao; /** * Servlet implementation class Delete */ @WebServlet("/Delete") public class Delete extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Delete() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String sno = request.getParameter("sno"); System.out.println(sno); UserDao dao=new UserDao(); int result=dao.del(sno); if (result==1) { request.setAttribute("message", "删除成功"); request.getRequestDispatcher("index.jsp").forward(request, response); }else { request.setAttribute("message", "删除失败"); request.getRequestDispatcher("index.jsp").forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package servlet; import java.io.IOException; import java.util.ArrayList; 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 bean.User; import dao.UserDao; /** * Servlet implementation class Select */ @WebServlet("/Select") public class Select extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Select() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String content = request.getParameter("content"); String way = request.getParameter("way"); ArrayList<User> list=null; UserDao dao=new UserDao(); list=dao.select(content,way); request.setAttribute("list", list); request.getRequestDispatcher("index.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
④dao
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import bean.User; import util.DBUtil; public class UserDao { public int adduser(User bean) { int i=0; Connection con=null; PreparedStatement pstmt=null; ResultSet rs=null; try { con=DBUtil.getConn(); String sql="insert into student(sno,sname,ssex,sbirthday,saddress) values(?,?,?,?,?)"; pstmt=con.prepareStatement(sql); pstmt.setString(1, bean.getSno()); pstmt.setString(2, bean.getSname()); pstmt.setString(3, bean.getSsex()); pstmt.setString(4, bean.getSbirthday()); pstmt.setString(5, bean.getSaddress()); i = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, pstmt, con); } return i; } public ArrayList<User> getall(){ ArrayList<User> list=new ArrayList<>(); Connection con=null; Statement state=null; ResultSet rs=null; try { con=DBUtil.getConn(); String sql="select * from student"; state=con.createStatement(); rs=state.executeQuery(sql); while(rs.next()) { User bean=new User(); bean.setSno(rs.getString("sno")); bean.setSname(rs.getString("sname")); bean.setSsex(rs.getString("ssex"));; bean.setSbirthday(rs.getString("sbirthday")); bean.setSaddress(rs.getString("saddress")); list.add(bean); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DBUtil.close(rs, state, con); return list; } public User searchbysno(String sno) { // TODO Auto-generated method stub Connection con=null; Statement state=null; ResultSet rs=null; User bean=new User(); try { con=DBUtil.getConn(); String sql="select * from student where sno='"+sno+"'"; state=con.createStatement(); rs=state.executeQuery(sql); while(rs.next()) { bean.setSno(rs.getString("sno")); bean.setSname(rs.getString("sname")); bean.setSsex(rs.getString("ssex"));; bean.setSbirthday(rs.getString("sbirthday")); bean.setSaddress(rs.getString("saddress")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DBUtil.close(rs, state, con); return bean; } public int update(User bean) { // TODO Auto-generated method stub int i=0; Connection con=null; ResultSet rs=null; PreparedStatement pstmt=null; try { con=DBUtil.getConn(); String sql="update student set sno=?,sname=?,ssex=?,sbirthday=?,saddress=? where sno=?"; pstmt=con.prepareStatement(sql); pstmt.setString(1, bean.getSno()); pstmt.setString(2, bean.getSname()); pstmt.setString(3, bean.getSsex()); pstmt.setString(4, bean.getSbirthday()); pstmt.setString(5, bean.getSaddress()); pstmt.setString(6, bean.getSno()); i = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, pstmt, con); } return i; } public int del(String sno) { Connection con=null; ResultSet rs=null; PreparedStatement pstmt=null; int i=0; try { con=DBUtil.getConn(); String sql="delete from student where sno='" + sno + "'"; pstmt=con.prepareStatement(sql); i = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, pstmt, con); } return i; } public ArrayList<User> select(String content,String way){ ArrayList<User> list=new ArrayList<>(); Connection con=null; Statement state=null; ResultSet rs=null; String sql="select * from student"; if(content!=""&&"1".equals(way)){ sql +=" where sno='"+content+"'"; }else if(content!=""&&"2".equals(way)){ sql += " where sname like'%" + content + "%'"; }else if(content!=""&&"3".equals(way)){ sql += " where ssex='"+content+"'"; }else if(content!=""&&"4".equals(way)){ sql += " where saddress='"+content+"'"; }else{ sql ="select * from student"; } try { con=DBUtil.getConn(); state=con.createStatement(); rs=state.executeQuery(sql); while(rs.next()) { User bean=new User(); bean.setSno(rs.getString("sno")); bean.setSname(rs.getString("sname")); bean.setSsex(rs.getString("ssex"));; bean.setSbirthday(rs.getString("sbirthday")); bean.setSaddress(rs.getString("saddress")); list.add(bean); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DBUtil.close(rs, state, con); return list; } }
五、运行截图