Java Jsp+Servlet+mysql实现的图书借阅系统(系统管理员/图书管理员/读者 功能:图书信息管理、图书借阅、我的借阅记录、管理图书、用户管理)

java系统 专栏收录该内容
25 篇文章 1 订阅

JSP图书借阅系统

本系统是一套图书馆图书借阅管理系统,涉及后台管理员、图书管理员、用户,包括图书的管理、用户管理、信息维护等。

实现功能截图

登录:
在这里插入图片描述
读者主页面:
在这里插入图片描述
系统管理员主页面:
在这里插入图片描述
图书管理员主页面:
在这里插入图片描述
图书信息管理:
在这里插入图片描述
添加图书:
在这里插入图片描述
借阅记录查询:
在这里插入图片描述
用户管理:
在这里插入图片描述
图书管理:
在这里插入图片描述

技术点总结

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)
最后,别忘了一键三连哦

  • 8
    点赞
  • 1
    评论
  • 17
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

<p style="font-size:13px;"> <span style="font-size:16px;">JSP全称Java Server Pages,是一种动态网页开发技术。它使用JSP标签在HTML网页中插入Java代码。标签通常以<%开头以%>结束。</span> </p> <p style="font-size:13px;"> <span style="font-size:16px;">JSP是一种Java servlet,主要用于实现Java web应用程序的用户界面部分</span> </p> <p style="font-size:13px;"> <span style="font-size:16px;">JSP通过网页表单获取用户输入数据、访问数据库及其他数据源,然后动态地创建网页。</span> </p> <p style="font-size:13px;"> <span style="font-size:16px;"><br /> </span> </p> <p style="font-size:13px;"> <span style="font-size:16px;"><strong><span style="color:#E53333;">本课程对JSP做了详细讲解,主要涉及的课程内容:</span></strong>jsp的底层原理,jspservlet的区别,jsp页面如何插入java代码,jsp中的3个指令、7个动作,9个内置对象的使用,EL表达式,JSTL标签,如何自定义标签,MVC模式,jsp+servlet实战案例</span> </p> <p style="font-size:13px;"> <span style="font-size:16px;"><img src="https://img-bss.csdn.net/201909200401039572.jpg" alt="" /><br /> </span> </p> <p style="font-size:13px;"> <span style="font-size:16px;"><img src="https://img-bss.csdn.net/201909200401156598.jpg" alt="" /><br /> </span> </p> <p style="font-size:13px;"> <span style="font-size:16px;"><img src="https://img-bss.csdn.net/201909200401276144.jpg" alt="" /><br /> </span> </p> <p style="font-size:13px;"> <span style="font-size:16px;"><img src="https://img-bss.csdn.net/201909200401403823.png" alt="" /><br /> </span> </p> <p style="font-size:13px;"> <span style="font-size:16px;"><br /> </span> </p> <p style="font-size:13px;"> <span style="font-size:16px;"><img src="https://img-bss.csdn.net/201909200402149339.png" alt="" /><br /> </span> </p>
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

anmu4200

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值