package com.b305.bookStore.service;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.b305.bookStore.dao.BookStoreDAO;
import com.b305.bookStore.javaBean.Goods;
public class GoodsServiceImp implements GoodsService{
private PreparedStatement pstm;
private ResultSet rSet;
private List<Goods> list;
public static List<Goods> findData = new ArrayList<Goods>();
public static int page = 1;
/**显示所有商品信息*/
public List<Goods> findAll() {
list = new ArrayList<Goods>();
String sql = "select * from goods";
try {
pstm = BookStoreDAO.getConnection().prepareStatement(sql);
rSet = pstm.executeQuery();
while (rSet.next()) {
Goods goods = new Goods();
goods.setIsbn(rSet.getString(1));
goods.setBookName(rSet.getString(2));
goods.setAuthor(rSet.getString(3));
goods.setPrice(rSet.getDouble(4));
goods.setShowImage(rSet.getString(5));
goods.setPublish(rSet.getString(6));
goods.setBookDetails(rSet.getString(7));
goods.setInventory(rSet.getInt(8));
goods.setType(rSet.getString(9));
list.add(goods);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeAll();
}
return list;
}
/**查找指定商品*/
public List<Goods> findGoods(String bookName,String type) {
list = new ArrayList<Goods>();
try {
if(type.equals("所有类型")) {
String sql = "select * from goods where bookName = ?";
pstm = BookStoreDAO.getConnection().prepareStatement(sql);
pstm.setString(1, bookName);
} else {
String sql = "select * from goods where bookName = ? and type = ?";
pstm = BookStoreDAO.getConnection().prepareStatement(sql);
pstm.setString(1, bookName);
pstm.setString(2, type);
}
rSet = pstm.executeQuery();
while (rSet.next()) {
Goods goods = new Goods();
goods.setIsbn(rSet.getString(1));
goods.setBookName(rSet.getString(2));
goods.setAuthor(rSet.getString(3));
goods.setPrice(rSet.getDouble(4));
goods.setShowImage(rSet.getString(5));
goods.setPublish(rSet.getString(6));
goods.setBookDetails(rSet.getString(7));
goods.setInventory(rSet.getInt(8));
goods.setType(rSet.getString(9));
list.add(goods);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeAll();
}
return list;
}
/**修改指定商品*/
public List<Goods> modifyGoods(String isbn,String bookName,
String author,String publish,String details,
String type,String showImage,double price,int inventory) {
list = new ArrayList<Goods>();
String sql = "update goods set bookName = ?,author = ?, publish = ?,bookDetails = ?,"
+ "type = ?,showImage = ?,price = ?,inventory = ? where isbn = ?";
try {
pstm = BookStoreDAO.getConnection().prepareStatement(sql);
pstm.setString(1, bookName);
pstm.setString(2, author);
pstm.setString(3, publish);
pstm.setString(4, details);
pstm.setString(5, type);
pstm.setString(6, showImage);
pstm.setDouble(7, price);
pstm.setInt(8, inventory);
pstm.setString(9, isbn);
pstm.executeUpdate();
list = findAll();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeAll();
}
return list;
}
/**增加商品*/
public List<Goods> addGoods(Goods goods) {
list = new ArrayList<Goods>();
String sql = "insert into goods values(?,?,?,?,?,?,?,?,?)";
try {
pstm = BookStoreDAO.getConnection().prepareStatement(sql);
pstm.setString(1, goods.getIsbn());
pstm.setString(2, goods.getBookName());
pstm.setString(3, goods.getAuthor());
pstm.setDouble(4, goods.getPrice());
pstm.setString(5, goods.getShowImage());
pstm.setString(6, goods.getPublish());
pstm.setString(7, goods.getBookDetails());
pstm.setInt(8, goods.getInventory());
pstm.setString(9, goods.getType());
pstm.executeUpdate();
list = findAll();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeAll();
}
return list;
}
/**删除用户*/
public List<Goods> delGoods(String isbn) {
list = new ArrayList<Goods>();
String sql = "delete from goods where isbn = ?";
try {
pstm = BookStoreDAO.getConnection().prepareStatement(sql);
pstm.setString(1, isbn);
pstm.executeUpdate();
list = findAll();
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll();
}
return list;
}
/**查找指定商品*/
public List<Goods> searchBook(String bookName) {
list = new ArrayList<Goods>();
String sql = "select * from goods where bookName = ?";
try {
pstm = BookStoreDAO.getConnection().prepareStatement(sql);
pstm.setString(1, bookName);
rSet = pstm.executeQuery();
while (rSet.next()) {
Goods goods = new Goods();
goods.setIsbn(rSet.getString(1));
goods.setBookName(rSet.getString(2));
goods.setAuthor(rSet.getString(3));
goods.setPrice(rSet.getDouble(4));
goods.setShowImage(rSet.getString(5));
goods.setPublish(rSet.getString(6));
goods.setBookDetails(rSet.getString(7));
goods.setInventory(rSet.getInt(8));
goods.setType(rSet.getString(9));
list.add(goods);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeAll();
}
return list;
}
/**书本信息*/
public Goods bookMes(String isbn) {
Goods goods = new Goods();
String sql = "select * from goods where isbn = ?";
try {
pstm = BookStoreDAO.getConnection().prepareStatement(sql);
pstm.setString(1, isbn);
rSet = pstm.executeQuery();
while (rSet.next()) {
goods.setIsbn(rSet.getString(1));
goods.setBookName(rSet.getString(2));
goods.setAuthor(rSet.getString(3));
goods.setPrice(rSet.getDouble(4));
goods.setShowImage(rSet.getString(5));
goods.setPublish(rSet.getString(6));
goods.setBookDetails(rSet.getString(7));
goods.setInventory(rSet.getInt(8));
goods.setType(rSet.getString(9));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeAll();
}
return goo