package com.tushugl.daos;

import java.sql.*;
import java.util.ArrayList;

import com.tushugl.DbUtil.Dbutil;
import com.tushugl.vos.Book;

public class Bookdao {
	// 添加图书
	public boolean addBook(Book book) {
		boolean result = false;
		Connection conn = Dbutil.getConn();
		try {
			PreparedStatement ps = conn.prepareStatement("insert into books(b"
					+ "name,price) values(?,?)");
			ps.setString(1, book.getName());
			ps.setFloat(2, book.getPrice());
			ps.execute();
			result = true;
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			Dbutil.close();
		}
		return result;
	}

	// 根据图书的编号查询图书
	public Book queryBookById(int id) {
		Connection conn = Dbutil.getConn();
		Book book = null;

		try {
			PreparedStatement ps = conn
					.prepareStatement("select id,bname,price from books where id = ?");
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				book = new Book(rs.getInt(1), rs.getString(2), rs.getFloat(3));
			}

		} catch (SQLException e) {

			e.printStackTrace();
		}
		return book;

	}

	// 根据图书编号删除图书
	public int deleteBook(int id) {

		Connection conn = Dbutil.getConn();
		int ret = 0;
		try {
			PreparedStatement ps = conn
					.prepareStatement("delete from books where id = ?");
			ps.setInt(1, id);
			ret = ps.executeUpdate();

		} catch (SQLException e) {

			e.printStackTrace();
		}
		return ret;
	}

	// 修改图书
	public int modifyBook(int id, String name, float price) {
		Connection conn = Dbutil.getConn();
		int ret = 0;
		try {
			PreparedStatement ps = conn
					.prepareStatement("update books set bname=? , price=? where id =?");
			ps.setString(1, name);
			ps.setFloat(2, price);
			ps.setInt(3, id);
			ret = ps.executeUpdate();
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return ret;

	}

	// 查询所有图书
	public ArrayList<Book> queryAllBook() {
		ArrayList<Book> list = new ArrayList<Book>();
		Connection conn = Dbutil.getConn();

		try {
			PreparedStatement ps = conn.prepareStatement("select * from books");
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				int id = rs.getInt(1);
				String name = rs.getString(2);
				float price = rs.getFloat(3);
				Book book = new Book(id, name, price);
				list.add(book);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;
	}

}
最近下载更多
xiaoyu111ewsd  LV4 1月7日
taoshen95  LV14 1月5日
我的心里面激动  LV1 1月4日
Kaiaahh  LV1 2023年12月30日
微信网友_5986558820093952  LV3 2023年12月26日
lang嘻嘻嘻  LV1 2023年12月25日
xxxueaaaa  LV1 2023年12月24日
微信网友_6779895352037376  LV1 2023年12月15日
微信网友_6430036841664512  LV2 2023年7月31日
微信网友_6551318769733632  LV1 2023年7月7日
最近浏览更多
pi-nang  LV2 3月11日
xiaoyu111ewsd  LV4 1月7日
我的心里面激动  LV1 1月4日
minjiang110  LV3 1月3日
hxy19991216  LV4 1月2日
wiehwhfe  LV1 1月1日
Kaiaahh  LV1 2023年12月30日
17693282606  LV11 2023年12月26日
asddwh  LV12 2023年12月25日
lang嘻嘻嘻  LV1 2023年12月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友