package dao;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.io.File;
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 {
private static InitialContext context=null;
private DataSource dataSource=null;
public BookDao(){
try {
if(context==null){
context=new InitialContext();
}
dataSource= (DataSource) context.lookup("java:comp/env/jdbc/mysql");
} catch (NamingException e) {
e.printStackTrace();
}
}
//添加图书时判断是否已存在此图书编号
public boolean bookfindone(String bookid){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection=dataSource.getConnection();
String sql="select * from book where bookid=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,bookid);
resultSet=preparedStatement.executeQuery();
if(resultSet.next()){
//System.out.println("333");
return true;
}
}catch (SQLException e){
e.printStackTrace();
}
//System.out.println("444");
return false;
}
//根据编号查找到该图书
public Book bookfindonebyid(String bookid){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
Book book=new Book();
try {
connection=dataSource.getConnection();
//System.out.println("105");
String sql="select * from book where bookid=?";
//System.out.println("106");
preparedStatement=connection.prepareStatement(sql);
//System.out.println("107");
preparedStatement.setString(1,bookid);
//System.out.println("108");
resultSet=preparedStatement.executeQuery();
//System.out.println("109");
if (resultSet.next()){
// System.out.println("110");
book.setBookid(resultSet.getString(1));
book.setBookname(resultSet.getString(2));
book.setBookprice(resultSet.getDouble(3));
//将路径中的文件名提取出来
String bookpath1=resultSet.getString(4);
File tempFile=new File(bookpath1.trim());
String bookpath=tempFile.getName();
book.setBookpath(bookpath);
}
}catch (SQLException e){
//System.out.println("111");
e.printStackTrace();
}
return book;
}
//添加图书信息
public boolean bookadd(Book book){
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection = dataSource.getConnection();
String sql="insert into book values(?,?,?,?)";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,book.getBookid());
preparedStatement.setString(2,book.getBookname());
preparedStatement.setDouble(3,book.getBookprice());
preparedStatement.setString(4,book.getBookpath());
//System.out.println("111");
preparedStatement.executeUpdate();
return true;
}catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public List<Book> bookshow(){
List<Book> list=new ArrayList<>();
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection = dataSource.getConnection();
String sql="select * from book";
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
Book book=new Book();
book.setBookid(resultSet.getString("bookid"));
//System.out.println(resultSet.getString("bookid"));
book.setBookname(resultSet.getString("bookname"));
book.setBookprice(resultSet.getDouble("bookprice"));
//将路径中的文件名提取出来
String bookpath1=resultSet.getString("bookpath");
File tempFile=new File(bookpath1.trim());
String bookpath=tempFile.getName();
book.setBookpath(bookpath);
list.add(book);
//System.out.println(list);
}
//System.out.println("222");
return list;
}catch (SQLException e){
e.printStackTrace();
}
//System.out.println("333");
return null;
}
//修改图片
public boolean bookchange1(Book book){
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection=dataSource.getConnection();
String sql="update book set bookname=?,bookprice=?,bookpath=? where bookid=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,book.getBookname());
preparedStatement.setDouble(2,book.getBookprice());
preparedStatement.setString(3,book.getBookpath());
preparedStatement.setString(4,book.getBookid());
preparedStatement.executeUpdate();
return true;
}catch (SQLException e){
e.printStackTrace();
}
return false;
}
public boolean bookchange2(Book book){
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection=dataSource.getConnection();
String sql="update book set bookname=?,bookprice=? where bookid=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,book.getBookname());
preparedStatement.setDouble(2,book.getBookprice());
preparedStatement.setString(3,book.getBookid());
preparedStatement.executeUpdate();
return true;
}catch (SQLException e){
e.printStackTrace();
}
return false;
}
public boolean bookdelete(String bookid){
Connection connection=null;
PreparedStatement preparedStatement=null;
try{
connection=dataSource.getConnection();
String sql="delete from book where bookid=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,bookid);
preparedStatement.executeUpdate();
return true;
}catch (SQLException e){
e.printStackTrace();
}
return false;
}
//分页
public List<Book> bookshow1(int currentPage,int pageSize){
List<Book> book=new ArrayList<>();
book=this.bookshow();
int listLength=0;
listLength=book.size();
int startIndex=(currentPage-1)*pageSize;//startIndex每一页开始的第一个编号
int endIndex=startIndex+pageSize;
if(endIndex>listLength){
endIndex=listLength;
}
book=book.subList(startIndex,endIndex);
return book;
}
}