package com.book.dao;

import java.util.List;

import com.book.model.Book;
import com.book.util.StringUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class BookDao {
	
	// 图书添加
	public int add(Connection con,Book book)throws Exception{
		String sql="insert into book (book_name,type_id,author,publish,price,number,status,remark) values(?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql);
		pstmt.setString(1, book.getBookName());
		pstmt.setInt(2, book.getBookTypeId());
		pstmt.setString(3, book.getAuthor());
		pstmt.setString(4, book.getPublish());
		pstmt.setDouble(5, book.getPrice());
		pstmt.setInt(6, book.getNumber());
		pstmt.setInt(7, book.getStatus());
		pstmt.setString(8, book.getRemark());
		return pstmt.executeUpdate();
	}
	
	// 图书信息查询(管理员)
	public ResultSet list(Connection con,Book book)throws Exception{
		StringBuffer sb=new StringBuffer("select b.*,bt.type_name from book b,book_type bt where b.type_id=bt.id");
		if(!StringUtil.isEmpty(book.getBookName())){
			sb.append(" and b.book_name like '%"+book.getBookName()+"%'");
		}
//		if(!StringUtil.isEmpty(book.getAuthor())){
//			sb.append(" and b.author like '%"+book.getAuthor()+"%'");
//		}
		if(book.getBookTypeId()!=null && book.getBookTypeId()!=0){
			sb.append(" and b.type_id="+book.getBookTypeId());
		}
		if(book.getStatus()!=null){
			sb.append(" and b.status="+book.getStatus());
		}
		if(book.getBookId() != null){
			sb.append(" and b.id="+book.getBookId());
		}
		sb.append(" ORDER BY b.status");
		PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sb.toString());
		return pstmt.executeQuery();
	}
	
	// 图书信息查询(学生)
	public ResultSet listCan(Connection con,Book book)throws Exception{
		StringBuffer sb=new StringBuffer("select b.*,bt.type_name from book b,book_type bt where type_id=bt.id and b.status = 1");
		if(!StringUtil.isEmpty(book.getBookName())){
			sb.append(" and b.book_name like '%"+book.getBookName()+"%'");
		}
		if(book.getBookId() != null){
			sb.append(" and b.id="+book.getBookId());
		}
		PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sb.toString());
		return pstmt.executeQuery();
	}
	
	//图书信息删除
	public int delete(Connection con,String id)throws Exception{
		String sql="delete from book where id=?";
		PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql);
		pstmt.setString(1, id);
		return pstmt.executeUpdate();
	}
	
	//图书信息修改
	public int update(Connection con,Book book)throws Exception{
		String sql="update book set book_name=?,type_id=?,author=?,publish=?,price=?,number=?,status=?,remark=? where id=?";
		PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql);
		pstmt.setString(1, book.getBookName());
		pstmt.setInt(2, book.getBookTypeId());
		pstmt.setString(3, book.getAuthor());
		pstmt.setString(4, book.getPublish());
		pstmt.setDouble(5, book.getPrice());
		pstmt.setInt(6, book.getNumber());
		pstmt.setInt(7, book.getStatus());
		pstmt.setString(8, book.getRemark());
		pstmt.setInt(9, book.getBookId());
		return pstmt.executeUpdate();
	}
	
}
最近下载更多
lsglsg9  LV22 2023年12月20日
sunshine9920  LV12 2023年10月23日
hongdongdong  LV12 2023年8月9日
12345681  LV2 2023年6月10日
gxluchuan  LV1 2023年4月7日
lolokei  LV1 2022年10月10日
喃喵xxxx  LV6 2022年9月6日
zxcvbnm1580  LV1 2022年6月15日
testuser1234567  LV24 2022年5月31日
sks666  LV5 2022年5月25日
最近浏览更多
小小白银110  LV1 1月2日
偷喝汽水 2023年12月26日
暂无贡献等级
lshlsh 2023年12月25日
暂无贡献等级
huangzy  LV12 2023年12月21日
夜猫子111  LV2 2023年11月6日
卢本伟不开挂  LV4 2023年10月22日
sunshine9920  LV12 2023年10月22日
ezra77934  LV2 2023年9月16日
hongdongdong  LV12 2023年8月9日
yuexiaolong  LV1 2023年7月4日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友