当前位置:   article > 正文

JavaWeb期末大作业——图书管理系统

javaweb期末大作业

目录

一、系统介绍

1.开发的环境

2.本系统实现的功能

3.数据库用到的表

4.工程截图

二、系统展示

1.登录页面

2.注册页面

3.管理员主页面

​编辑 

4. 用户主页面

 5.图书管理页面(管理员)

6.读者管理页面(管理员)

​编辑 7.图书分类管理页面(管理员)

8.图书借阅管理页面(管理员)

 9.图书归还信息页面(管理员)

10.图书日志查询(管理员)

​编辑

11. 图书借阅页面(用户)

12.还书页面(用户)

​编辑 13.借阅历史页面(用户)

 三、部分代码展示

AdminDao.java

 LoginServlet.java

 RegisterServlet.java

 BookBean.java

 AdminServlet.java 

四、源码

点击以下链接(内含数据库文件——books.sql):

五、备注 

六、祝福


一、系统介绍

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

  1. package com.hry.dao;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import com.hry.bean.AdminBean;
  8. import com.hry.dbUtils.DbUtil;
  9. public class AdminDao {
  10. /**
  11. * 登录验证功能,传入用户名和密码,在数据库中查找,如果找到了,返回true,没找到则返回false
  12. * username、password
  13. */
  14. public boolean Login_verify(String username,String password){
  15. //连接数据库
  16. DbUtil dbUtil=new DbUtil();
  17. Connection conn = dbUtil.getConn();
  18. //sql语句
  19. String sql = "select * from admin where username='"+username+"' and password='"+password+"'";
  20. PreparedStatement stm = null;
  21. ResultSet rs = null;
  22. try {
  23. //预编译SQL,减少sql执行
  24. stm = conn.prepareStatement(sql);
  25. rs = stm.executeQuery();
  26. if(rs.next()){
  27. return true;
  28. }
  29. } catch (SQLException e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. }
  33. finally {
  34. DbUtil.CloseDB(rs, stm, conn);
  35. }
  36. return false;
  37. }
  38. /**
  39. * 注册账号的函数,传入账号,密码,姓名,邮箱,手机号,借阅天数,可借阅数
  40. * username, password, name, email, phone, lend_num, max_num
  41. */
  42. public void Register(String username, String password, String name, String email, String phone,int lend_num,int max_num) {
  43. // TODO Auto-generated method stub
  44. //连接数据库
  45. DbUtil dbUtil=new DbUtil();
  46. Connection conn = dbUtil.getConn();
  47. //sql语句
  48. String sql = "insert into admin(status,username,password,name,email,phone,lend_num,max_num) values(?,?,?,?,?,?,?,?)";
  49. int rs = 0;
  50. PreparedStatement stm = null;
  51. try {
  52. //预编译SQL,减少sql执行
  53. stm = conn.prepareStatement(sql);
  54. //传参
  55. stm.setInt(1, 1);
  56. stm.setString(2, username);
  57. stm.setString(3, password);
  58. stm.setString(4, name);
  59. stm.setString(5, email);
  60. stm.setString(6, phone);
  61. stm.setInt(7, lend_num);//借阅天数
  62. stm.setInt(8, max_num);//可借阅数
  63. //执行更新
  64. rs = stm.executeUpdate();
  65. } catch (SQLException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. }
  69. }
  70. /**
  71. * 根据传入的账号,密码,来查找对应的读者信息,返回一个AdminBean类型,
  72. *
  73. */
  74. public AdminBean getAdminInfo(String username, String password) {
  75. // TODO Auto-generated method stub
  76. AdminBean adminbean = new AdminBean();
  77. DbUtil dbUtil=new DbUtil();
  78. Connection conn = dbUtil.getConn();
  79. String sql = "select * from admin where username='"+username+"' and password='"+password+"'";
  80. PreparedStatement stm = null;
  81. ResultSet rs = null;
  82. try {
  83. //预编译SQL,减少sql执行
  84. stm = conn.prepareStatement(sql);
  85. //执行查询
  86. rs = stm.executeQuery();
  87. if(rs.next()){
  88. //传参
  89. adminbean.setAid(rs.getInt("aid"));
  90. adminbean.setUsername(rs.getString("username"));
  91. adminbean.setName(rs.getString("name"));
  92. adminbean.setPassword(rs.getString("password"));
  93. adminbean.setEmail(rs.getString("email"));
  94. adminbean.setPhone(rs.getString("phone"));
  95. adminbean.setStatus(rs.getInt("status"));
  96. adminbean.setLend_num(rs.getInt("lend_num"));
  97. adminbean.setMax_num(rs.getInt("max_num"));
  98. }
  99. } catch (SQLException e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. }
  103. finally {
  104. DbUtil.CloseDB(rs, stm, conn);
  105. }
  106. return adminbean;
  107. }
  108. /**
  109. * 获取全部用户的信息,其中sql语句中的status=1,表示只查找读者(1),不显示管理员(2)的
  110. *
  111. */
  112. public ArrayList<AdminBean> get_ListInfo(){
  113. ArrayList<AdminBean> tag_Array = new ArrayList<AdminBean>();
  114. Connection conn = DbUtil.getConn();
  115. String sql = "select * from admin where status=1";
  116. PreparedStatement stm = null;
  117. ResultSet rs = null;
  118. try {
  119. stm = conn.prepareStatement(sql);
  120. rs = stm.executeQuery();
  121. while(rs.next()){
  122. AdminBean adminbean = new AdminBean();
  123. adminbean.setAid(rs.getInt("aid"));
  124. adminbean.setUsername(rs.getString("username"));
  125. adminbean.setName(rs.getString("name"));
  126. adminbean.setPassword(rs.getString("password"));
  127. adminbean.setEmail(rs.getString("email"));
  128. adminbean.setPhone(rs.getString("phone"));
  129. adminbean.setStatus(rs.getInt("status"));
  130. adminbean.setLend_num(rs.getInt("lend_num"));
  131. adminbean.setMax_num(rs.getInt("max_num"));
  132. tag_Array.add(adminbean);
  133. }
  134. } catch (SQLException e) {
  135. // TODO Auto-generated catch block
  136. e.printStackTrace();
  137. }
  138. finally {
  139. DbUtil.CloseDB(rs, stm, conn);
  140. }
  141. return tag_Array;
  142. }
  143. /**
  144. * 根据传入的aid图书号,查找到对应的读者的全部信息,返回一个AdminBean类型的数据,与上一个相似,只是aid的类型为String,
  145. *
  146. */
  147. public AdminBean get_AidInfo2(String aid){
  148. AdminBean adminbean = new AdminBean();
  149. DbUtil dbUtil=new DbUtil();
  150. Connection conn = dbUtil.getConn();
  151. String sql = "select * from admin where aid="+aid;
  152. PreparedStatement stm = null;
  153. ResultSet rs = null;
  154. try {
  155. stm = conn.prepareStatement(sql);
  156. rs = stm.executeQuery();
  157. if(rs.next()){
  158. adminbean.setAid(rs.getInt("aid"));
  159. adminbean.setUsername(rs.getString("username"));
  160. adminbean.setName(rs.getString("name"));
  161. adminbean.setPassword(rs.getString("password"));
  162. adminbean.setEmail(rs.getString("email"));
  163. adminbean.setPhone(rs.getString("phone"));
  164. adminbean.setStatus(rs.getInt("status"));
  165. adminbean.setLend_num(rs.getInt("lend_num"));
  166. adminbean.setMax_num(rs.getInt("max_num"));
  167. }
  168. } catch (SQLException e) {
  169. // TODO Auto-generated catch block
  170. e.printStackTrace();
  171. }
  172. finally {
  173. DbUtil.CloseDB(rs, stm, conn);
  174. }
  175. return adminbean;
  176. }
  177. /**
  178. * 修改读者的信息
  179. */
  180. public void updateUser(int aid, String username, String password, String name, String email, String phone,
  181. int lend_num, int max_num) {
  182. // TODO Auto-generated method stub
  183. DbUtil dbUtil=new DbUtil();
  184. Connection conn = dbUtil.getConn();
  185. String sql = "update admin set username=?,name=?,email=?,phone=?,password=?,lend_num=?,max_num=? where aid=?";
  186. PreparedStatement stm = null;
  187. try {
  188. stm = conn.prepareStatement(sql);
  189. stm.setString(1, username);
  190. stm.setString(2, name);
  191. stm.setString(3, email);
  192. stm.setString(4, phone);
  193. stm.setString(5, password);
  194. stm.setInt(6, lend_num);
  195. stm.setInt(7, max_num);
  196. stm.setInt(8, aid);
  197. stm.executeUpdate();
  198. } catch (SQLException e) {
  199. // TODO Auto-generated catch block
  200. e.printStackTrace();
  201. }
  202. }
  203. /**
  204. * 删除用户的信息,根据传入的aid作为条件
  205. *
  206. */
  207. public void deleteUser(int aid) {
  208. // TODO Auto-generated method stub
  209. DbUtil dbUtil=new DbUtil();
  210. Connection conn = dbUtil.getConn();
  211. String sql = "delete from admin where aid=?";
  212. PreparedStatement stm = null;
  213. try {
  214. stm = conn.prepareStatement(sql);
  215. stm.setInt(1, aid);
  216. stm.executeUpdate();
  217. } catch (SQLException e) {
  218. // TODO Auto-generated catch block
  219. e.printStackTrace();
  220. }
  221. }
  222. }

 LoginServlet.java

  1. package com.hry.servlet;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.sql.Connection;
  5. import java.sql.SQLException;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.annotation.WebServlet;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import javax.servlet.http.HttpSession;
  12. import com.hry.bean.AdminBean;
  13. import com.hry.dao.AdminDao;
  14. /**
  15. * Servlet implementation class LoginServlet
  16. */
  17. @WebServlet("/LoginServlet")
  18. public class LoginServlet extends HttpServlet {
  19. private static final long serialVersionUID = 1L;
  20. AdminDao userdao = new AdminDao();
  21. AdminDao admindao = new AdminDao();
  22. /**
  23. * @see HttpServlet#HttpServlet()
  24. */
  25. public LoginServlet() {
  26. super();
  27. // TODO Auto-generated constructor stub
  28. }
  29. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  30. }
  31. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  32. //登录的判断
  33. //编码格式
  34. request.setCharacterEncoding("UTF-8");
  35. response.setContentType("text/html;charset=UTF-8");
  36. //获取账号和密码
  37. // PrintWriter out = response.getWriter();
  38. String username = request.getParameter("username");
  39. String password = request.getParameter("password");
  40. System.out.println(username);
  41. //对账号和密码进行判断
  42. boolean result = userdao.Login_verify(username, password);
  43. System.out.println(result);
  44. HttpSession session = request.getSession();
  45. //判断输入正确
  46. if(result){
  47. AdminBean adminbean = new AdminBean();
  48. //更加账号和密码查找出读者的信息
  49. adminbean = admindao.getAdminInfo(username,password);
  50. //将aid存入session中
  51. session.setAttribute("aid", ""+adminbean.getAid());
  52. //设置session的失效时间
  53. session.setMaxInactiveInterval(6000);
  54. //根据status的值来判断是管理员,还是读者,status=1为读者
  55. System.out.println(adminbean.getStatus());
  56. if(adminbean.getStatus()==1){
  57. response.sendRedirect("index2.jsp");
  58. }else{
  59. response.sendRedirect("admin.jsp");
  60. }
  61. }else{
  62. //没有找到对应的账号和密码,返回重新登录
  63. session.setAttribute("state", "密码错误");
  64. response.sendRedirect("login.jsp");
  65. }
  66. }
  67. }

 RegisterServlet.java

  1. package com.hry.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import com.hry.bean.AdminBean;
  9. import com.hry.dao.AdminDao;
  10. /**
  11. * Servlet implementation class RegisterServlet
  12. */
  13. @WebServlet("/RegisterServlet")
  14. public class RegisterServlet extends HttpServlet {
  15. private static final long serialVersionUID = 1L;
  16. public RegisterServlet() {
  17. super();
  18. // TODO Auto-generated constructor stub
  19. }
  20. /**
  21. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  22. */
  23. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24. }
  25. /**
  26. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  27. */
  28. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  29. // TODO Auto-generated method stub
  30. // doGet(request, response);
  31. request.setCharacterEncoding("UTF-8");
  32. response.setContentType("text/html;charset=UTF-8");
  33. //获取注册信息
  34. AdminDao userdao = new AdminDao();
  35. String username = request.getParameter("username");
  36. String password = request.getParameter("password");
  37. String name = request.getParameter("name");
  38. String email = request.getParameter("email");
  39. String phone = request.getParameter("phone");
  40. System.out.println(username);
  41. //默认设置最大借阅数和借阅天数
  42. int lend_num = 30;
  43. int max_num = 5;
  44. //将注册信息存入数据库,再返回登录
  45. userdao.Register(username,password,name,email,phone,lend_num,max_num);
  46. response.sendRedirect("login.jsp");
  47. }
  48. }

 BookBean.java

  1. package com.hry.bean;
  2. public class BookBean {
  3. /**
  4. * 图书的数据表的bean
  5. */
  6. private int bid;//图书id
  7. private String name;//图书名称
  8. private String card;//图书号
  9. private String autho;//作者
  10. private int num;//图书数量
  11. private String type;//图书的分类
  12. private String press;//出版社
  13. public int getBid() {
  14. return bid;
  15. }
  16. public void setBid(int bid) {
  17. this.bid = bid;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public String getCard() {
  26. return card;
  27. }
  28. public void setCard(String card) {
  29. this.card = card;
  30. }
  31. public String getAutho() {
  32. return autho;
  33. }
  34. public void setAutho(String autho) {
  35. this.autho = autho;
  36. }
  37. public int getNum() {
  38. return num;
  39. }
  40. public void setNum(int num) {
  41. this.num = num;
  42. }
  43. public String getType() {
  44. return type;
  45. }
  46. public void setType(String type) {
  47. this.type = type;
  48. }
  49. public String getPress() {
  50. return press;
  51. }
  52. public void setPress(String press) {
  53. this.press = press;
  54. }
  55. }

 AdminServlet.java 

  1. package com.hry.servlet;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.servlet.http.HttpSession;
  10. import com.hry.bean.AdminBean;
  11. import com.hry.dao.AdminDao;
  12. import com.hry.dao.BookDao;
  13. /**
  14. * Servlet implementation class AdminServlet
  15. */
  16. @WebServlet("/AdminServlet")
  17. public class AdminServlet extends HttpServlet {
  18. private static final long serialVersionUID = 1L;
  19. public AdminServlet() {
  20. super();
  21. // TODO Auto-generated constructor stub
  22. }
  23. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24. }
  25. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  26. //PrintWriter:输出流,为异常处理
  27. PrintWriter out = response.getWriter();
  28. //设置编码类型
  29. request.setCharacterEncoding("UTF-8");
  30. response.setContentType("text/html;charset=UTF-8");
  31. BookDao bookdao = new BookDao();
  32. //这里为了简单,设置了tip,用来区分是修改密码功能,还是修改个人资料的功能,tip=1为修改密码
  33. int tip = Integer.parseInt(request.getParameter("tip"));
  34. //获取发起请求页面的文件名称,这个在对应的jsp里面的表单填写,修改完成后就可以直接返回对应的页面
  35. String url = request.getParameter("url");
  36. System.out.println(url);
  37. HttpSession session = request.getSession();
  38. AdminBean adminbean = new AdminBean();
  39. //获取存到session的aid
  40. String aid = (String) session.getAttribute("aid");
  41. AdminDao admindao = new AdminDao();
  42. //通过aid获取到读者的信息
  43. adminbean = admindao.get_AidInfo2(aid);
  44. //修改密码
  45. if (tip==1) {
  46. //获取到输入的旧密码,新密码
  47. String password = request.getParameter("password");
  48. String password2 = request.getParameter("password2");
  49. //获取读者数据表中的密码
  50. String old_password = adminbean.getPassword();
  51. //对旧密码进行比较,如果相同就修改,不相同就直接退出
  52. if(old_password.equals(password)){
  53. admindao.updateUser(adminbean.getAid(), adminbean.getUsername(), password2, adminbean.getName(),
  54. adminbean.getEmail(), adminbean.getPhone(), adminbean.getLend_num(), adminbean.getMax_num());
  55. response.sendRedirect(url+".jsp");
  56. }else{
  57. out.write("<script type='text/javascript'>alert('password error');location.href='"+url+".jsp'; </script>");
  58. }
  59. } else {
  60. //修改个人资料
  61. //获取输入的信息
  62. String name = request.getParameter("name");
  63. String email = request.getParameter("email");
  64. String phone = request.getParameter("phone");
  65. //修改输入的信息到数据表中
  66. admindao.updateUser(adminbean.getAid(), adminbean.getUsername(), adminbean.getPassword(), name,
  67. email, phone, adminbean.getLend_num(), adminbean.getMax_num());
  68. response.sendRedirect(url+".jsp");
  69. }
  70. }
  71. }

四、源码

点击以下链接(内含数据库文件——books.sql):

链接:https://pan.baidu.com/s/1HN8CY281ctJWjSkynUgxag?pwd=5ugw 
提取码:5ugw 

五、备注 

该项目是基于一位GitHub上的大佬的项目模板写的,我在上课时按照老师的要求给原项目增加和优化了一些功能,最后变成了现在这个样子。由于当时在GitHub参考了很多的项目,导致我现在已经不记得那位大佬的用户名了,只能说是非常抱歉了。

如有侵权,请联系我删除。

六、祝福

路虽远行则可至,未来一定可期!祝您生活愉快、工作顺心、学业有成!

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

闽ICP备14008679号