package org.yidu.ebook.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.yidu.ebook.entity.Product;
import org.yidu.ebook.util.JdbcUtil;
public class ProductDao {
/**
* @增加一件商品
* @author 贺显国
* @param product
* @return
*/
public int addProduct(Product product){
PreparedStatement pst=null;
Connection conn=null;
try {
//建立连接
conn=JdbcUtil.getConnection();
//获得SQL语句
String sql="insert into product" +
"(name,author,publish," +
"isbn,srcPrice,realPrice,quantity" +
"picName,content,categoryId)" +
"values(?,?,?,?,?,?,?,?,?,?)";
pst=conn.prepareStatement(sql);
//替换参数
pst.setString(1, product.getName());
pst.setString(2, product.getAuthor());
pst.setString(3, product.getPublish());
pst.setString(4, product.getIsbn());
pst.setDouble(5, product.getSrcPrice());
pst.setDouble(6, product.getRealPrice());
pst.setInt(7, product.getQuantity());
pst.setString(8, product.getPicName());
pst.setString(9, product.getContent());
pst.setString(10, product.getCategoryID());
int n=pst.executeUpdate();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 释放资源
JdbcUtil.closeAll(null, pst, conn);
}
return 0;
}
/**
* @author 贺显国
* @see 更改一件商品
* @param product
* @return
*/
public int UpdateProduct(Product product){
PreparedStatement pst=null;
Connection conn=null;
try {
//建立连接
conn=JdbcUtil.getConnection();
//获得SQL语句
String sql="update product " +
"set name=?,author=?,publish=?" +
"isbn=?,srcPrice=?,realPrice=?," +
"quantity=?,picName=?,content=?,category=?" +
"where id=?";
pst=conn.prepareStatement(sql);
//替换参数
pst.setString(1, product.getName());
pst.setString(2, product.getAuthor());
pst.setString(3, product.getPublish());
pst.setString(4, product.getIsbn());
pst.setDouble(5, product.getSrcPrice());
pst.setDouble(6, product.getRealPrice());
pst.setInt(7, product.getQuantity());
pst.setString(8, product.getPicName());
pst.setString(9, product.getContent());
pst.setString(10, product.getCategoryID());
pst.setInt(11,product.getId());
int n=pst.executeUpdate();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 释放资源
JdbcUtil.closeAll(null, pst, conn);
}
return 0;
}
/**
* @author 贺显国
* @see 查询全部商品
* @return
*/
public List<Product>queryProducts(){
List<Product> list = new ArrayList<Product>();
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try{
// 获得连接
conn = JdbcUtil.getConnection();
// 获得语句对象
String sql = "select * from product";
pst = conn.prepareStatement(sql);
// 执行SQL增删改的SQL
rs = pst.executeQuery();
// 接续查询结果
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
String author = rs.getString("author");
String publish = rs.getString("publish");
String isbn = rs.getString("isbn");
Double srcPrice = rs.getDouble("srcPrice");
Double realPrice= rs.getDouble("realPrice");
int quantity = rs.getInt("quantity");
String picName = rs.getString("picName");
String content = rs.getString("content");
String categoryId=rs.getString("categoryId");
Product product = new Product(id,name,author,
publish,isbn,
srcPrice,realPrice,quantity,
picName,content
,categoryId);
list.add(product);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 释放资源
JdbcUtil.closeAll(null, pst, conn);
}
return list;
}
/**
* @author 林天然
* @see 按id查询一件商品
* @param id
* @return
*/
public Product getProduct(int id){
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try{
// 获得连接
conn = JdbcUtil.getConnection();
// 获得语句对象
String sql = "select * from product where id=?";
// 替换参数
pst.setInt(1, id);
pst = conn.prepareStatement(sql);
// 执行SQL增删改的SQL
rs = pst.executeQuery();
//接续查询结果
if(rs.next()){
String name = rs.getString("name");
String author = rs.getString("author");
String publish = rs.getString("publish");
String isbn = rs.getString("isbn");
Double srcPrice = rs.getDouble("srcPrice");
Double realPrice= rs.getDouble("realPrice");
int quantity = rs.getInt("quantity");
String picName = rs.getString("picName");
String content = rs.getString("content");
String categoryId=rs.getString("categoryId");
Product product = new Product(id,name,author,publish,isbn,
srcPrice,realPrice,quantity,picName,content
,categoryId);
return product;
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 释放资源
JdbcUtil.closeAll(null, pst, conn);
}
return null;
}
/**
* @author 林天然
* @see 删除一件商品
* @param product
* @return
*/
public int deleteCustomer(Product product){
PreparedStatement pst = null;
Connection conn = null;
try{
// 获得连接
conn = JdbcUtil.getConnection();
// 获得语句对象
String sql = "delete from product where id=?";
pst = conn.prepareStatement(sql);
// 替换参数
pst.setInt(1, product.getId());
// 执行SQL增删改的SQL
int n = pst.executeUpdate();
// 返回结果
return n;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 释放资源
JdbcUtil.closeAll(null, pst, conn);
}
return 0;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
eBook图书网站,赛级高质量资源 (130个子文件)
buy.bmp 3KB
ProductDao.class 5KB
CustomerDao.class 5KB
CartDao.class 4KB
CategoryDao.class 4KB
Product.class 3KB
Customer.class 2KB
LoginServlet.class 2KB
JdbcUtil.class 2KB
Cart.class 1KB
Category.class 920B
.classpath 1KB
org.eclipse.wst.common.component 476B
org.eclipse.wst.jsdt.ui.superType.container 49B
style.css 7KB
success.gif 8KB
buy2.gif 6KB
buy3.gif 6KB
buy1.gif 6KB
orderbutton.gif 4KB
loginbg.gif 4KB
billboard.gif 4KB
hottitle.gif 4KB
productlist.gif 3KB
signup.gif 3KB
gif53_029.gif 3KB
finalbutton.gif 3KB
botton_gif_025.gif 2KB
logo.gif 2KB
buybutton.gif 2KB
logintitle.gif 2KB
bottomlogo.gif 2KB
infobg.gif 2KB
buy.gif 2KB
signupbutton.gif 1KB
gwc_jx.gif 1KB
gwc_buy.gif 1KB
car_step8.gif 1KB
menubg.gif 958B
check_use.gif 861B
serchbutton.gif 753B
cart.gif 671B
loginbutton.gif 457B
miniicon3.gif 410B
miniicon2.gif 326B
miniicon.gif 171B
loginline.gif 132B
product_list.html 16KB
search.html 10KB
info.html 9KB
modifyuserinfo.html 8KB
order.html 7KB
cart.html 7KB
register.html 7KB
orderfinal.html 6KB
orderlist.html 6KB
news.html 5KB
success.html 5KB
success2.html 5KB
my.html 4KB
registersuccess.html 3KB
Desk_top_.ini 9B
Desk_top_.ini 9B
Desk_top_.ini 9B
Desk_top_.ini 9B
sqljdbc4.jar 525KB
ProductDao.java 6KB
CustomerDao.java 6KB
CartDao.java 4KB
CategoryDao.java 4KB
LoginServlet.java 3KB
Product.java 2KB
Customer.java 2KB
JdbcUtil.java 1KB
Cart.java 750B
Category.java 640B
productlist.jpg 729KB
info.jpg 395KB
index.jpg 363KB
index_ad.jpg 154KB
myad.jpg 74KB
page_ad.jpg 59KB
bookcover.jpg 59KB
travelbook.jpg 47KB
java2.jpg 39KB
dayongxiaohua.jpg 39KB
subwayinshanghai.jpg 32KB
fish.jpg 31KB
tripcn.jpg 31KB
oneplant.jpg 29KB
euro.jpg 28KB
ngcn.jpg 25KB
photog.jpg 25KB
dulala.jpg 24KB
code.jpg 23KB
IconTexto_WebDev_009.jpg 22KB
think.jpg 20KB
102.jpg 8KB
104.jpg 6KB
103.jpg 5KB
共 130 条
- 1
- 2
资源评论
m0_74341333
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功