步骤 2 : 基本的CRUD操作 步骤 3 : 非CRUD方法
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package tmall.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import tmall.bean.Category;
import tmall.bean.Product;
import tmall.bean.ProductImage;
import tmall.util.DBUtil;
import tmall.util.DateUtil;
public class ProductDAO {
public int getTotal(int cid) {
int total = 0;
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select count(*) from Product where cid = " + cid;
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
public void add(Product bean) {
String sql = "insert into Product values(null,?,?,?,?,?,?,?)";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, bean.getName());
ps.setString(2, bean.getSubTitle());
ps.setFloat(3, bean.getOrignalPrice());
ps.setFloat(4, bean.getPromotePrice());
ps.setInt(5, bean.getStock());
ps.setInt(6, bean.getCategory().getId());
ps.setTimestamp(7, DateUtil.d2t(bean.getCreateDate()));
ps.execute();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(Product bean) {
String sql = "update Product set name= ?, subTitle=?, orignalPrice=?,promotePrice=?,stock=?, cid = ?, createDate=? where id = ?";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, bean.getName());
ps.setString(2, bean.getSubTitle());
ps.setFloat(3, bean.getOrignalPrice());
ps.setFloat(4, bean.getPromotePrice());
ps.setInt(5, bean.getStock());
ps.setInt(6, bean.getCategory().getId());
ps.setTimestamp(7, DateUtil.d2t(bean.getCreateDate()));
ps.setInt(8, bean.getId());
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void delete(int id) {
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "delete from Product where id = " + id;
s.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Product get(int id) {
Product bean = new Product();
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select * from Product where id = " + id;
ResultSet rs = s.executeQuery(sql);
if (rs.next()) {
String name = rs.getString("name");
String subTitle = rs.getString("subTitle");
float orignalPrice = rs.getFloat("orignalPrice");
float promotePrice = rs.getFloat("promotePrice");
int stock = rs.getInt("stock");
int cid = rs.getInt("cid");
Date createDate = DateUtil.t2d( rs.getTimestamp("createDate"));
bean.setName(name);
bean.setSubTitle(subTitle);
bean.setOrignalPrice(orignalPrice);
bean.setPromotePrice(promotePrice);
bean.setStock(stock);
Category category = new CategoryDAO().get(cid);
bean.setCategory(category);
bean.setCreateDate(createDate);
bean.setId(id);
setFirstProductImage(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bean;
}
public List<Product> list(int cid) {
return list(cid,0, Short.MAX_VALUE);
}
public List<Product> list(int cid, int start, int count) {
List<Product> beans = new ArrayList<Product>();
Category category = new CategoryDAO().get(cid);
String sql = "select * from Product where cid = ? order by id desc limit ?,? ";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, cid);
ps.setInt(2, start);
ps.setInt(3, count);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Product bean = new Product();
int id = rs.getInt(1);
String name = rs.getString("name");
String subTitle = rs.getString("subTitle");
float orignalPrice = rs.getFloat("orignalPrice");
float promotePrice = rs.getFloat("promotePrice");
int stock = rs.getInt("stock");
Date createDate = DateUtil.t2d( rs.getTimestamp("createDate"));
bean.setName(name);
bean.setSubTitle(subTitle);
bean.setOrignalPrice(orignalPrice);
bean.setPromotePrice(promotePrice);
bean.setStock(stock);
bean.setCreateDate(createDate);
bean.setId(id);
bean.setCategory(category);
setFirstProductImage(bean);
beans.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return beans;
}
public List<Product> list() {
return list(0,Short.MAX_VALUE);
}
public List<Product> list(int start, int count) {
List<Product> beans = new ArrayList<Product>();
String sql = "select * from Product limit ?,? ";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, start);
ps.setInt(2, count);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Product bean = new Product();
int id = rs.getInt(1);
int cid = rs.getInt("cid");
String name = rs.getString("name");
String subTitle = rs.getString("subTitle");
float orignalPrice = rs.getFloat("orignalPrice");
float promotePrice = rs.getFloat("promotePrice");
int stock = rs.getInt("stock");
Date createDate = DateUtil.t2d( rs.getTimestamp("createDate"));
bean.setName(name);
bean.setSubTitle(subTitle);
bean.setOrignalPrice(orignalPrice);
bean.setPromotePrice(promotePrice);
bean.setStock(stock);
bean.setCreateDate(createDate);
bean.setId(id);
Category category = new CategoryDAO().get(cid);
bean.setCategory(category);
beans.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return beans;
}
public void fill(List<Category> cs) {
for (Category c : cs)
fill(c);
}
public void fill(Category c) {
List<Product> ps = this.list(c.getId());
c.setProducts(ps);
}
public void fillByRow(List<Category> cs) {
int productNumberEachRow = 8;
for (Category c : cs) {
List<Product> products = c.getProducts();
List<List<Product>> productsByRow = new ArrayList<>();
for (int i = 0; i < products.size(); i+=productNumberEachRow) {
int size = i+productNumberEachRow;
size= size>products.size()?products.size():size;
List<Product> productsOfEachRow =products.subList(i, size);
productsByRow.add(productsOfEachRow);
}
c.setProductsByRow(productsByRow);
}
}
public void setFirstProductImage(Product p) {
List<ProductImage> pis= new ProductImageDAO().list(p, ProductImageDAO.type_single);
if(!pis.isEmpty())
p.setFirstProductImage(pis.get(0));
}
public void setSaleAndReviewNumber(Product p) {
int saleCount = new OrderItemDAO().getSaleCount(p.getId());
p.setSaleCount(saleCount);
int reviewCount = new ReviewDAO().getCount(p.getId());
p.setReviewCount(reviewCount);
}
public void setSaleAndReviewNumber(List<Product> products) {
for (Product p : products) {
setSaleAndReviewNumber(p);
}
}
public List<Product> search(String keyword, int start, int count) {
List<Product> beans = new ArrayList<Product>();
if(null==keyword||0==keyword.trim().length())
return beans;
String sql = "select * from Product where name like ? limit ?,? ";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, "%"+keyword.trim()+"%");
ps.setInt(2, start);
ps.setInt(3, count);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Product bean = new Product();
int id = rs.getInt(1);
int cid = rs.getInt("cid");
String name = rs.getString("name");
String subTitle = rs.getString("subTitle");
float orignalPrice = rs.getFloat("orignalPrice");
float promotePrice = rs.getFloat("promotePrice");
int stock = rs.getInt("stock");
Date createDate = DateUtil.t2d( rs.getTimestamp("createDate"));
bean.setName(name);
bean.setSubTitle(subTitle);
bean.setOrignalPrice(orignalPrice);
bean.setPromotePrice(promotePrice);
bean.setStock(stock);
bean.setCreateDate(createDate);
bean.setId(id);
Category category = new CategoryDAO().get(cid);
bean.setCategory(category);
setFirstProductImage(bean);
beans.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return beans;
}
}
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2022-05-21
太牛了啊站长,为什么后面的业务逻辑可以处理的这么紧密啊,完全都想不到啊
2021-08-14
美滋滋
2020-10-14
ProductImageDAO.type_single提示type_single cannot be resolved or is not a field,不知道怎么回事
2020-06-27
代码看不懂
2019-12-28
为什么list(int start, int count)方法中没有调用setFirstProductImage(bean)方法啊
提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 11 条以前的提问,请 点击查看
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|