package com.ambow.ecport.imp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import com.ambow.ecport.dao.ProductDao;
import com.ambow.ecport.entity.Category;
import com.ambow.ecport.entity.Product;
import com.ambow.ecport.entity.User;
import com.ambow.ecport.exception.ManagerProductException;
import com.ambow.ecport.util.DBConnection;
/**
* 管理数据库中产品表的数据的增删改查操作的类 实现接口中定义的方法
*
* @author user
*
*/
public class ProductDaoImp implements ProductDao {
// 增加产品的 SQL 语句
private String addSql = "INSERT INTO product (name,descriptions,basePrice,category_id,author,publish,pages, images) "
+ "VALUES ( ?, ?, ?, ?, ?, ?, ?, ?);";
private String updateSql = "update product set name=?,author=?,publish=?,pages=?,images=?,basePrice=? where category_id=?";
private String deleteSql = "delete from product where product_id=?";
private String selectByID = "select * from product where product_id=?";
private String selectAll = "select * from product";
private String selectCount = "select count(*) as count from product";
/**
* 在数据库中增加新的产品
*
* @param product
* 要添加到数据库中的产品信息
* @throws 抛出专门处理
* product 类型的异常
*/
public void add(Product product) throws ManagerProductException {
// 定义数据库连接操作所需要的对象
Connection conn = null;
PreparedStatement ps = null;
DBConnection dbc = null;
// 定义数据库连接类对象,用于连接数据库
try {
dbc = new DBConnection();
conn = dbc.getConnection();
// 预处理 SQL 语句
ps = conn.prepareStatement(addSql);
// 给 SQL 语句中的 ? 赋值
ps.setString(1, product.getName());
ps.setString(2, product.getDescriptions());
ps.setDouble(3, product.getBasePrice());
ps.setInt(4, product.getCategory().getId().intValue()); // 类型 id
ps.setString(5, product.getAuthor());
ps.setString(6, product.getPublish());
ps.setInt(7, product.getPages()); // 页数
ps.setString(8, product.getImages());
boolean b = ps.execute();
System.out.println("插入数据的结果 :" + b);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
dbc.closeConnection(conn);
}
}
/**
* 删除数据库中一个产品
*
* @param product
* 要删除的产品信息
* @throws 抛出专门处理
* product 类型的异常
*/
public void delete(Product product) throws ManagerProductException {
// 定义数据库连接操作所需要的对象
Connection conn = null;
PreparedStatement ps = null;
DBConnection dbc = null;
// 定义数据库连接类对象,用于连接数据库
try {
dbc = new DBConnection();
conn = dbc.getConnection();
// 预处理 SQL 语句
ps = conn.prepareStatement(this.deleteSql);
// delete from product where product_id=?
// 给 SQL 语句中的 ? 赋值
ps.setInt(1, product.getId().intValue());
boolean b = ps.execute();
System.out.println("删除数据的结果 :" + b);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
dbc.closeConnection(conn);
}
}
/**
* 更新数据库中一个用户的信息
*
* @param product
* 要修改的产品信息
* @throws 抛出专门处理
* product 类型的异常
*/
public void update(Product product) throws ManagerProductException {
// 定义数据库连接操作所需要的对象
Connection conn = null;
PreparedStatement ps = null;
DBConnection dbc = null;
// 定义数据库连接类对象,用于连接数据库
try {
dbc = new DBConnection();
conn = dbc.getConnection();
// 预处理 SQL 语句
ps = conn.prepareStatement(this.updateSql);
// 给 SQL 语句中的 ? 赋值
ps.setString(1, product.getName());
ps.setString(2, product.getAuthor());
ps.setString(3, product.getPublish());
ps.setInt(4, product.getPages()); // 页数
ps.setString(5, product.getImages());
ps.setDouble(6, product.getBasePrice());
ps.setInt(7, product.getCategory().getId().intValue()); // 类型 id
ps.setInt(8, product.getId().intValue());
boolean b = ps.execute();
System.out.println("修改数据的结果 :" + b);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
dbc.closeConnection(conn);
}
}
/**
* 根据 id ,查找产品
*
* @param id
* 要查找的产品的 id
* @return Product 查找到的产品对象
* @throws 抛出专门处理
* product 类型的异常
*/
public Product findById(Integer id) throws ManagerProductException {
Product pro=new Product();
Category category=new Category();
int category_id=0;
try
{
DBConnection dbconn = new DBConnection();
Connection conn=dbconn.getConnection();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from product where product_id="+id);
while(rs.next())
{
pro.setId(rs.getInt("product_id"));
pro.setName(rs.getString("name"));
pro.setAuthor(rs.getString("author"));
pro.setPublish(rs.getString("publish"));
pro.setPages(rs.getInt("pages"));
pro.setImages(rs.getString("images"));
pro.setBasePrice(rs.getDouble("basePrice"));
pro.setDescriptions(rs.getString("descriptions"));
category_id=rs.getInt("category_id");
DBConnection dbconn2 = new DBConnection();
Connection conn2=dbconn2.getConnection();
Statement stmt2=conn2.createStatement();
ResultSet rss=stmt2.executeQuery("select * from category where category_id="+category_id);
while(rss.next())
{
category.setId(rss.getInt("category_id"));
category.setName(rss.getString("name"));
category.setDescriptions(rss.getString("descriptions"));
pro.setCategory(category);
}
stmt2.close();
conn2.close();
}
stmt.close();
conn.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return pro;
}
/**
* 查找全部产品
*
* @return 返回保存查找到的所有产品的集合对象
* @throws 抛出专门处理
* product 类型的异常
*/
public Collection findAll() throws ManagerProductException {
Collection list=new ArrayList();
int category_id=0;
try
{
DBConnection dbconn = new DBConnection();
Connection conn=dbconn.getConnection();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from product");
while(rs.next())
{
Product pro=new Product();
pro.setId(rs.getInt("product_id"));
pro.setName(rs.getString("name"));
pro.setAuthor(rs.getString("author"));
pro.setPublish(rs.getString("publish"));
pro.setPages(rs.getInt("pages"));
pro.setImages(rs.getString("images"));
pro.setBasePrice(rs.getDouble("basePrice"));
pro.setDescriptions(rs.getString("descriptions"));
category_id=rs.getInt("category_id");
Statement stmt2=conn.createStatement();
ResultSet rss=stmt2.executeQuery("select * from category where category_id="+category_id);
while(rss.next())
{
Category category=new Category();
category.setId(Integer.valueOf(rss.getInt("category_id")));
category.setName(rss.getString("name"));
category.setDescriptions(rss.getString("descriptions"));
pro.setCategory(category);
list.add(pro);
}
}
}
catch(Exception e)
{
System.out.println(e.getMess
没有合适的资源?快使用搜索试试~ 我知道了~
java实训项目 安博教育集团
共267个文件
svn-base:59个
class:38个
java:38个
3星 · 超过75%的资源 需积分: 12 17 下载量 175 浏览量
2011-06-28
16:39:34
上传
评论
收藏 2.8MB RAR 举报
温馨提示
java实训项目,在安博时候做的一个实训项目,很详细,用的主流SSH框架,都可以运行!
资源推荐
资源详情
资源评论
收起资源包目录
java实训项目 安博教育集团 (267个子文件)
all-wcprops 2KB
all-wcprops 2KB
all-wcprops 600B
all-wcprops 600B
all-wcprops 552B
all-wcprops 552B
all-wcprops 515B
all-wcprops 498B
all-wcprops 498B
all-wcprops 376B
all-wcprops 266B
all-wcprops 244B
all-wcprops 244B
all-wcprops 240B
all-wcprops 240B
all-wcprops 230B
all-wcprops 214B
all-wcprops 204B
all-wcprops 200B
all-wcprops 200B
all-wcprops 192B
ProductDaoImp.class 9KB
UserDaoHibernateImp.class 9KB
OrderDaoHibernateImp.class 8KB
RegisterServlet.class 5KB
TestUserDaoImp.class 4KB
ModifyServlet.class 4KB
GetProductListServlet.class 4KB
GetOrderDetailServlet.class 4KB
ContcatInfo.class 4KB
ShopCart.class 3KB
Order.class 3KB
ShowCartShowServlet.class 3KB
DeleteProductServlet.class 3KB
ModifyShopCartServlet.class 3KB
DBConnection.class 3KB
GetProductDetailServlet.class 3KB
LoginServlet.class 3KB
FindByIdServlet.class 3KB
GetOrderListServlet.class 3KB
ShowOrderServlet.class 2KB
Product.class 2KB
AddOrderServlet.class 2KB
EncodingFilter.class 2KB
LogoutServlet.class 2KB
ClearShopCartServlet.class 2KB
User.class 1KB
OrderLine.class 1KB
Category.class 1KB
Province.class 1KB
OrderStatus.class 1009B
Country.class 907B
PayWay.class 800B
ManagerProductException.class 629B
ManagerOrderException.class 623B
ManagerUserException.class 620B
UserDao.class 540B
ProductDao.class 417B
OrderDao.class 408B
.classpath 692B
cart_show_style.css 10KB
style.css 10KB
style_product_list.css 8KB
style_order_list.css 8KB
Thumbs.db 80KB
GWAP项目开发任务单_day4.doc 59KB
entries 2KB
entries 2KB
entries 760B
entries 760B
entries 746B
entries 746B
entries 743B
entries 731B
entries 716B
entries 716B
entries 442B
entries 423B
entries 422B
entries 420B
entries 420B
entries 419B
entries 419B
entries 412B
entries 407B
entries 407B
entries 403B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
共 267 条
- 1
- 2
- 3
资源评论
- wskuwlihc2014-08-14有一些参考价值 对于新手
奔跑吧__小犀牛
- 粉丝: 46
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功