项目源码及数据库:
链接:https://pan.baidu.com/s/1ktUyxbOI9lljWr-HRTRIiQ?pwd=1024
提取码:1024
目录
一.项目介绍
二.运行效果
1.登录界面
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
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>学生信息管理系统-登录界面
*{margin:0;padding:0;} a{text-decoration:none;color:#666;} a:hover{ text-decoration:underline; color:E4393C; } html,body { font:12px/150% Arial,Verdana; } .wrap{ width:1000px; margin:0 auto; } .left{ float:left; } .content{ background:url(../image/login.jpg); background-size: cover; width:1280px; height:559px; } .login-frame{ margin:50px 5% 50px 5%; float:right; padding:60px; background:white; background-color:rgba(255,255,255,0.9); border-radius:25px; order-right:1px #bdbdbd solid; width:280px; height:230px; } .login-frame h2{ font-size:25px; height:40px; margin-buttom:25px; } .login-frame h2 a{ font-size:15px; color:#59c2c5; padding-left:20px; background:url(../image/icon5.jpg)no-repeat; } .login-frame .item{ height:60px; margin-buttom:40px; } .login-frame .item input{ line-height:40px; width:260px; border:none; border-bottom: 1px solid #59c2c5; } .login-btn{ display:block; height:50px;display:block; height:50px; color:#fff; background:#59c2c5; width:265px; font-size:16px; line-height:30px; text-align:center; border-radius:10px; border:none; color:#fff; background:#59c2c5; width:265px; font-size:16px; line-height:30px; text-align:center; border-radius:10px; border:none; } .login-frame .item1{ dislpay:flex; justify-content: space-between; margin-top:1 rem; } .login-frame .item1 a{ line-height:40px; font-size:1.1rem; margin-top:5 rem; padding:1rem 3rem; }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>注册账号
*{margin:0;padding:0;} html,body { font:12px/150% Arial,Verdana; } .content{ background:url(../image/login.jpg); background-size: cover; width:1280px; height:559px; } .box{ margin:60px 18% 60px 18%; float:right; padding:30px; background:white; background-color:rgba(255,255,255,0.9); border-radius:15px; } .item{ height:60px; margin-buttom:40px; } .item input{ line-height:40px; width:260px; border:none; border-bottom: 1px solid #59c2c5; border-radius:3px; } .item1{ font-size:15px; height:40px; } .btn{ display:block; height:50px; color:#fff; background:#59c2c5; width:265px; font-size:16px; line-height:30px; text-align:center; border-radius:10px; border:none; }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>更改账号密码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>注销账号
点击学号进入单个学生信息展示界面,可修改学生信息
点击删除删除学生信息
<%@page import="student.entity.Page"%> <%@page import="java.util.List"%> <%@page import="student.entity.Student"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>学生信息列表
学号 |
姓名 |
年龄 |
专业 |
操作 |
---|---|---|---|---|
"><%=student.getSno() %> | <%=student.getName() %> | <%=student.getAge() %> | <%=student.getDept() %> | ">删除 |
*{margin:0;padding:0;} html,body { font:12px/150% Arial,Verdana; } .content{ background:url(../image/index.JPG); background-size: cover; width:1280px; height:559px; } .box{ margin:60px 18% 60px 18%; float:right; padding:30px; background:white; background-color:rgba(255,255,255,0.9); border-radius:15px; } .item{ height:40px; border-radius:15px; } .item a{ background-color: lightgrey; text-decoration: none; font-size:15px; color:black; border-radius:3px; } .item1{ font-size:15px; height:40px; } table{ width:100%; border-collapse:collapse; } table body{ diaplay:block; height:300px; overflow-y: scroll; } table td{ border:1px solid #A6A6A6; height:60px; width:300px; text-align: center; font-size: 15px; } table th{ height:60px; border:1px solid #A6A6A6; }
<%@page import="student.entity.Student"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>修改学生信息 <% Student student = (Student)request.getAttribute("student"); %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>增加学生信息
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>上传作业
检查登录的用户名和密码是否匹配(CheckLoginServlet.java)
package student.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import student.service.IStudentService; import student.service.impl.StudentServiceImpl; public class CheckLoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String ID = request.getParameter("UID"); String pwd = request.getParameter("upwd"); IStudentService service = new StudentServiceImpl(); boolean result = service.checkLoginID(ID,pwd); response.setContentType("text/html;charest=UTF-8"); response.setCharacterEncoding("utf-8"); if(!result) { request.setAttribute("error", "loginError"); }else { request.setAttribute("error", "nologinError"); } request.getRequestDispatcher("login.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
注册账户表示层(AddLoginIDServlet.java)
package student.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import student.service.IStudentService; import student.service.impl.StudentServiceImpl; /** * Servlet implementation class AddLoginIDServlet */ public class AddLoginIDServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String ID = request.getParameter("UID"); String pwd = request.getParameter("upwd"); IStudentService studentService = new StudentServiceImpl(); boolean result = studentService.addLoginID(ID,pwd); response.setContentType("text/html;charest=UTF-8"); response.setCharacterEncoding("utf-8"); if(!result) { request.setAttribute("error4", "addError"); request.getRequestDispatcher("register.jsp").forward(request, response); }else { request.setAttribute("error4", "noaddError"); request.getRequestDispatcher("login.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); } }
修改密码表示层(UpdateLoginPwdServlet.java)
package student.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import student.service.IStudentService; import student.service.impl.StudentServiceImpl; /** * Servlet implementation class UpdateLoginPwdServlet */ public class UpdateLoginPwdServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String ID = request.getParameter("UID"); String pwd = request.getParameter("upwd"); String pwd1 = request.getParameter("upwd1"); IStudentService service = new StudentServiceImpl(); boolean result = service.checkLoginID(ID,pwd); response.setContentType("text/html;charest=UTF-8"); response.setCharacterEncoding("utf-8"); if(!result) { request.setAttribute("error0", "loginError"); request.getRequestDispatcher("UpdateLoginPwd.jsp").forward(request, response); }else { request.setAttribute("error0", "nologinError"); boolean result1 = service.updateLoginPwd(ID,pwd1); if(!result1) { request.getRequestDispatcher("UpdateLoginPwd.jsp").forward(request, response); }else { request.setAttribute("error1", "noupdateError"); request.getRequestDispatcher("login.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); } }
注销用户表示层(DeleteLoginIDServlet.java)
package student.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import student.service.IStudentService; import student.service.impl.StudentServiceImpl; /** * Servlet implementation class DeleteLoginIDServlet */ public class DeleteLoginIDServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String ID = request.getParameter("UID"); String pwd = request.getParameter("upwd"); IStudentService service = new StudentServiceImpl(); boolean result = service.checkLoginID(ID,pwd); boolean result1 = service.deleteLoginID(ID); response.setContentType("text/html;charest=UTF-8"); response.setCharacterEncoding("utf-8"); if(!result) { request.setAttribute("error2", "loginError"); request.getRequestDispatcher("DeleteLoginID.jsp").forward(request, response); }else { request.setAttribute("error2", "nologinError"); if(!result1) { request.setAttribute("error3", "deleteError"); request.getRequestDispatcher("DeleteLoginID.jsp").forward(request, response); }else { request.setAttribute("error3", "nodeleteError"); request.getRequestDispatcher("login.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); } }
分页显示(QueryStudentByPageServlet.java)
package student.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import student.entity.Page; import student.entity.Student; import student.service.IStudentService; import student.service.impl.StudentServiceImpl; public class QueryStudentByPageServlet extends HttpServlet { private static final long serialVersionUID = 1L; public QueryStudentByPageServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { IStudentService studentService = new StudentServiceImpl(); int count = studentService.getTotalCount(); Page page = new Page(); String cPage = request.getParameter("currentPage"); if(cPage == null) { cPage = "0"; } int currentPage = Integer.parseInt(cPage); page.setCurrentPage(currentPage); int totalCount = studentService.getTotalCount(); page.setTotalCount(totalCount); int pageSize = 4; page.setPageSize(pageSize); Liststudents = studentService.queryStudentsByPage(currentPage, pageSize); page.setStudents(students); request.setAttribute("page1", page); 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); } }
按学号查询学生信息(QueryStudentBySnoServlet.java)
package student.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import student.entity.Student; import student.service.IStudentService; import student.service.impl.StudentServiceImpl; public class QueryStudentBySnoServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); int sno = Integer.parseInt(request.getParameter("sno")); IStudentService service = new StudentServiceImpl(); Student student = service.queryStudentBySno(sno); request.setAttribute("student", student);//将查询到的request信息放在request域中 request.getRequestDispatcher("StudentInfo.jsp").forward(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
增加学生信息(AddStudentServlet.java)
package student.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import student.entity.Student; import student.service.IStudentService; import student.service.impl.StudentServiceImpl; public class AddStudentServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); int sno = Integer.parseInt(request.getParameter("sno")); int age = Integer.parseInt(request.getParameter("age")); String dept = request.getParameter("dept"); Student student = new Student(name, sno, age, dept); IStudentService studentService = new StudentServiceImpl(); boolean result = studentService.addStudent(student); response.setContentType("text/html;charest=UTF-8"); response.setCharacterEncoding("utf-8"); if(!result) { request.setAttribute("error", "addError"); }else { request.setAttribute("error", "noaddError"); } request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
删除学生信息(DeleteStudentServlet.java)
package student.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import student.service.IStudentService; import student.service.impl.StudentServiceImpl; /** * Servlet implementation class DeleteStudentServlet */ public class DeleteStudentServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); int sno = Integer.parseInt(request.getParameter("sno")); IStudentService service = new StudentServiceImpl(); boolean result = service.deleteStudentBySno(sno); response.setContentType("text/html;charest=UTF-8"); response.setCharacterEncoding("utf-8"); if(!result) { request.setAttribute("error1", "deleteError"); }else { request.setAttribute("error1", "nodeleteError"); } request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
修改学生信息表示层(UpdateStudentServlet.java)
package student.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import student.entity.Student; import student.service.IStudentService; import student.service.impl.StudentServiceImpl; public class UpdateStudentServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); int sno = Integer.parseInt(request.getParameter("sno")); int age = Integer.parseInt(request.getParameter("age")); String dept = request.getParameter("dept"); Student student = new Student(name, age, dept); System.out.println(sno); IStudentService service = new StudentServiceImpl(); boolean result = service.updateStudentBySno(sno, student); response.setContentType("text/html;charest=UTF-8"); response.setCharacterEncoding("utf-8"); if(!result) { request.setAttribute("error2", "updateError"); }else { request.setAttribute("error2", "noupdateError"); } request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
上传作业(UploadServlet.java)
package student.servlet; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import student.service.IStudentService; import student.service.impl.StudentServiceImpl; public class UploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String name = null; int sno = -1; boolean isMutipart = ServletFileUpload.isMultipartContent(request); if(isMutipart) { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); factory.setRepository(new File("D:\\uploadtemp")); Listitems = null; try { items = upload.parseRequest(request); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } Iterator iter = items.iterator(); while(iter.hasNext()) { FileItem item= iter.next(); String fileName = item.getName(); String itemName = item.getFieldName(); if(item.isFormField()) { if(itemName.equals("name")){ name = item.getString("utf-8"); }else if(itemName.equals("sno")) { sno =Integer.parseInt(item.getString("utf-8")); }else { } }else { //定义上传路径:指定上传的位置 String path = "D:\\upload"; File file = new File(path,fileName); try { item.write(file); } catch (Exception e) { e.printStackTrace(); } } } IStudentService studentService = new StudentServiceImpl(); boolean result = studentService.upLoadWork(sno,name); System.out.println(name+sno); System.out.println(result); if(!result) { request.setAttribute("error3", "uploadError"); request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response); }else{ request.setAttribute("error3", "nouploadError"); request.getRequestDispatcher("QueryStudentByPageServlet").forward(request, response); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
Service接口(IStudentService.java)
package student.service; import java.util.List; import student.entity.Student; public interface IStudentService { //查询全部学生信息 public ListqueryAllStudents(); //按照学号查询学生信息 public Student queryStudentBySno(int sno); //删除学生信息 public boolean deleteStudentBySno(int sno) ; //更改学生信息 public boolean updateStudentBySno(int sno, Student student) ; //增加学生信息 public boolean addStudent(Student student) ; //查询总数据 public int getTotalCount(); //分页 public List queryStudentsByPage(int current, int pageSize); //检查登陆账户和密码 public boolean checkLoginID(String ID,String pwd); //注册账户 public boolean addLoginID(String ID, String pwd); //更改密码 public boolean updateLoginPwd(String ID,String pwd1); //注销账号 public boolean deleteLoginID(String ID); //判断ID是否存在 public boolean IDExist(String ID); //判断上传作业输入的学生信息是否存在 public boolean upLoadWork(int sno, String name); }
接口的实现类(StudentServiceImpl.java)
package student.service.impl; import java.util.List; import student.dao.IStudentDao; import student.dao.impl.StudentDaoImpl; import student.entity.Student; import student.service.IStudentService; import student.util.DBUtil; //业务逻辑层:逻辑性的增删改查(增:查+增),对dao层进行的组装 public class StudentServiceImpl implements IStudentService{ IStudentDao studentDao = new StudentDaoImpl(); //查询全部学生信息 public ListqueryAllStudents(){ return studentDao.queryAllStudents(); } //按照学号查询学生信息 public Student queryStudentBySno(int sno) { return studentDao.queryStudentBySno(sno); } //删除学生信息 public boolean deleteStudentBySno(int sno) { if(studentDao.isExist(sno)) { return studentDao.deleteStudentBySno(sno); } return false; } //更改学生信息 public boolean updateStudentBySno(int sno, Student student) { return studentDao.updateStudentBySno(sno, student); } //增加学生信息 public boolean addStudent(Student student) { if(!studentDao.isExist(student.getSno())) { studentDao.addStudent(student); return true; }else { System.out.println("学号重复!"); return false; } } //查询总条数 @Override public int getTotalCount() { return studentDao.getTotalCount(); } //查询当前页的数据集合 @Override public List queryStudentsByPage(int current, int pageSize) { return studentDao.queryStudentByPage(current, pageSize); } @Override public boolean checkLoginID(String ID, String pwd) { return studentDao.checkLoginID(ID, pwd); } @Override public boolean addLoginID(String ID, String pwd) { return studentDao.addLoginID(ID,pwd); } @Override public boolean updateLoginPwd(String ID, String pwd1) { return studentDao.updateLoginPwd(ID,pwd1); } @Override public boolean deleteLoginID(String ID) { return studentDao.deleteLoginID(ID); } @Override public boolean IDExist(String ID) { return studentDao.IDExist(ID); } @Override public boolean upLoadWork(int sno, String name) { return studentDao.upLoadWork(sno,name); } }
接口(IStudentDao.java)
package student.dao; import java.util.List; import student.entity.Student; public interface IStudentDao { public boolean updateLoginPwd(String ID,String pwd1); //查询全部学生信息 public ListqueryAllStudents(); //判断此人是否存在 public boolean isExist(int sno) ; //增加学生信息 public boolean addStudent(Student student); //删除学生信息 public boolean deleteStudentBySno(int sno); //根据sno找到要修改的学生,然后再进行修改 public boolean updateStudentBySno(int sno,Student student); //根据学号查询学生信息 public Student queryStudentBySno(int sno); //查询总数据数 public int getTotalCount(); //currentPage:当前页(页码)pageSize:页面大小(每页显示的数据条数) public List queryStudentByPage(int currentPage,int pageSize); public boolean checkLoginID(String ID,String pwd); public boolean addLoginID(String ID, String pwd); public boolean deleteLoginID(String ID); public boolean IDExist(String ID); public boolean upLoadWork(int sno, String name); }
接口的实现类(StudentDaoImpl.java)
package student.dao.impl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import student.dao.IStudentDao; import student.entity.Student; import student.util.DBUtil; public class StudentDaoImpl implements IStudentDao{ private final String URL = "jdbc:mysql://localhost:3306/STUDENT?useSSL=false&serverTimezone=UTC"; private final String UserName = "root"; private final String Pwd = "123456"; String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; //查询全部学生信息 public ListqueryAllStudents(){ PreparedStatement pstmt = null; Student student = null; List students = new ArrayList<>(); ResultSet rs = null; try { String sql = "select * from student1"; rs = DBUtil.executeQuery(sql, null); while(rs.next()) { int sno= rs.getInt("sno"); String name = rs.getString("name"); int age = rs.getInt("age"); String dept = rs.getString("dept"); student = new Student(name, sno, age, dept); students.add(student); } return students; } catch(Exception e) { e.printStackTrace(); return null; } finally { DBUtil.closeAll(rs, pstmt, DBUtil.connection); } } //判断此人是否存在 public boolean isExist(int sno) { return queryStudentBySno(sno) == null? false:true; } //增加学生信息 public boolean addStudent(Student student) { String sql = "insert into student1(name,sno,age,dept) values(?,?,?,?)"; Object[] params = {student.getName(),student.getSno(),student.getAge(),student.getDept()}; return DBUtil.executeUpdate(sql, params); } //删除学生信息 public boolean deleteStudentBySno(int sno) { String sql = "delete from student1 where sno =?"; Object[] params = {sno}; return DBUtil.executeUpdate(sql, params); } //根据sno找到要修改的学生,然后再进行修改 public boolean updateStudentBySno(int sno,Student student) { String sql = "update student1 set name =?,age=?,dept=? where sno=?"; Object[] params = {student.getName(),student.getAge(),student.getDept(),sno}; return DBUtil.executeUpdate(sql, params); } //根据学号查询学生信息 public Student queryStudentBySno(int sno){ PreparedStatement pstmt = null; Student student = null; Connection connection = null; ResultSet rs = null; try { Class.forName(JDBC_DRIVER); connection = DriverManager.getConnection(URL,UserName,Pwd); String sql = "select * from student1 where sno = ?"; pstmt = connection.prepareStatement(sql); pstmt.setInt(1, sno); rs = pstmt.executeQuery(); if(rs.next()) { int no= rs.getInt("sno"); String name = rs.getString("name"); int age = rs.getInt("age"); String dept = rs.getString("dept"); student = new Student(name, no, age, dept); } return student; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch(SQLException e) { e.printStackTrace(); return null; }catch(Exception e) { e.printStackTrace(); return null; } finally { DBUtil.closeAll(rs, pstmt, DBUtil.connection); } } @Override public int getTotalCount() {//查询总数据数 String sql = "select count(1) from student1"; return DBUtil.getTotalCount(sql); } @Override public List queryStudentByPage(int currentPage, int pageSize) { String sql = "select * from student1 order by sno asc limit ?,?"; Object[] params = {currentPage*pageSize,pageSize}; List students = new ArrayList<>(); ResultSet rs = DBUtil.executeQuery(sql, params); try { while(rs.next()) { Student student = new Student(rs.getString("name"),rs.getInt("sno"),rs.getInt("age"),rs.getString("dept")); students.add(student); } } catch (SQLException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } return students; } @Override public boolean checkLoginID(String ID, String pwd){ int count = 0; String sql = "select * from login where ID=? and pwd=?"; Object[] params = {ID,pwd}; ResultSet rs = DBUtil.executeQuery(sql, params); try { while(rs.next()) { count++; } if(count>0) return true; else return false; } catch (SQLException e) { e.printStackTrace(); } return false; } @Override public boolean addLoginID(String ID, String pwd) { // TODO Auto-generated method stub String sql = "insert into login(ID,pwd) values(?,?)"; Object[] params = {ID,pwd}; return DBUtil.executeUpdate(sql, params); } @Override public boolean updateLoginPwd(String ID, String pwd1) { String sql = "update login set pwd =? where ID=?"; Object[] params = {pwd1,ID}; return DBUtil.executeUpdate(sql, params); } @Override public boolean deleteLoginID(String ID) { String sql = "delete from login where ID =?"; Object[] params = {ID}; return DBUtil.executeUpdate(sql, params); } @Override public boolean IDExist(String ID) { String sql = "select *from login where ID = ?"; Object[] params = {ID}; return DBUtil.executeUpdate(sql, params); } @Override public boolean upLoadWork(int sno, String name) { int count = 0; String sql = "select *from student1 where sno = ? and name = ?"; Object[] params = {sno,name}; ResultSet rs = DBUtil.executeQuery(sql, params); try { while(rs.next()) { count++; } if(count>0) return true; else return false; } catch (SQLException e) { e.printStackTrace(); } return false; } }
package student.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import student.entity.Student; //通用的数据库操作方法 public class DBUtil { private static final String URL = "jdbc:mysql://localhost:3306/STUDENT?useSSL=false&serverTimezone=UTC"; private static final String UserName = "root"; private static final String Pwd = "123456"; private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; public static Connection connection = null; public static PreparedStatement pstmt = null; public static ResultSet rs = null; //查询总数 public static int getTotalCount(String sql){ int count = -1; try { pstmt = createPreParedStatement(sql,null); rs = pstmt.executeQuery(); if(rs.next()) { count = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); }finally { closeAll(rs,pstmt,connection); } return count; } //增删改 public static boolean executeUpdate(String sql,Object[] params) { try { pstmt = createPreParedStatement(sql,params); int count = pstmt.executeUpdate(); System.out.println(count); if(count>0) { return true; } else { return false; } } catch (ClassNotFoundException e) { e.printStackTrace(); return false; } catch(SQLException e) { e.printStackTrace(); return false; }catch(Exception e) { e.printStackTrace(); return false; } finally { closeAll(null,pstmt,connection); } } public static void closeAll(ResultSet rs,Statement stmt,Connection connection){ try{ if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close(); if(connection!=null)connection.close(); }catch(SQLException e) { e.printStackTrace(); } } public static PreparedStatement createPreParedStatement(String sql,Object[] params) throws ClassNotFoundException, SQLException { pstmt = getConnection().prepareStatement(sql); if(params!=null) { for(int i = 0;istudents = new ArrayList<>(); Student student = null; try { pstmt = createPreParedStatement(sql,params); rs = pstmt.executeQuery(); return rs; } catch(SQLException e) { e.printStackTrace(); return null; }catch(Exception e) { e.printStackTrace(); return null; } } }
package student.entity; import java.util.List; //分页帮助类 public class Page { private int currentPage; private int pageSize; private int totalCount; private int totalPage; private Liststudents; public Page() { } public Page(int currentPage, int pageSize, int totalCount, int totalPage, List students) { this.currentPage = currentPage; this.pageSize = pageSize; this.totalCount = totalCount; this.totalPage = totalPage; this.students = students; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; this.totalPage = this.totalCount%this.pageSize==0?this.totalCount/this.pageSize:this.totalCount/this.pageSize+1; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { return totalPage; } public List getStudents() { return students; } public void setStudents(List students) { this.students = students; } }
package student.entity; public class Student { private String name; private int sno; private int age; private String dept; public Student(int sno) { this.sno = sno; } public Student() { } public Student(String name, int age, String dept) { this.name = name; this.age = age; this.dept = dept; } public Student(String name, int sno, int age, String dept) { this.name = name; this.sno = sno; this.age = age; this.dept = dept; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSno() { return sno; } public void setSno(int sno) { this.sno = sno; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public String toString() { return this.getSno()+"-"+this.getName()+"-"+this.getAge()+"-"+this.getDept(); } }
项目需要3个jar包,前两个jar包属于文件上传所需,最后一个为连接数据库的jar包
下载官网地址:https://mvnrepository.com/
本人用的是mysql数据库,直接在mysql数据库中新建表格