package com.hk.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.hk.domain.Category;
import com.hk.domain.Product;
import com.hk.util.DBUtil;
public class ProductDao {
public List<Product> findProductByCid(Category category){
List<Product> products=new ArrayList<Product>();
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
conn=DBUtil.getConnection();
String sql="select * from product where pflag=0 and cid=? limit 12";
pstmt=conn.prepareStatement(sql);
//设置点位符的参数
pstmt.setString(1, category.getCid());
rs=pstmt.executeQuery();
while(rs.next()) {
String pid=rs.getString("pid");
String pname=rs.getString("pname");
double market_price=rs.getDouble("market_price");
double shop_price=rs.getDouble("shop_price");
String pimage=rs.getString("pimage");
String is_hot=rs.getString("is_hot");
String pdate=rs.getString("pdate");
String pdesc=rs.getString("pdesc");
int pflag=rs.getInt("pflag");
Product p=new Product(pid, pname, market_price, shop_price, pimage, pdate, is_hot, pdesc, pflag, category);
products.add(p);
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
DBUtil.close(rs, pstmt, conn);
}
return products;
}
/*@Test
public void testFindProduct() {
Category category=new Category();
category.setCid("1");
category.setCname("手机数码");
List<Product> products=findProductByCid(category);
System.out.println(products.toString());
}*/
public List<Product> findProductNew(){
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
List<Product> products=new ArrayList<Product>();
try {
conn=DBUtil.getConnection();
String sql="SELECT p.*,c.cname FROM product p,category c WHERE pflag=0 AND p.cid=c.cid ORDER BY pdate DESC LIMIT 9";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()) {
String pid=rs.getString("pid");
String pname=rs.getString("pname");
double market_price=rs.getDouble("market_price");
double shop_price=rs.getDouble("shop_price");
String pimage=rs.getString("pimage");
String pdate=rs.getString("pdate");
String is_hot=rs.getString("is_hot");
String pdesc=rs.getString("pdesc");
int pflag=rs.getInt("pflag");
String cid=rs.getString("cid");
String cname=rs.getString("cname");
Category category=new Category(cid,cname);
Product product= new Product(pid,pname,market_price,shop_price,pimage,pdate,is_hot,pdesc,pflag,category);
products.add(product);
}
}catch(Exception e) {
throw new RuntimeException(e);
}finally {
DBUtil.close(rs, pstmt, conn);
}
return products;
}
/*@Test
public void testFindNew() {
List<Product> products=findProductNew();
System.out.println(products);
}*/
//根据商品编号查询商品
public Product findProductByPid(String pid,String cid,String cname) {
Connection conn = null;
String sql = null;
PreparedStatement pstmt = null;
java.sql.ResultSet rs=null;
Product product=null;
try {
// 获得连接对象
conn = DBUtil.getConnection();
sql = "SELECT *\r\n" +
"FROM product WHERE pid=?";
// 创建命令执行
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,pid);
rs=pstmt.executeQuery();
if(rs.next()) {
String pid1=rs.getString("pid");
String pname=rs.getString("pname");
double market_price=rs.getDouble("market_price");
double shop_price=rs.getDouble("shop_price");
String pimage=rs.getString("pimage");
String pdate=rs.getString("pdate");
String is_hot=rs.getString("is_hot");
String pdesc=rs.getString("pdesc");
int pflag=rs.getInt("pflag");
Category category=new Category(cid,cname);
product=new Product(pid1, pname, market_price, shop_price, pimage, pdate, is_hot, pdesc, pflag, category);
}
}
// 释放资源
catch (Exception e) {
throw new RuntimeException(e);
} finally {
DBUtil.close(rs, pstmt, conn);
}
return product;
}
public Product findProductByPid(String pid) {
Connection conn = null;
String sql = null;
PreparedStatement pstmt = null;
java.sql.ResultSet rs=null;
Product product=null;
try {
// 获得连接对象
conn = DBUtil.getConnection();
sql = "SELECT * FROM product WHERE pid=?";
// 创建命令执行
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,pid);
rs=pstmt.executeQuery();
if(rs.next()) {
String pid1=rs.getString("pid");
String pname=rs.getString("pname");
double market_price=rs.getDouble("market_price");
double shop_price=rs.getDouble("shop_price");
String pimage=rs.getString("pimage");
String pdate=rs.getString("pdate");
String is_hot=rs.getString("is_hot");
String pdesc=rs.getString("pdesc");
int pflag=rs.getInt("pflag");
product=new Product(pid1, pname, market_price, shop_price, pimage, pdate, is_hot, pdesc, pflag);
}
}
// 释放资源
catch (Exception e) {
throw new RuntimeException(e);
} finally {
DBUtil.close(rs, pstmt, conn);
}
return product;
}
@Test
public void testFindNew() {
Product products=findProductByPid("1");
System.out.println(products);
}
}