package work;
import book.Book;
import util.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BookDao {
//新增图书
public boolean addBook(Book book) {
Connection conn = JDBCUtil.getConnection();
String sql = " insert into book1(name,author,price,type,statues) VALUES (?,?,?,?,?)";
PreparedStatement ps = null;
boolean result = false;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, book.getName());
ps.setString(2,book.getAuthor());
ps.setInt(3,book.getPrice());
ps.setString(4,book.getType());
ps.setInt(5,book.getStatus());
result = ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil.release(conn, ps);
}
return result;
}
//查找图书
public Book selectByName(String bookName){
Connection conn = JDBCUtil.getConnection();
//String sql = "SELECT name,author,price,type,statues FROM book1 WHERE name like %"+bookName+“%”; 模糊查询
String sql = "SELECT name,author,price,type,statues FROM book1 WHERE name = '"+bookName+"'";
PreparedStatement ps = null;
ResultSet rSet = null;
try {
ps = conn.prepareStatement(sql);
rSet = ps.executeQuery();
Book book = new Book();
while (rSet.next()){
String name = rSet.getString("name");
String author = rSet.getString("author");
int price = rSet.getInt("price");
String type = rSet.getString("type");
int statues = rSet.getInt("statues");
book.setName(name);
book.setAuthor(author);
book.setPrice(price);
book.setType(type);
if(statues == 0){
book.setStatues(false);
}else{
book.setStatues(true);
}
return book;
}
}catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil.release(conn, ps,rSet);
}
return null;
}
//显示所有图书
public List<Book> selectAll(){
Connection conn = JDBCUtil.getConnection();
//String sql = "SELECT name,author,price,type,statues FROM book1 WHERE name like %"+bookName+“%”; 模糊查询
String sql = "SELECT name,author,price,type,statues FROM book1";
PreparedStatement ps = null;
ResultSet rSet = null;
try {
ps = conn.prepareStatement(sql);
rSet = ps.executeQuery();
List<Book> bookList = new ArrayList<>();
while (rSet.next()){
Book book = new Book();
String name = rSet.getString("name");
String author = rSet.getString("author");
int price = rSet.getInt("price");
String type = rSet.getString("type");
int statues = rSet.getInt("statues");
book.setName(name);
book.setAuthor(author);
book.setPrice(price);
book.setType(type);
if(statues == 0){
book.setStatues(false);
}else{
book.setStatues(true);
}
bookList.add(book);
}
return bookList;
}catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil.release(conn, ps,rSet);
}
return null;
}
//借阅归还图书
public boolean updateStatusByName(String bookName,int status){
Connection conn = JDBCUtil.getConnection();
String sql = " update book1 set statues = "+status+" where name = '"+bookName+"'";
PreparedStatement ps = null;
boolean result = false;
try {
ps = conn.prepareStatement(sql);
result = ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil.release(conn, ps);
}
return result;
}
//删除图书
public boolean deleteByName(String bookName) {
Connection conn = JDBCUtil.getConnection();
String sql = " delete from book1 where name = '"+bookName+"'";
PreparedStatement ps = null;
boolean result = false;
try {
ps = conn.prepareStatement(sql);
result = ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil.release(conn, ps);
}
return result;
}
}