图书管理系统(增删改查带分页,上传)

首先要创建web项目,取名Book,创建分层,dao(impl),entuty,service,servlet,因为要用到分页,所以还有一个util;

创建分层之后,先写实体类,实体类包括下面这两张表的数据:
实体类对字符进行封装;
然后写basedao,就是连接数据库的工具类;
下一步就是创建bookdao接口的实现类,bookdaoimpl。bookdao接口中写要实现功能的方法,
在接口中 写入实现方法的sql语句:第一步,在页面查询出数据库中现有的数据:

public class BookDaoImpl extends BaseDao implements BookDao {


	@Test
	public void selectTest() throws Exception {
		
	}


	// 查询图书列表(分页)
	@Override
	public List<Book> selectbook(int pageIndex, int pageSize) throws Exception {
		// 创建list集合存放book对象
		List<Book> list = new ArrayList<Book>();
		String sql = "select * from book limit ?,?";
		Object[] obj = { pageIndex, pageSize };
		ResultSet rs = executeSelect(sql, obj);
		if (rs != null) {
			while (rs.next()) {
				// 创建book对象
				Book book = new Book();
				book.setBookid(rs.getInt("bookid"));
				book.setBookname(rs.getString("bookname"));
				book.setBookpicture(rs.getString("bookpicture"));
				book.setBookprice(rs.getDouble("bookprice"));
				book.setBookabout(rs.getString("bookabout"));
				book.setBookauthor(rs.getString("bookauthor"));
				book.setBookcategory(rs.getInt("bookcategory"));
				book.setBookdatatime(rs.getDate("bookdatetime"));
				list.add(book);
			}
		}
		return list;
	}


	// 查询book表中的记录数
	@Override
	public int getCount() throws Exception {
		int result = 0;
		String sql = "select count(*) as num from book";
		ResultSet rs = executeSelect(sql);
		if (rs != null) {
			if (rs.next()) {
				result = rs.getInt("num");
			}
			closeAll();
		}
		return result;
	}
// 按名称模糊查询(分页)
	@Override
	public List<Book> likebook(int category, String name,int pageIndex, int pageSize)
			throws Exception {
		// 创建list集合存放book对象
		List<Book> list = new ArrayList<Book>();
		StringBuffer sb=new StringBuffer("select * from book where 1=1");
		if(category!=0)
		{
			sb=sb.append(" and bookcategory='"+category+"' ");
		}
		if(name!="")
		{
			sb=sb.append(" and bookname like '%"+name+"%'");
		}
		sb=sb.append(" limit ?,?");
		Object[] obj = { pageIndex, pageSize };
		
		ResultSet rs = executeSelect(sb.toString(), obj);
		if (rs != null) {
			while (rs.next()) {
				// 创建book对象
				Book book = new Book();
				book.setBookid(rs.getInt("bookid"));
				book.setBookname(rs.getString("bookname"));
				book.setBookpicture(rs.getString("bookpicture"));
				book.setBookprice(rs.getDouble("bookprice"));
				book.setBookabout(rs.getString("bookabout"));
				book.setBookauthor(rs.getString("bookauthor"));
				book.setBookcategory(rs.getInt("bookcategory"));
				book.setBookdatatime(rs.getDate("bookdatetime"));
				list.add(book);
			}
		}
		return list;
	}


	@Override
	public int getselectCount(int category,String name) throws Exception {
		int result = 0;
		StringBuffer sb=new StringBuffer("select count(*) as num from book where 1=1 ");
		if(category!=0)
		{
			sb=sb.append(" and bookcategory='"+category+"' ");
		}
		if(name!="")
		{
			sb=sb.append(" and bookname like '%"+name+"%'");
		}
		ResultSet rs = executeSelect(sb.toString());
		if (rs != null) {
			if (rs.next()) {
				result = rs.getInt("num");
			}
			closeAll();
		}
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值