JSP图书借阅系统

随着社会经济的迅速发展和科学技术的全面进步以及计算机事业的飞速发展,以计算机科学与通信技术为基础的信息管理系统IE处于蓬勃发展的时期。随着经济文化水平的显著提高,人们对生活质量及工作环境的要求也越来越高,但伴随着人的劳动强度的增大,以及社交活动的广泛开展,如何来提高人民纸质书本阅读量,是一个很现实的问题。无疑,科技的蓬勃发展使更多人依赖电子书,逐渐失去了对阅读纸质书本重要性的理解。如今书籍的发展,也继承了信息化的发展道路,网络的兴起,给了人们各种各样不同的选择。与此同时,为了管理好一个书店的正常营运,管理问题也就提上了日程。随着图书借阅问题的白热化,管理难度也越来越大,如何优化书店的日常管理也就成为了一个大众化的课题。

主要功能点

读者:
登录注册,借阅还书,查询书籍,查看当前借阅信息以及历史借阅信息,查看、查询读者借阅榜以及借阅书籍借阅榜,问题反馈以及查询反馈结果等功能。
管理员:
对图书、图书分类、读者信息的增删改查,查看全部读者当前借阅信息以及借阅历史,查看、查询书籍借阅榜及读者借阅榜,查看、查询用户反馈信息并修改反馈状态(未解决或已解决)。
超级管理员:
除管理员权限外,新增管理员权限。对管理员信息进行增删改功能。为书店员工定制个人账号,方便管理。

实现功能截图

注册登录:
在这里插入图片描述
在这里插入图片描述

读者主页面:
在这里插入图片描述
修改个人资料:
在这里插入图片描述

图书管理员主页面:
在这里插入图片描述

超级管理:
在这里插入图片描述
图书查询:
在这里插入图片描述

借阅图书:
在这里插入图片描述

借阅信息:
在这里插入图片描述

还书:
在这里插入图片描述

借阅历史:
在这里插入图片描述
热门推荐:
在这里插入图片描述
最佳读者:
在这里插入图片描述
问题反馈:
在这里插入图片描述
添加修改删除图书:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
读者管理:
在这里插入图片描述

技术点总结

jsp、Servlet
jdk版本:1.7
tomcat: 7.0
数据库:mysql
开发工具:eclipse

项目包结构分类清晰:
在这里插入图片描述

(叉号有些是误报。。)

代码

实体类Entity:
BookBean.java:

package com.code2life.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;// 出版社
	private int times;//借阅次数

	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;
	}

	public int getTimes() {
		return times;
	}

	public void setTimes(int times) {
		this.times = times;
	}
}

TUserBean.java

package com.code2life.bean;

public class TUserBean {
	/**
	 * 用户的数据表的bean
	 */
	private int id;// id
	private int status;// 用来判断是管理员还是读者,读者的值为1,管理员为2
	private String username;// 账号
	private String phone;// 手机号
	private String password;// 密码

	public int getId() {
		return id;
	}

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

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}


	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
}

数据库连接:
DBUtil.java:

package com.code2life.util;

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

public class DBUtil {
	/**
	 * 连接数据库的操作,用户名,密码,使用jdbc连接
	 */
	public static String username = "root";
	public static String password = "123456";
	public static String url = "jdbc:mysql://localhost:3306/books?useSSL=false&serverTimezone=UTC";

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnectDb() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(url, username, password);

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

	public static void CloseDB(ResultSet rs, PreparedStatement stm, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (stm != null) {
			try {
				stm.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


dao层
BookDao.java

package com.code2life.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;

import com.code2life.bean.AdminBean;
import com.code2life.bean.BookBean;
import com.code2life.bean.HistoryBean;
import com.code2life.util.DBUtil;

/**
 * 关于图书连接数据库的所有操作的类
 */
public class BookDao {

	/**
	 * 添加图书信息,传入所有的信息
	 * 
	 * @param card
	 * @param name
	 * @param type
	 * @param autho
	 * @param press
	 * @param num
	 * @param times
	 */
	public void addBook(String card, String name, String type, String autho, String press, int num) {
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnectDb();
		String sql = "insert into book(card,name,type,autho,press,num,times) values(?,?,?,?,?,?,?)";
		int rs = 0;
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, card);
			stm.setString(2, name);
			stm.setString(3, type);
			stm.setString(4, autho);
			stm.setString(5, press);
			stm.setInt(6, num);
			stm.setInt(7, 0);
			rs = stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 获取所有的图书信息,返回的是ArrayList数组形式
	 * 
	 * @return
	 */
	public ArrayList<BookBean> get_ListInfo() {
		ArrayList<BookBean> tag_Array = new ArrayList<BookBean>();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from book";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				BookBean tag = new BookBean();
				tag.setBid(rs.getInt("bid"));
				tag.setName(rs.getString("name"));
				tag.setCard(rs.getString("card"));
				tag.setType(rs.getString("type"));
				tag.setAutho(rs.getString("autho"));
				tag.setPress(rs.getString("press"));
				tag.setNum(rs.getInt("num"));
				tag.setTimes(rs.getInt("times"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}
	
	/**
	 * 获取所有的图书信息并排序,返回的是ArrayList数组形式
	 * 
	 * @return
	 */
	public ArrayList<BookBean> get_ListInfo2() {
		ArrayList<BookBean> tag_Array = new ArrayList<BookBean>();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from book order by times desc";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				BookBean tag = new BookBean();
				tag.setBid(rs.getInt("bid"));
				tag.setName(rs.getString("name"));
				tag.setCard(rs.getString("card"));
				tag.setType(rs.getString("type"));
				tag.setAutho(rs.getString("autho"));
				tag.setPress(rs.getString("press"));
				tag.setNum(rs.getInt("num"));
				tag.setTimes(rs.getInt("times"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	/**
	 * 获取借阅记录的全部信息,传入的条件有status,aid,表示搜索正在借阅的,或者已经还书的信息,aid代表当前登录用户
	 * 
	 * @param status
	 * @return
	 */
	public ArrayList<HistoryBean> get_HistoryListInfo(int status, String aid) {
		ArrayList<HistoryBean> tag_Array = new ArrayList<HistoryBean>();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from history where aid=" + aid + " and status=" + status;
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				HistoryBean tag = new HistoryBean();
				tag.setHid(rs.getInt("hid"));
				tag.setAid(rs.getInt("aid"));
				tag.setBid(rs.getInt("bid"));
				tag.setBookname(rs.getString("bookname"));
				tag.setCard(rs.getString("card"));
				tag.setAdminname(rs.getString("adminname"));
				tag.setUsername(rs.getString("username"));
				tag.setBegintime(rs.getString("begintime"));
				tag.setEndtime(rs.getString("endtime"));
				tag.setStatus(rs.getInt("status"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	/**
	 * 获取借阅记录的全部信息,传入的条件有status,表示搜索正在借阅的,或者已经还书的信息
	 * 
	 * @param status
	 * @return
	 */
	public ArrayList<HistoryBean> get_HistoryListInfo2(int status) {
		ArrayList<HistoryBean> tag_Array = new ArrayList<HistoryBean>();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from history where status=" + status;
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				HistoryBean tag = new HistoryBean();
				tag.setHid(rs.getInt("hid"));
				tag.setAid(rs.getInt("aid"));
				tag.setBid(rs.getInt("bid"));
				tag.setBookname(rs.getString("bookname"));
				tag.setCard(rs.getString("card"));
				tag.setAdminname(rs.getString("adminname"));
				tag.setUsername(rs.getString("username"));
				tag.setBegintime(rs.getString("begintime"));
				tag.setEndtime(rs.getString("endtime"));
				tag.setStatus(rs.getInt("status"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	/**
	 * 获取单个图书的信息,根据传入的bid来查找,返回一个BookBean数据类型
	 * 
	 * @param bid
	 * @return
	 */
	public BookBean get_BookInfo(int bid) {
		BookBean tag = new BookBean();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from book where bid=" + bid;
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				tag.setBid(rs.getInt("bid"));
				tag.setName(rs.getString("name"));
				tag.setCard(rs.getString("card"));
				tag.setType(rs.getString("type"));
				tag.setAutho(rs.getString("autho"));
				tag.setPress(rs.getString("press"));
				tag.setTimes(rs.getInt("times"));
				tag.setNum(rs.getInt("num"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tag;
	}

	/**
	 * 修改图书的信息,bid作为条件
	 */
	public void updateBook(int bid, String card, String name, String type, String autho, String press, int num) {
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnectDb();
		String sql = "update book set name=?,card=?,type=?,autho=?,press=?,num=? where bid=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, name);
			stm.setString(2, card);
			stm.setString(3, type);
			stm.setString(4, autho);
			stm.setString(5, press);
			stm.setInt(6, num);
			stm.setInt(7, bid);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 删除图书信息,根据传入的bid作为条件
	 * 
	 * @param bid
	 */
	public void deleteBook(int bid) {
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnectDb();
		String sql = "delete from book where bid=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setInt(1, bid);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 用户查找图书,根据输入的名称,使用like进行模糊查询,然后返回一个ArrayList数组类型
	 * 
	 * @param name
	 * @return
	 */
	public ArrayList<BookBean> getLikeList(String name) {
		// TODO Auto-generated method stub
		ArrayList<BookBean> tag_Array = new ArrayList<BookBean>();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from book where name like '%" + name + "%' or autho like '%" + name
				+ "%' or type like '%" + name + "%'";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				BookBean tag = new BookBean();
				tag.setBid(rs.getInt("bid"));
				tag.setName(rs.getString("name"));
				tag.setCard(rs.getString("card"));
				tag.setType(rs.getString("type"));
				tag.setAutho(rs.getString("autho"));
				tag.setPress(rs.getString("press"));
				tag.setTimes(rs.getInt("times"));
				tag.setNum(rs.getInt("num"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	/**
	 * 图书借阅函数,根据传入bid图书id,adminbean当前登录用户的信息,在借阅记录数据表中新插入一条记录
	 * 
	 * @param bid
	 * @param adminbean
	 */
	public void borrowBook(int bid, AdminBean adminbean) {
		// TODO Auto-generated method stub
		BookBean bookbean = new BookBean();
		bookbean = this.get_BookInfo(bid);
		// 生成日期的功能
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DATE);
		// 生成借阅开始日期
		String begintime = "" + year + "-" + month + "-" + day;
		month = month + 1;
		// 生成截止还书日期
		String endtime = "" + year + "-" + month + "-" + day;
		Connection conn = DBUtil.getConnectDb();
		String sql = "insert into history(aid,bid,card,bookname,adminname,username,begintime,endtime,status) values(?,?,?,?,?,?,?,?,?)";
		int rs = 0;
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setInt(1, adminbean.getAid());
			stm.setInt(2, bookbean.getBid());
			stm.setString(3, bookbean.getCard());
			stm.setString(4, bookbean.getName());
			stm.setString(5, adminbean.getUsername());
			stm.setString(6, adminbean.getName());
			stm.setString(7, begintime);
			stm.setString(8, endtime);
			stm.setInt(9, 1);
			rs = stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql2 = "update book set times=? where bid=?";
		PreparedStatement stm2 = null;
		try {
			stm2 = conn.prepareStatement(sql2);
			stm2.setInt(1, bookbean.getTimes() + 1);
			stm2.setInt(2, bookbean.getBid());
			stm2.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql3 = "update admin set times=? where aid=?";
		PreparedStatement stm3 = null;
		try {
			stm3 = conn.prepareStatement(sql3);
			stm3.setInt(1, adminbean.getTimes() + 1);
			stm3.setInt(2, adminbean.getAid());
			stm3.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	/**
	 * 还书功能的函数,根据传入的hid借阅记录id,将status字段的值改为0,并将还书日期改变为当前日期
	 * 
	 * @param hid
	 */
	public void borrowBook2(int hid) {
		// TODO Auto-generated method stub
		// 生成日期
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DATE);
		// 生成还书日期
		String endtime = "" + year + "-" + month + "-" + day;
		Connection conn = DBUtil.getConnectDb();
		String sql = "update history set endtime=?,status=? where hid=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, endtime);
			stm.setInt(2, 0);
			stm.setInt(3, hid);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 图书延期,根据传入hid记录id,在借阅记录数据表中更新endtime记录
	 * 
	 * @param hid
	 * @param endtime
	 */
	public void AddTime(int hid, String endtime) {
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnectDb();
		String sql = "update history set endtime=? where hid=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, endtime);
			stm.setInt(2, hid);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

TUserDao.java

package com.code2life.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.code2life.bean.AdminBean;
import com.code2life.bean.BookBean;
import com.code2life.bean.TUserBean;
import com.code2life.util.DBUtil;

/**
 * 有关读者账号的连接数据库操作,登录验证,注册,修改账号,修改密码
 */
public class TUserDao {

	/**
	 * 登录验证功能,传入用户名和密码,在数据库中查找,如果找到了,返回true,没找到则返回false
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean Login_verify(String username, String password) {
		Connection conn = DBUtil.getConnectDb();
		PreparedStatement stm = null;
		ResultSet rs = null;
		String sql = "select * from t_user where username='" + username + " 'and password='" + password + "'";
		try {
			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;
	}

	/**
	 * 注册账号的函数,传入账号,密码,手机号
	 * 
	 * @param username
	 * @param password
	 * @param phone
	 */
	public void Register(String username, String password, String phone) {
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnectDb();
		String sql = "insert into t_user(username,phone,password,status) values(?,?,?,?)";
		int rs = 0;
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, username);
			stm.setString(2, phone);
			stm.setString(3, password);
			stm.setInt(4, 1);
			rs = stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 新增管理员账号,传入密码,姓名,手机号
	 * 
	 * @param username
	 * @param password
	 * @param phone
	 */
	public void Register2(String username, String password, String phone) {
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnectDb();
		String sql = "insert into t_user(username,phone,password,status) values(?,?,?,?)";
		int rs = 0;
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, username);
			stm.setString(2, phone);
			stm.setString(3, password);
			stm.setInt(4, 2);
			rs = stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 根据传入的账号,密码,来查找对应的读者信息,返回一个TUserBean类型,
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public TUserBean getTUserInfo(String username, String password) {
		// TODO Auto-generated method stub
		TUserBean tUserbean = new TUserBean();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from t_user where username= '"+username+"' and password= '"+password+"'";
		
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			if (rs.next()) {
				tUserbean.setId(rs.getInt("id"));
				tUserbean.setUsername(rs.getString("username"));
				tUserbean.setPhone(rs.getString("phone"));
				tUserbean.setPassword(rs.getString("password"));
				tUserbean.setStatus(rs.getInt("status"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tUserbean;
	}

	/**
	 * 获取全部用户的信息,其中sql语句中的status=1,表示只查找读者,不显示管理员的
	 * 
	 * @return
	 */
	public ArrayList<TUserBean> get_ListInfo() {
		ArrayList<TUserBean> tag_Array = new ArrayList<TUserBean>();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from t_user where status=1";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				TUserBean tUserBean = new TUserBean();
				tUserBean.setId(rs.getInt("id"));
				tUserBean.setUsername(rs.getString("username"));
				tUserBean.setPassword(rs.getString("password"));
				tUserBean.setPhone(rs.getString("phone"));
				tUserBean.setStatus(rs.getInt("status"));
				tag_Array.add(tUserBean);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	/**
	 * 获取全部用户的信息,其中sql语句中的status=2,表示只查找管理员,不显示读者的
	 * 
	 * @return
	 */
	public ArrayList<TUserBean> get_ListInfo2() {
		ArrayList<TUserBean> tag_Array = new ArrayList<TUserBean>();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from t_user where status=2";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				TUserBean tUserBean = new TUserBean();
				tUserBean.setId(rs.getInt("id"));
				tUserBean.setUsername(rs.getString("username"));
				tUserBean.setPassword(rs.getString("password"));
				tUserBean.setPhone(rs.getString("phone"));
				tUserBean.setStatus(rs.getInt("status"));
				tag_Array.add(tUserBean);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}
	/**
	 * 获取全部用户的信息并排序,其中sql语句中的status=1,表示只查找读者,不显示管理员的
	 * 
	 * @return
	 */
	public ArrayList<TUserBean> get_ListInfo3() {
		ArrayList<TUserBean> tag_Array = new ArrayList<TUserBean>();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from t_user where status=1 order by times desc";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				TUserBean tUserBean = new TUserBean();
				tUserBean.setId(rs.getInt("aid"));
				tUserBean.setUsername(rs.getString("username"));
				tUserBean.setPassword(rs.getString("password"));
				tUserBean.setPhone(rs.getString("phone"));
				tUserBean.setStatus(rs.getInt("status"));
				tag_Array.add(tUserBean);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	/**
	 * 根据传入的id,查找到对应的读者的全部信息,返回一个TUserBean类型的数据
	 * 
	 * @param id
	 * @return
	 */
	public TUserBean get_AidInfo(int aid) {
		TUserBean tUserBean = new TUserBean();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from t_user where aid=" + aid;
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			if (rs.next()) {
				tUserBean.setId(rs.getInt("aid"));
				tUserBean.setUsername(rs.getString("username"));
				tUserBean.setPassword(rs.getString("password"));
				tUserBean.setPhone(rs.getString("phone"));
				tUserBean.setStatus(rs.getInt("status"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tUserBean;
	}

	/**
	 * 根据传入的id,查找到对应的读者的全部信息,返回一个TUserBean类型的数据,与上一个相似,只是id的类型为String
	 * 
	 * @param id
	 * @return
	 */
	public TUserBean get_AidInfo2(String aid) {
		TUserBean tUserBean = new TUserBean();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from t_user where aid=" + aid;
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			if (rs.next()) {
				tUserBean.setId(rs.getInt("aid"));
				tUserBean.setUsername(rs.getString("username"));
				tUserBean.setPassword(rs.getString("password"));
				tUserBean.setPhone(rs.getString("phone"));
				tUserBean.setStatus(rs.getInt("status"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tUserBean;
	}

	/**
	 * 修改读者的信息
	 */
	public void updateUser(int id, String username, String password, String phone) {
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnectDb();
		String sql = "update t_user set username=?,phone=?,password=? where id=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, username);
			stm.setString(2, phone);
			stm.setString(3, password);
			stm.setInt(4, id);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 修改管理员的信息
	 */
	public void updateAdmin(int id, String username, String password,String phone) {
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnectDb();
		String sql = "update t_user set username=?,phone=?,password=? where id=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, username);
			stm.setString(2, phone);
			stm.setString(3, password);
			stm.setInt(4, id);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 删除用户的信息,根据传入的id作为条件
	 * 
	 * @param id
	 */
	public void deleteUser(int id) {
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnectDb();
		String sql = "delete from t_user where id=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setInt(1, id);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 删除管理员的信息,根据传入的id作为条件
	 * 
	 * @param aid
	 */
	public void deleteAdmin(int id) {
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnectDb();
		String sql = "delete from t_user where id=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setInt(1, id);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 查找用户,根据输入的名称,使用like进行模糊查询,然后返回一个ArrayList数组类型
	 * 
	 * @param name
	 * @return
	 */
	public ArrayList<TUserBean> getLikeList(String name) {
		// TODO Auto-generated method stub
		ArrayList<TUserBean> tag_Array = new ArrayList<TUserBean>();
		Connection conn = DBUtil.getConnectDb();
		String sql = "select * from t_user where username like '%" + name + "%' or id like '%" + name + "%'";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				TUserBean tag = new TUserBean();
				tag.setId(rs.getInt("id"));
				tag.setStatus(rs.getInt("status"));
				tag.setUsername(rs.getString("username"));
				tag.setPassword(rs.getString("password"));
				tag.setPhone(rs.getString("phone"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}
}

servlet层:
AddBookServlet.java:

package com.code2life.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.code2life.dao.BookDao;

/**
 * Servlet implementation class AddBookServlet
 */
@WebServlet("/AddBookServlet")
public class AddBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddBookServlet() {
		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
//		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @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");
		// 获取要添加图书的信息
		String card = request.getParameter("card");
		String name = request.getParameter("name");
		String type = request.getParameter("type");
		String autho = request.getParameter("autho");
		String press = request.getParameter("press");
		int num = Integer.parseInt(request.getParameter("num"));
		BookDao bookdao = new BookDao();
		// 调用函数,存入图书
		bookdao.addBook(card, name, type, autho, press, num);
		response.sendRedirect("/books/admin_book.jsp");
	}

}

AddUserServlet.java

package com.code2life.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.code2life.dao.AdminDao;

/**
 * Servlet implementation class AddUserServlet
 */
@WebServlet("/AddUserServlet")
public class AddUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddUserServlet() {
		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
//		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @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");
		// 获取要添加的读者的信息
		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");
		int lend_num = Integer.parseInt(request.getParameter("lend_num"));
		int max_num = Integer.parseInt(request.getParameter("max_num"));

		AdminDao userdao = new AdminDao();
		// 调用函数添加读者信息
		userdao.Register(username, password, name, email, phone, lend_num, max_num);
		response.sendRedirect("/books/admin_user.jsp");
	}

}

写在最后

码代码不容易,需要的同学可以参考学习,全部代码不能都贴出,如果需要可以私博主V交流(Code2Life2)
最后,别忘了一键三连哦

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐