阅读量:6
目录
一、系统介绍
1.开发的环境
开发工具:eclipse 2022
JDK版本:1.8
数据库:MySQL 5.5
数据库管理软件:Navicat 15.0.27
服务器:apach-tomcat-9.0.68
2.本系统实现的功能
管理员功能:登陆系统、用户信息的增删改查、图书的增删改查、图书分类的增删改查、图书日志查询、图书归还。
用户功能:登陆系统、查阅图书、借阅和归还图书、查询借阅记录
3.数据库用到的表
admin、book、booktype、history
4.工程截图
二、系统展示
1.登录页面
2.注册页面
3.管理员主页面
4. 用户主页面
5.图书管理页面(管理员)
6.读者管理页面(管理员)
7.图书分类管理页面(管理员)
8.图书借阅管理页面(管理员)
9.图书归还信息页面(管理员)
10.图书日志查询(管理员)
11. 图书借阅页面(用户)
12.还书页面(用户)
13.借阅历史页面(用户)
三、部分代码展示
AdminDao.java
package com.hry.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.hry.bean.AdminBean; import com.hry.dbUtils.DbUtil; public class AdminDao { /** * 登录验证功能,传入用户名和密码,在数据库中查找,如果找到了,返回true,没找到则返回false * username、password */ public boolean Login_verify(String username,String password){ //连接数据库 DbUtil dbUtil=new DbUtil(); Connection conn = dbUtil.getConn(); //sql语句 String sql = "select * from admin where username='"+username+"' and password='"+password+"'"; PreparedStatement stm = null; ResultSet rs = null; try { //预编译SQL,减少sql执行 stm = conn.prepareStatement(sql); rs = stm.executeQuery(); if(rs.next()){ return true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DbUtil.CloseDB(rs, stm, conn); } return false; } /** * 注册账号的函数,传入账号,密码,姓名,邮箱,手机号,借阅天数,可借阅数 * username, password, name, email, phone, lend_num, max_num */ public void Register(String username, String password, String name, String email, String phone,int lend_num,int max_num) { // TODO Auto-generated method stub //连接数据库 DbUtil dbUtil=new DbUtil(); Connection conn = dbUtil.getConn(); //sql语句 String sql = "insert into admin(status,username,password,name,email,phone,lend_num,max_num) values(?,?,?,?,?,?,?,?)"; int rs = 0; PreparedStatement stm = null; try { //预编译SQL,减少sql执行 stm = conn.prepareStatement(sql); //传参 stm.setInt(1, 1); stm.setString(2, username); stm.setString(3, password); stm.setString(4, name); stm.setString(5, email); stm.setString(6, phone); stm.setInt(7, lend_num);//借阅天数 stm.setInt(8, max_num);//可借阅数 //执行更新 rs = stm.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 根据传入的账号,密码,来查找对应的读者信息,返回一个AdminBean类型, * */ public AdminBean getAdminInfo(String username, String password) { // TODO Auto-generated method stub AdminBean adminbean = new AdminBean(); DbUtil dbUtil=new DbUtil(); Connection conn = dbUtil.getConn(); String sql = "select * from admin where username='"+username+"' and password='"+password+"'"; PreparedStatement stm = null; ResultSet rs = null; try { //预编译SQL,减少sql执行 stm = conn.prepareStatement(sql); //执行查询 rs = stm.executeQuery(); if(rs.next()){ //传参 adminbean.setAid(rs.getInt("aid")); adminbean.setUsername(rs.getString("username")); adminbean.setName(rs.getString("name")); adminbean.setPassword(rs.getString("password")); adminbean.setEmail(rs.getString("email")); adminbean.setPhone(rs.getString("phone")); adminbean.setStatus(rs.getInt("status")); adminbean.setLend_num(rs.getInt("lend_num")); adminbean.setMax_num(rs.getInt("max_num")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DbUtil.CloseDB(rs, stm, conn); } return adminbean; } /** * 获取全部用户的信息,其中sql语句中的status=1,表示只查找读者(1),不显示管理员(2)的 * */ public ArrayList<AdminBean> get_ListInfo(){ ArrayList<AdminBean> tag_Array = new ArrayList<AdminBean>(); Connection conn = DbUtil.getConn(); String sql = "select * from admin where status=1"; PreparedStatement stm = null; ResultSet rs = null; try { stm = conn.prepareStatement(sql); rs = stm.executeQuery(); while(rs.next()){ AdminBean adminbean = new AdminBean(); adminbean.setAid(rs.getInt("aid")); adminbean.setUsername(rs.getString("username")); adminbean.setName(rs.getString("name")); adminbean.setPassword(rs.getString("password")); adminbean.setEmail(rs.getString("email")); adminbean.setPhone(rs.getString("phone")); adminbean.setStatus(rs.getInt("status")); adminbean.setLend_num(rs.getInt("lend_num")); adminbean.setMax_num(rs.getInt("max_num")); tag_Array.add(adminbean); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DbUtil.CloseDB(rs, stm, conn); } return tag_Array; } /** * 根据传入的aid图书号,查找到对应的读者的全部信息,返回一个AdminBean类型的数据,与上一个相似,只是aid的类型为String, * */ public AdminBean get_AidInfo2(String aid){ AdminBean adminbean = new AdminBean(); DbUtil dbUtil=new DbUtil(); Connection conn = dbUtil.getConn(); String sql = "select * from admin where aid="+aid; PreparedStatement stm = null; ResultSet rs = null; try { stm = conn.prepareStatement(sql); rs = stm.executeQuery(); if(rs.next()){ adminbean.setAid(rs.getInt("aid")); adminbean.setUsername(rs.getString("username")); adminbean.setName(rs.getString("name")); adminbean.setPassword(rs.getString("password")); adminbean.setEmail(rs.getString("email")); adminbean.setPhone(rs.getString("phone")); adminbean.setStatus(rs.getInt("status")); adminbean.setLend_num(rs.getInt("lend_num")); adminbean.setMax_num(rs.getInt("max_num")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DbUtil.CloseDB(rs, stm, conn); } return adminbean; } /** * 修改读者的信息 */ public void updateUser(int aid, String username, String password, String name, String email, String phone, int lend_num, int max_num) { // TODO Auto-generated method stub DbUtil dbUtil=new DbUtil(); Connection conn = dbUtil.getConn(); String sql = "update admin set username=?,name=?,email=?,phone=?,password=?,lend_num=?,max_num=? where aid=?"; PreparedStatement stm = null; try { stm = conn.prepareStatement(sql); stm.setString(1, username); stm.setString(2, name); stm.setString(3, email); stm.setString(4, phone); stm.setString(5, password); stm.setInt(6, lend_num); stm.setInt(7, max_num); stm.setInt(8, aid); stm.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 删除用户的信息,根据传入的aid作为条件 * */ public void deleteUser(int aid) { // TODO Auto-generated method stub DbUtil dbUtil=new DbUtil(); Connection conn = dbUtil.getConn(); String sql = "delete from admin where aid=?"; PreparedStatement stm = null; try { stm = conn.prepareStatement(sql); stm.setInt(1, aid); stm.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
LoginServlet.java
package com.hry.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; 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 com.hry.bean.AdminBean; import com.hry.dao.AdminDao; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; AdminDao userdao = new AdminDao(); AdminDao admindao = new AdminDao(); /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //登录的判断 //编码格式 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); //获取账号和密码 // PrintWriter out = response.getWriter(); String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username); //对账号和密码进行判断 boolean result = userdao.Login_verify(username, password); System.out.println(result); HttpSession session = request.getSession(); //判断输入正确 if(result){ AdminBean adminbean = new AdminBean(); //更加账号和密码查找出读者的信息 adminbean = admindao.getAdminInfo(username,password); //将aid存入session中 session.setAttribute("aid", ""+adminbean.getAid()); //设置session的失效时间 session.setMaxInactiveInterval(6000); //根据status的值来判断是管理员,还是读者,status=1为读者 System.out.println(adminbean.getStatus()); if(adminbean.getStatus()==1){ response.sendRedirect("index2.jsp"); }else{ response.sendRedirect("admin.jsp"); } }else{ //没有找到对应的账号和密码,返回重新登录 session.setAttribute("state", "密码错误"); response.sendRedirect("login.jsp"); } } }
RegisterServlet.java
package com.hry.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 com.hry.bean.AdminBean; import com.hry.dao.AdminDao; /** * Servlet implementation class RegisterServlet */ @WebServlet("/RegisterServlet") public class RegisterServlet extends HttpServlet { private static final long serialVersionUID = 1L; public RegisterServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } /** * @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); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); //获取注册信息 AdminDao userdao = new AdminDao(); String username = request.getParameter("username"); String password = request.getParameter("password"); String name = request.getParameter("name"); String email = request.getParameter("email"); String phone = request.getParameter("phone"); System.out.println(username); //默认设置最大借阅数和借阅天数 int lend_num = 30; int max_num = 5; //将注册信息存入数据库,再返回登录 userdao.Register(username,password,name,email,phone,lend_num,max_num); response.sendRedirect("login.jsp"); } }
BookBean.java
package com.hry.bean; public class BookBean { /** * 图书的数据表的bean */ private int bid;//图书id private String name;//图书名称 private String card;//图书号 private String autho;//作者 private int num;//图书数量 private String type;//图书的分类 private String press;//出版社 public int getBid() { return bid; } public void setBid(int bid) { this.bid = bid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCard() { return card; } public void setCard(String card) { this.card = card; } public String getAutho() { return autho; } public void setAutho(String autho) { this.autho = autho; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getPress() { return press; } public void setPress(String press) { this.press = press; } }
AdminServlet.java
package com.hry.servlet; import java.io.IOException; import java.io.PrintWriter; 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 com.hry.bean.AdminBean; import com.hry.dao.AdminDao; import com.hry.dao.BookDao; /** * Servlet implementation class AdminServlet */ @WebServlet("/AdminServlet") public class AdminServlet extends HttpServlet { private static final long serialVersionUID = 1L; public AdminServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //PrintWriter:输出流,为异常处理 PrintWriter out = response.getWriter(); //设置编码类型 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); BookDao bookdao = new BookDao(); //这里为了简单,设置了tip,用来区分是修改密码功能,还是修改个人资料的功能,tip=1为修改密码 int tip = Integer.parseInt(request.getParameter("tip")); //获取发起请求页面的文件名称,这个在对应的jsp里面的表单填写,修改完成后就可以直接返回对应的页面 String url = request.getParameter("url"); System.out.println(url); HttpSession session = request.getSession(); AdminBean adminbean = new AdminBean(); //获取存到session的aid String aid = (String) session.getAttribute("aid"); AdminDao admindao = new AdminDao(); //通过aid获取到读者的信息 adminbean = admindao.get_AidInfo2(aid); //修改密码 if (tip==1) { //获取到输入的旧密码,新密码 String password = request.getParameter("password"); String password2 = request.getParameter("password2"); //获取读者数据表中的密码 String old_password = adminbean.getPassword(); //对旧密码进行比较,如果相同就修改,不相同就直接退出 if(old_password.equals(password)){ admindao.updateUser(adminbean.getAid(), adminbean.getUsername(), password2, adminbean.getName(), adminbean.getEmail(), adminbean.getPhone(), adminbean.getLend_num(), adminbean.getMax_num()); response.sendRedirect(url+".jsp"); }else{ out.write("<script type='text/javascript'>alert('password error');location.href='"+url+".jsp'; </script>"); } } else { //修改个人资料 //获取输入的信息 String name = request.getParameter("name"); String email = request.getParameter("email"); String phone = request.getParameter("phone"); //修改输入的信息到数据表中 admindao.updateUser(adminbean.getAid(), adminbean.getUsername(), adminbean.getPassword(), name, email, phone, adminbean.getLend_num(), adminbean.getMax_num()); response.sendRedirect(url+".jsp"); } } }
四、源码
点击以下链接(内含数据库文件——books.sql):
链接:https://pan.baidu.com/s/1HN8CY281ctJWjSkynUgxag?pwd=5ugw
提取码:5ugw
五、备注
该项目是基于一位GitHub上的大佬的项目模板写的,我在上课时按照老师的要求给原项目增加和优化了一些功能,最后变成了现在这个样子。由于当时在GitHub参考了很多的项目,导致我现在已经不记得那位大佬的用户名了,只能说是非常抱歉了。
如有侵权,请联系我删除。
六、祝福
路虽远行则可至,未来一定可期!祝您生活愉快、工作顺心、学业有成!