package dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import javabean.PagerBean;
import pojo.*;
import util.ConnManager;
public class GoodsDao {
//分页查询图书信息
public void readGoodsByProc(PagerBean bean){
ArrayList<Goods> allGoods = new ArrayList<Goods>();
Goods goods = null;
Connection conn = null;
CallableStatement cstmt = null;
ResultSet rs = null;
try {
conn = ConnManager.getConn();
cstmt = conn.prepareCall("{ call proc_table_paging(?,?,?,?,?,?,?) }");
cstmt.setString(1, "vw_Goods_Actor_Publication_GoodsType");
cstmt.setString(2, "*");
cstmt.setString(3, bean.getOrderByFil());
cstmt.setInt(4, bean.getPageSize());
cstmt.setInt(5, bean.getPageIndex());
cstmt.setInt(6,bean.getOrderByType());
cstmt.setString(7,bean.getWhereByProc());
rs = cstmt.executeQuery();
if(rs.next()){
int count = rs.getInt(1);
bean.setTotalCount(count);
}
if(cstmt.getMoreResults()){
rs = cstmt.getResultSet();
while(rs.next()){
goods = new Goods();
goods.setIsbn(rs.getString(1));
goods.setBookName(rs.getString(2));
goods.setMarketPrice(rs.getDouble(3));
goods.setMemberPrice(rs.getDouble(4));
goods.setNumber(rs.getInt(5));
goods.setImage(rs.getString(6));
goods.setBookIntroduce(rs.getString(7));
goods.setActorName(rs.getString(9));
goods.setPublicationName(rs.getString(11));
goods.setAddDate(rs.getDate(12));
goods.setGoodsTypeName(rs.getString(14));
goods.setPublicationDate(rs.getDate(15));
allGoods.add(goods);
}
bean.setAllGoods(allGoods);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
ConnManager.closeAll(conn, cstmt, rs);
}
//按出版日期查询前十本新书
public ArrayList<Goods> readGoodsByPublicationDate(){
ArrayList<Goods> listGoods = new ArrayList<Goods>();
Goods goods = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
String sql = "select top(10) * from vw_Goods_Actor_Publication_GoodsType order by PublicationDate desc";
conn = ConnManager.getConn();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
goods = new Goods();
goods.setIsbn(rs.getString(1));
goods.setBookName(rs.getString(2));
goods.setMarketPrice(rs.getDouble(3));
goods.setMemberPrice(rs.getDouble(4));
goods.setNumber(rs.getInt(5));
goods.setImage(rs.getString(6));
goods.setBookIntroduce(rs.getString(7));
goods.setActorName(rs.getString(9));
goods.setPublicationName(rs.getString(11));
goods.setAddDate(rs.getDate(12));
goods.setGoodsTypeName(rs.getString(14));
goods.setPublicationDate(rs.getDate(15));
listGoods.add(goods);
}
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
ConnManager.closeAll(conn, null, pstmt, rs);
return listGoods;
}
//实现关键字搜索 模糊查询
public ArrayList<String> readBookName(String bookName){
ArrayList<String> listString = new ArrayList<String>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select top(10) BookName from Goods where BookName like '%"+ bookName +"%'";
try {
conn = ConnManager.getConn();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
String goodsName = rs.getString(1);
listString.add(goodsName);
}
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
ConnManager.closeAll(conn, null, pstmt, rs);
return listString;
}
//根据图书的ISBN号查询图书的信息
public void readBookDetailByISBN(Goods goods){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from vw_Goods_Actor_Publication_GoodsType where ISBN = ?";
try {
conn = ConnManager.getConn();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, goods.getIsbn());
rs = pstmt.executeQuery();
while(rs.next()){
goods.setIsbn(rs.getString(1));
goods.setBookName(rs.getString(2));
goods.setMarketPrice(rs.getDouble(3));
goods.setMemberPrice(rs.getDouble(4));
goods.setNumber(rs.getInt(5));
goods.setImage(rs.getString(6));
goods.setBookIntroduce(rs.getString(7));
goods.setActorName(rs.getString(9));
goods.setPublicationName(rs.getString(11));
goods.setAddDate(rs.getDate(12));
goods.setGoodsTypeName(rs.getString(14));
goods.setPublicationDate(rs.getDate(15));
goods.setActorIntroduce(rs.getString(16));
}
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
ConnManager.closeAll(conn, null, pstmt, rs);
}
}
评论5