package cn.sina.ttjava_13.product;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cn.sina.ttjava_13.category.Category;
import cn.sina.ttjava_13.database.DB;
public class ProductMysqlDAO implements ProductDAO {
public List<Product> getProducts() {
List<Product> list = new ArrayList<Product>();
Connection conn = null;
ResultSet rs = null;
String sql = "select * from product";
try {
conn = DB.getConn();
rs = DB.getRs(conn, sql);
while(rs.next()) {
Product p = new Product();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setDescr(rs.getString("descr"));
p.setNormalPrice(rs.getDouble("normalPrice"));
p.setMemberPrice(rs.getDouble("memberPrice"));
p.setPdate(rs.getTimestamp("pdate"));
p.setCategoryId(rs.getInt("categoryId"));
list.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(conn);
}
return list;
}
public List<Product> getProducts(int pageNo,int pageSize) {
List<Product> list = new ArrayList<Product>();
Connection conn = null;
ResultSet rs = null;
String sql = "select product.id,product.name,product.descr,product.normalprice,product.memberprice,product.pdate,product.categoryid" +
", category.id cid,category.pid,category.name cname,category.descr cdescr,category.isleaf,category.grade" +
" from product join category on (product.categoryid = category.id) limit " + (pageNo-1)*pageSize + "," + pageSize;
System.out.println(sql);
try {
conn = DB.getConn();
rs = DB.getRs(conn, sql);
while(rs.next()) {
Product p = new Product();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setDescr(rs.getString("descr"));
p.setNormalPrice(rs.getDouble("normalPrice"));
p.setMemberPrice(rs.getDouble("memberPrice"));
p.setPdate(rs.getTimestamp("pdate"));
p.setCategoryId(rs.getInt("categoryId"));
Category c = new Category();
c.setId(rs.getInt("cid"));
c.setPid(rs.getInt("pid"));
c.setName(rs.getString("cname"));
c.setDescr(rs.getString("cdescr"));
c.setLeaf(rs.getInt("isleaf") == 0 ? true : false);
c.setGrade(rs.getInt("grade"));
p.setCategory(c);
list.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(conn);
}
return list;
}
public int findProducts( List<Product> list,
String keyword,
double lowNormalPrice,
double highNormalPrice,
double lowMemberPrice,
double highMemberPrice,
Date startPdate,
Date endPdate,
int categoryId,
int pageNo,
int pageSize) {
Connection conn = null;
ResultSet rs = null;
String sql = "select * from product where 1=1";
//閸氬孩娼甸悽鍙樼艾鐟曚胶鐓¢柆鎾达拷妞ゅ灚鏆熸晶鐐插閻拷
int totalPages = 0;
String sqlCount = "";
ResultSet rsCount = null;
if(categoryId != 0) {
sql += " and categoryId = " + categoryId;
}
if(keyword != null && !keyword.trim().equals("")) {
//濡紕纭﹂弻銉嚄
sql += " and name like '%" + keyword + "%' or descr like '%" + keyword + "%'";
}
if(lowNormalPrice >= 0) {
sql += " and normalprice > " + lowNormalPrice;
}
if(highNormalPrice > 0) {
sql += " and normalprice < " + highNormalPrice;
}
if(lowMemberPrice >= 0) {
sql += " and memberprice > " + lowMemberPrice;
}
if(highMemberPrice >= 0) {
sql += " and memberprice > " + highMemberPrice;
}
if(startPdate != null ) {
sql += " pdate >" + new SimpleDateFormat("yyyy-MM-dd").format(startPdate);
}
if(endPdate != null ) {
sql += " pdate <" + new SimpleDateFormat("yyyy-MM-dd").format(endPdate);
}
//是计算完整个页码之后才进行分页
sqlCount = sql.replaceFirst("select \\*", "select count(*)");
//System.out.println("sqlCount = " + sqlCount);
//fen ye
sql += " limit " + (pageNo-1)*pageSize + "," + pageSize;
//System.out.println(sql);
try {
conn = DB.getConn();
rsCount = DB.getRs(conn, sqlCount);
rsCount.next();
totalPages = (rsCount.getInt(1) + pageSize - 1) / pageSize;
rs = DB.getRs(conn, sql);
while(rs.next()) {
Product p = new Product();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setDescr(rs.getString("descr"));
p.setNormalPrice(rs.getDouble("normalPrice"));
p.setMemberPrice(rs.getDouble("memberPrice"));
p.setPdate(rs.getTimestamp("pdate"));
p.setCategoryId(rs.getInt("categoryId"));
list.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(conn);
}
return totalPages;
}
public boolean updateProduct(Product p) {
Connection conn = null;
PreparedStatement pStmt = null;
String sql = "update product set name=?,descr=?,normalPrice=?,memberPrice=?,categoryId=? where id=" + p.getId();
try {
conn = DB.getConn();
pStmt = DB.createPreparedStmt(conn, sql);
pStmt.setString(1, p.getName());
pStmt.setString(2, p.getDescr());
pStmt.setDouble(3, p.getNormalPrice());
pStmt.setDouble(4, p.getMemberPrice());
pStmt.setInt(5, p.getCategoryId());
pStmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
DB.close(pStmt);
DB.close(conn);
}
return true;
}
public boolean deleteProductById(int[] idArray) {
return false;
}
public boolean deleteProductByCategoryId(int categoryId) {
return false;
}
public boolean addProduct(Product p) {
Connection conn = null;
PreparedStatement pStmt = null;
String sql = "insert into product values(null,?,?,?,?,?,?)";
try {
conn = DB.getConn();
pStmt = DB.createPreparedStmt(conn, sql);
pStmt.setString(1, p.getName());
pStmt.setString(2, p.getDescr());
pStmt.setDouble(3, p.getNormalPrice());
pStmt.setDouble(4, p.getMemberPrice());
pStmt.setTimestamp(5, p.getPdate());
pStmt.setInt(6, p.getCategoryId());
pStmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
DB.close(pStmt);
DB.close(conn);
}
return true;
}
public int getTotalPages(int pageSize) {
int totalPages = 1;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select count(*) from product";
try {
conn = DB.getConn();
stmt = DB.createStmt(conn);
rs = stmt.executeQuery(sql);
rs.next();
int temp = rs.getInt(1);
//System.out.println(temp);
totalPages = (temp + pageSize - 1)/pageSize;
//System.out.println(totalPages);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(stmt);
DB.close(conn);
}
return totalPages;
}
/*
* (non-Javadoc)
* @see cn.sina.ttjava_13.product.ProductDAO#getProduct(java.util.List, int, int)
* 閸︺劌绶遍崚棰侀獓閸濅礁鍨悰銊ф畱閸氬本妞傛潻妯垮厴瀵版鍩岄幀鑽ゆ畱妞ょ數鐖滈弫锟� */
public int getProduct(List<Product> products, int pageNo, int pageSize) {
int totalPages = 0;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from product limit " + (pageNo-1)*pageSize + "," + pageSize;
try {
conn = DB.getConn();
rs = DB.getRs(conn, sql);
while(rs.next()) {
Product p = new Product();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setDescr
没有合适的资源?快使用搜索试试~ 我知道了~
网上在线商城系统jsp
共961个文件
gif:652个
jpg:154个
class:32个
2星 需积分: 19 41 下载量 172 浏览量
2018-12-18
23:01:26
上传
评论 4
收藏 6.3MB ZIP 举报
温馨提示
java+jsp+Tomcat+mysql实现的一个网上在线购物商城,java我用的最新的,Tomcat我用的8.5
资源推荐
资源详情
资源评论
收起资源包目录
网上在线商城系统jsp (961个子文件)
thcode.bmp 142B
ProductMysqlDAO.class 13KB
ProductMysqlDAO.class 12KB
CategoryDAO.class 7KB
CategoryDAO.class 7KB
UserManager.class 4KB
UserManager.class 4KB
ProductMgr.class 4KB
ProductMgr.class 4KB
DB.class 4KB
DB.class 3KB
OrderMysqlDAO.class 3KB
Category.class 3KB
Category.class 3KB
OrderMysqlDAO.class 3KB
User.class 3KB
User.class 2KB
Product.class 2KB
Product.class 2KB
SaleOrder.class 2KB
SaleOrder.class 2KB
Cart.class 2KB
Cart.class 2KB
ProductDAO.class 1KB
ProductDAO.class 1KB
CartItem.class 1KB
CartItem.class 1KB
OrderMgr.class 1KB
OrderMgr.class 1KB
UserNotFoundException.class 337B
UserNotFoundException.class 337B
PasswordErrException.class 334B
PasswordErrException.class 334B
.classpath 840B
org.eclipse.wst.common.component 464B
org.eclipse.wst.jsdt.ui.superType.container 49B
global.css 35KB
newhead.css 23KB
newhead.css 23KB
index.css 14KB
Default.css 9KB
style.css 7KB
User_Login.css 6KB
css1.css 5KB
css1.css 5KB
xtree.css 5KB
new.css 4KB
style.css 2KB
lightred.css 1KB
yellow.css 1KB
green.css 1KB
brown.css 1KB
blue.css 1KB
toc.css 620B
ie6.css 218B
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
1281744644030083310.gif 18KB
crm_13.gif 16KB
crm_13.gif 16KB
shihuiggao_bg_top.gif 14KB
tt0850.gif 13KB
crm_12.gif 12KB
crm_12.gif 12KB
pingpai_new.gif 11KB
242-98.gif 11KB
index.gif 11KB
crm_22.gif 11KB
crm_22.gif 11KB
crm_8.gif 11KB
crm_8.gif 11KB
fcthink2.gif 9KB
leftmf.gif 8KB
na-167.gif 8KB
paihang_top.gif 8KB
index_02.gif 8KB
crm_11.gif 8KB
crm_11.gif 8KB
op0141.gif 8KB
boy151.gif 8KB
crm_5.gif 8KB
crm_5.gif 8KB
crm_15.gif 7KB
crm_15.gif 7KB
tel.gif 7KB
user_top_c.gif 7KB
redsign.gif 7KB
crm_21.gif 7KB
crm_21.gif 7KB
ex_dz.gif 7KB
共 961 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
- weixin_459443792019-11-28报错太多,新手很难懂weixin_387454882019-12-16不要慌问题不大,总会调通的,您是祖国的花朵
weixin_38745488
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功