package s2jsp.bysj.dao.Impl;
import java.util.*;
import s2jsp.bysj.dao.*;
import s2jsp.bysj.entity.Product;
import java.sql.*;
public class ProductDaoImpl extends BaseDao implements ProductDao {
private Connection con = null;
private PreparedStatement pStatement = null;
private ResultSet rSet = null;
public static final int PAGE_NUM = 5;// 定义产品列表页面显示产品个数
/**
* 根据产品Id查找产品
*
* @param uId
* @return
*/
public Product findProduct(int productId) {
Product product = null;
String sql = "select * from product where productid=?";
try {
con = getConn();
pStatement = con.prepareStatement(sql);
pStatement.setInt(1, productId);
rSet = pStatement.executeQuery();
while (rSet.next()) {
product = new Product();
product.setProductId(rSet.getInt("productId"));
product.setSerialNumber(rSet.getString("serialNumber"));
product.setName(rSet.getString("name"));
product.setBrand(rSet.getString("brand"));
product.setModel(rSet.getString("model"));
product.setPrice(rSet.getString("price"));
product.setPicture(rSet.getString("picture"));
product.setDescription(rSet.getString("description"));
}
} catch (SQLException ex) {
ex.getStackTrace();
} finally {
closeAll(rSet, pStatement, con);
}
return product;
}
/**
* 根据产品名查找产品
*
* @param name
* @return
*/
public Product findProduct(String name) {
Product product = null;
String sql = "select * from product where name=?";
try {
con = getConn();
pStatement = con.prepareStatement(sql);
pStatement.setString(1, name);
rSet = pStatement.executeQuery();
while (rSet.next()) {
product = new Product();
product.setProductId(rSet.getInt("productId"));
product.setSerialNumber(rSet.getString("serialNumber"));
product.setName(rSet.getString("name"));
product.setBrand(rSet.getString("brand"));
product.setModel(rSet.getString("model"));
product.setPrice(rSet.getString("price"));
product.setPicture(rSet.getString("picture"));
product.setDescription(rSet.getString("description"));
}
} catch (SQLException ex) {
ex.getStackTrace();
} finally {
closeAll(rSet, pStatement, con);
}
return product;
}
/**
* 返回所有产品的列表
* @return 产品信息列表
*/
public List findAllProduct(){
List list = new ArrayList();
Product product = null;
String sql = "select * from product order by productId desc";
try {
con = getConn();
pStatement = con.prepareStatement(sql);
rSet = pStatement.executeQuery();
while (rSet.next()) {
product = new Product();
product.setProductId(rSet.getInt("productId"));
product.setSerialNumber(rSet.getString("serialNumber"));
product.setName(rSet.getString("name"));
product.setBrand(rSet.getString("brand"));
product.setModel(rSet.getString("model"));
product.setPrice(rSet.getString("price"));
product.setPicture(rSet.getString("picture"));
product.setDescription(rSet.getString("description"));
list.add(product);
}
} catch (SQLException ex) {
ex.getStackTrace();
} finally {
closeAll(rSet, pStatement, con);
}
return list;
};
/**
* 根据page页,返回所有产品列表
*
* @param page
* @return 产品列表
*/
public List findAllProductByPage(int page) {
List list = new ArrayList();
int number = 0;
if (page > 1) {
number = PAGE_NUM * (page - 1);
}
String sql = "select top "
+ PAGE_NUM
+ " * from product where productID not in ( select top "
+ number
+ " productID from product order by productID desc ) order by productID desc";
try {
con = this.getConn();
pStatement = con.prepareStatement(sql);
rSet = pStatement.executeQuery();
while (rSet.next()) {
Product product = new Product();
product.setProductId(rSet.getInt("productID"));
product.setSerialNumber(rSet.getString("serialNumber"));
product.setName(rSet.getString("name"));
product.setBrand(rSet.getString("brand"));
product.setModel(rSet.getString("model"));
product.setPrice(rSet.getString("price"));
product.setPicture(rSet.getString("picture"));
product.setDescription(rSet.getString("description"));
list.add(product);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
this.closeAll(rSet, pStatement,con);
}
return list;
}
/**
* 增加产品,返回增加个数
*
* @param product
* @return
*/
public int addProduct(Product product) {
int num = 0;
String sql = "insert into product(serialNumber,name,brand,model,price,picture,description) values(?,?,?,?,?,?,?)";
try {
con = getConn();
pStatement = con.prepareStatement(sql);
pStatement.setString(1, product.getSerialNumber());
pStatement.setString(2, product.getName());
pStatement.setString(3, product.getBrand());
pStatement.setString(4, product.getModel());
pStatement.setString(5, product.getPrice());
pStatement.setString(6, product.getPicture());
pStatement.setString(7, product.getDescription());
num = pStatement.executeUpdate();
} catch (SQLException ec) {
ec.getStackTrace();
} finally {
closeAll(null, pStatement, con);
}
return num;
}
/**
* 根据产品id删除产品,返回删除个数
*
* @param productId
* @return 删除个数
*/
public int deleteProduct(int productid) {
int num = 0;
String sql="delete from product where productid=?";
try{
con = getConn();
pStatement = con.prepareStatement(sql);
pStatement.setInt(1, productid);
num=pStatement.executeUpdate();
}catch(SQLException ec){
ec.getStackTrace();
}finally{
closeAll(null,pStatement,con);
}
return num;
}
/**
* 更新一个产品的信息,返回更新个数
*
* @param product
* @return 更新个数
*/
public int updateProduct(Product product) {
String sql = "update product set serialnumber=?,name=?,brand=?,model=?,price=?,picture=?,description=? where productID="
+ product.getProductId();
String[] param = new String[] { product.getSerialNumber(),
product.getName(), product.getBrand(), product.getModel(),
product.getPrice(),product.getPicture(), product.getDescription() };
return this.executeSQL(sql, param);
}
}
Accp5.0S2Jsp项目(企业宣传网站)
需积分: 6 27 浏览量
2008-10-16
18:04:13
上传
评论 1
收藏 1.93MB RAR 举报
zhu_zhaowei
- 粉丝: 0
- 资源: 2
最新资源
- 《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第1章docker基础(1.1-1.4)
- 基于python实现的水下压缩空气储能互补系统建模仿真与经济效益分析+源代码+论文
- 华中科技大学-自然语言处理实验,Bi-LSTM+CRF的中文分词框架,并且利用基于深度学习的方法进行中文命名实体识别++源码报告
- 基于动态罚函数的铁路车流分配与径路优化模型python源码
- 鱼群算法求解组环问题python源码+文档说明
- 基于决策优化的多波束测深测线规划模型MATLAB代码
- 课程设计-基于python实现的多目标优化算法求解带时间窗的车辆路径规划问题+源代码+文档说明+界面截图+pptx
- 基于通信信号与通信系统的MATLAB仿真源码-课程设计
- 嵌入式-信号机制(概念,发送,定时,捕捉,SIGCHLD 信号实现回收子进程)
- c语言管理系统大一大二笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈