package com.bjsxt.shopping.book;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.bjsxt.shopping.category.Category;
import com.bjsxt.shopping.util.DB;
public class BookService {
public static void add(Book b) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DB.getConn();
String sql = "insert into book (ISBN,bookname,author,publisher,descr,normalprice,memberprice,pdate,categoryid,num) values (?,?,?,?, ?, ?, ?, ?, ?,?)";
pstmt = DB.prepare(conn, sql);
pstmt.setString(1, b.getISBN());
pstmt.setString(2, b.getBookname());
pstmt.setString(3, b.getAuthor());
pstmt.setString(4, b.getPublisher());
pstmt.setString(5, b.getDescr());
pstmt.setDouble(6, b.getNormalPrice());
pstmt.setDouble(7, b.getMemberPrice());
pstmt.setTimestamp(8, new Timestamp(b.getPdate().getTime()));
pstmt.setInt(9, b.getCategoryId());
pstmt.setInt(10, b.getNum());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(pstmt);
DB.close(conn);
}
}
public static void delete(String ISBN) {
Connection conn = null;
Statement stmt = null;
String sql;
try {
conn = DB.getConn();
sql = "delete from book where ISBN = '" + ISBN+"'";
stmt = DB.getStatement(conn);
DB.executeUpdate(stmt, sql);
} finally {
DB.close(stmt);
DB.close(conn);
}
}
public static List<Book> getBooks() {
Connection conn = DB.getConn();
Statement stmt = DB.getStatement(conn);
String sql = "select * from book order by pdate desc";
ResultSet rs = DB.getResultSet(stmt, sql);
List<Book> books = new ArrayList<Book>();
try {
while (rs.next()) {
Book b = getBookFromRs(rs);
books.add(b);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(stmt);
DB.close(rs);
DB.close(conn);
}
return books;
}
/**
* @param lazy 为true时,只取Product的信息,否则同时取出Product内Category对象的信息
*/
public static int getBooks(List<Book> books, int pageNo, int pageSize, boolean lazy) {
int totalRecords = -1;
Connection conn = DB.getConn();
Statement stmt = DB.getStatement(conn);
String sql = "";
if(lazy) {
sql = "select * from book order by pdate desc";
} else {
sql = "select b.ISBN, b.bookname, b.author,b.publisher,b.descr bdescr, b.normalprice, " +
" b.memberprice, b.pdate, b.categoryid ,b.picname,b.num, " +
" c.id categoryid, c.name cname, c.descr cdescr, c.pid, c.cno, c.grade " +
" from book b join category c on (b.categoryid = c.id) order by b.pdate desc";
}
//sql += " limit " + (pageNo - 1) * pageSize + "," + pageSize;
ResultSet rs = DB.getResultSet(stmt, sql);
Statement stmtCount = DB.getStatement(conn);
ResultSet rsCount = DB.getResultSet(stmtCount,
"select count(*) from book");
//products = new ArrayList<Product>(); 千万小心这句话不要添加
try {
rsCount.next();
totalRecords = rsCount.getInt(1);
while (rs.next()) {
Book b = null;
if(lazy) {
b = getBookFromRs(rs);
} else {
b = new Book();
b.setISBN(rs.getString("ISBN"));
b.setBookname(rs.getString("bookname"));
b.setAuthor(rs.getString("author"));
b.setPublisher(rs.getString("publisher"));
b.setDescr(rs.getString("bdescr"));
b.setNormalPrice(rs.getDouble("normalprice"));
b.setMemberPrice(rs.getDouble("memberprice"));
b.setPdate(rs.getTimestamp("pdate"));
b.setCategoryId(rs.getInt("categoryid"));
b.setPicname(rs.getString("picname"));
b.setNum(rs.getInt("num"));
Category c = new Category();
c.setId(rs.getInt("categoryid"));
c.setName(rs.getString("cname"));
c.setDescr(rs.getString("cdescr"));
c.setPid(rs.getInt("pid"));
c.setCno(rs.getInt("cno"));
c.setGrade(rs.getInt("grade"));
b.setCategory(c);
}
books.add(b);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rsCount);
DB.close(stmtCount);
DB.close(stmt);
DB.close(rs);
DB.close(conn);
}
return totalRecords;
}
public static Book loadByISBN(String ISBN) {
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
Book b = null;
try {
String sql = "select * from book where ISBN = '" + ISBN+"'";
conn = DB.getConn();
stmt = DB.getStatement(conn);
rs = DB.getResultSet(stmt, sql);
if (rs.next()) {
b = getBookFromRs(rs);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(stmt);
DB.close(rs);
DB.close(conn);
}
return b;
}
public static void update(Book b) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DB.getConn();
String sql = "update book set bookname=? , author=? , publisher=? , descr=?, normalprice=?, memberprice=?,pdate=?, categoryid=?,num=? where ISBN=?";
pstmt = DB.prepare(conn, sql);
pstmt.setString(1, b.getBookname());
pstmt.setString(2, b.getAuthor());
pstmt.setString(3, b.getPublisher());
pstmt.setString(4, b.getDescr());
pstmt.setDouble(5, b.getNormalPrice());
pstmt.setDouble(6, b.getMemberPrice());
pstmt.setTimestamp(7, new Timestamp(b.getPdate().getTime()));
pstmt.setInt(8, b.getCategoryId());
pstmt.setInt(9, b.getNum());
pstmt.setString(10, b.getISBN());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(pstmt);
DB.close(conn);
}
}
public static void updatePicname(Book b) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DB.getConn();
String sql = "update book set picname=? where ISBN=?";
pstmt = DB.prepare(conn, sql);
pstmt.setString(1, b.getPicname());
pstmt.setString(2, b.getISBN());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(pstmt);
DB.close(conn);
}
}
private static Book getBookFromRs(ResultSet rs) {
Book b = null;
try {
b = new Book();
b.setISBN(rs.getString("ISBN"));
b.setBookname(rs.getString("bookname"));
b.setAuthor(rs.getString("author"));
b.setPublisher(rs.getString("publisher"));
b.setDescr(rs.getString("descr"));
b.setNormalPrice(rs.getDouble("normalprice"));
b.setMemberPrice(rs.getDouble("memberprice"));
b.setPdate(rs.getTimestamp("pdate"));
b.setCategoryId(rs.getInt("categoryid"));
b.setPicname(rs.getString("picname"));
b.setNum(rs.getInt("num"));
} catch (SQLException e) {
e.printStackTrace();
}
return b;
}
public static void delete(String [] ISBNs) {
Connection conn = null;
Statement stmt = null;
String sql = "delete from book where ISBN in ('-1'";
for(int i=0;i<ISBNs.length;i++){
sql += ",'"+ISBNs[i]+"'";
}
sql +=")";
try {
conn = DB.getConn();
stmt = DB.getStatement(conn);
DB.executeUpdate(stmt, sql);
} finally {
DB.close(stmt);
DB.close(conn);
}
}
public static int find(List<Book> books, int pageNo, int pageSize, int categoryid) {
String queryStr = " where b.categoryid="+categoryid;
int totalRecords = -1;
Connection conn = DB.getConn();
Statement stmt = DB.getStatement(conn);
String sql = "";
sql = "select b.ISBN, b.bookname, b.author,b.publisher,b.descr bdescr, b.normalprice, " +
" b.memberprice, b.pdate, b.categoryid ,b.picname,b.num, " +
" c.id categoryid, c.name cname, c.descr cdescr, c.pid, c.cno, c.grade " +
" from book b join category c on (b.categoryid = c.id)" + queryStr +
" order by b.pdate desc";
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JSP+Javabean 网上商城书店 (586个子文件)
thcode.bmp 142B
BookService.class 14KB
OrderService.class 10KB
User.class 9KB
CategoryService.class 7KB
FileUpload.class 5KB
DB.class 4KB
Book.class 3KB
BookSearchFormBean.class 3KB
Category.class 3KB
SalesOrder.class 2KB
Cart.class 2KB
TestContextParamServlet.class 2KB
AuthFilter.class 2KB
ConvertDate.class 2KB
IPFilter.class 2KB
LoggedUserCounter.class 2KB
SalesItem.class 1KB
PropertyMgr.class 1KB
CartItem.class 789B
PasswordNotCorrectException.class 428B
UserNotFoundException.class 410B
GradeOutOfBoundsException.class 366B
.classpath 824B
datepicker.css 5KB
datepicker.css 5KB
new.css 4KB
style.css 2KB
toc.css 620B
WdatePicker.css 158B
newtitle.gif 202KB
pma_24.gif 60KB
pma_banner_12_1.gif 59KB
pma_4.gif 54KB
pma_20.gif 48KB
brand_banner.gif 47KB
pma_10.gif 37KB
pma_banner_21_1.gif 37KB
pma_banner_22_2.gif 36KB
bitmap.gif 34KB
index_01.gif 30KB
na-185.gif 21KB
tt0850.gif 13KB
pingpai_new.gif 11KB
242-98.gif 11KB
index.gif 11KB
fcthink2.gif 9KB
leftmf.gif 8KB
na-167.gif 8KB
paihang_top.gif 8KB
index_02.gif 8KB
op0141.gif 8KB
boy151.gif 8KB
tel.gif 7KB
redsign.gif 7KB
ex_dz.gif 7KB
ex_my.gif 6KB
index_11.gif 6KB
ex_help.gif 6KB
br0064.gif 5KB
op0129.gif 5KB
op0155.gif 5KB
op0147.gif 5KB
op0174.gif 5KB
sv0056.gif 5KB
sv0054.gif 5KB
sv0055.gif 5KB
thinkpad_logo.gif 5KB
op0149.gif 5KB
tt0868.gif 5KB
op0156.gif 5KB
bk0001.gif 5KB
tt0922.gif 5KB
tt0923.gif 5KB
tt0919.gif 5KB
op0173.gif 5KB
sv0051.gif 5KB
tt0910.gif 5KB
tt0907.gif 5KB
op0169.gif 4KB
op0172.gif 4KB
tt0700.gif 4KB
br0103.gif 4KB
tt0906.gif 4KB
sr_01.gif 4KB
cp0053.gif 4KB
op0170.gif 4KB
zp0226.gif 4KB
na-163.gif 4KB
op0134.gif 4KB
tt0869.gif 4KB
op0010.gif 4KB
op0171.gif 4KB
op0120.gif 4KB
op0154.gif 4KB
na-140-1.gif 4KB
br0006.gif 4KB
br0007.gif 4KB
br0016.gif 4KB
br0068.gif 4KB
共 586 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
- yingii2013-01-03还可以,但不是特别美观
- u0117907972013-10-14还可以吧 就是界面需要优化 总体还行
- 鹏程902015-05-16有点问题啊
- qq32661502013-05-18很好,做毕业设计很好的模板
- cswhw2012-12-26不错,可以运行,界面不够美观
feng3128166
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_20241117_024114_com.huawei.browser.jpg
- .turing.dat
- shopex升级补丁只针对 485.78660版本升级至485.80603版本 其它版本的请勿使用!
- 基于Django和HTML的新疆地区水稻产量影响因素可视化分析系统(含数据集)
- windows conan2应用构建模板
- 3_base.apk.1
- 鼎微R16中控升级包R16-4.5.10-20170221及强制升级方法
- 基于STM32F103C8T6的4g模块(air724ug)
- 基于Java技术的ASC学业支持中心并行项目开发设计源码
- 基于Java和微信支付的wxmall开源卖票商城设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功