public class ProductDAOImpl implements ProductDAO {
private static final String FIND_BY_IDS = "select * from d_product where id=?";
public List<Product> findById(List<Integer> ids) throws Exception {
List<Product> pros = null;
Connection conn = DBUtil.openConnection();
PreparedStatement prep = conn.prepareStatement(FIND_BY_IDS);
for(int i=0;i<ids.size();i++){//批处理
prep.setInt(1, ids.get(i));
prep.addBatch();
} /**int[] rs请问怎么写*/ = prep.executeBatch();
while(/**请问怎么写*/) {
Product pro = new Product();
pro.setDangPrice(rs.getDouble("dang_price"));
pro.setFixedPrice(rs.getDouble("fixed_price"));
pro.setProductName(rs.getString("product_name"));
pro.setProductPic(rs.getString("product_pic"));
pro.setId(rs.getInt(id));
}
return pros;
}
}
解决方案 »
- 菜鸟发问:The constructor Printwriter(String) is undefined
- ThreadPoolExecutor线程池
- 关于简单倒计时处理问题,求大虾帮忙
- String str = new String("hello");内存执行小分析
- 请教基本语法this.class.getClassLoader()怎么理解?
- jsp连接sql server 2000数据库问题
- sos!!!sos!!!sos!!!sos!!!
- 谁知道在applet的窗口中,从数据库读出多条图片和文字数据,怎样在窗口中显示,用label还是什么,给点通常的方法!
- 网络游戏与图片
- 光看书没什么用,所以偶想用JAVA做个什么东东的,请高手指点
- 求教
- 请大家指导一下,看看这个是考的什么
package org.eric.dao.impl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;import org.eric.dao.ProductDAO;
import org.eric.entity.Book;
import org.eric.entity.Product;
import org.eric.service.CartItem;
import org.eric.util.DBUtil;
public class ProductDAOImpl implements ProductDAO {
private static final String FIND_NEW_PRODUCT = "select * from d_product where has_deleted=0 " +
"order by add_time desc limit ?";
private static final String FIND_BY_CHIRLD_ID = "select * from d_product dp join d_book db " +
"on (dp.id = db.id) join d_category_product dcp " +
"on (dcp.product_id=dp.id) where dcp.cat_id=?";
private static final String FIND_BY_IDS = "select * from d_product where id=?"; public List<Product> findNewProduct(int size) throws Exception {
List<Product> pros = new ArrayList<Product>();
Connection conn = DBUtil.openConnection();
PreparedStatement prep = conn.prepareStatement(FIND_NEW_PRODUCT);
prep.setInt(1, size);
ResultSet rs = prep.executeQuery();
while(rs.next()) {
Product pro = new Product();
pro.setProductName(rs.getString("product_name"));
pro.setFixedPrice(rs.getDouble("fixed_price"));
pro.setDangPrice(rs.getDouble("dang_price"));
pro.setProductPic(rs.getString("product_pic"));
pros.add(pro);
}
DBUtil.close();
return pros;
} @Override
public List<Book> findBookByChirldId(int cid) throws Exception {
List<Book> books = new ArrayList<Book>();
Connection conn = DBUtil.openConnection();
PreparedStatement prep = conn.prepareStatement(FIND_BY_CHIRLD_ID);
prep.setInt(1, cid);
ResultSet rs = prep.executeQuery();
while(rs.next()) {
Book b = new Book();
b.setId(rs.getInt("id"));
b.setProductName(rs.getString("product_name"));
b.setFixedPrice(rs.getDouble("fixed_price"));
b.setDangPrice(rs.getDouble("dang_price"));
b.setProductPic(rs.getString("product_pic"));
b.setAuthor(rs.getString("author"));
b.setPublsihTime(rs.getLong("publish_time"));
b.setCatalogue(rs.getString("catalogue"));
b.setPublishing(rs.getString("publishing"));
books.add(b);
}
DBUtil.close();
return books;
} public List<CartItem> findById(List<Integer> ids) throws Exception {
List<CartItem> items = new ArrayList<CartItem>();
Connection conn = DBUtil.openConnection();
PreparedStatement prep = conn.prepareStatement(FIND_BY_IDS);
Product pro = null ;
CartItem item = null;//购物车里的商品
for(int i=0;i<ids.size();i++){//批处理
prep.setInt(1, ids.get(i));
ResultSet rs = prep.executeQuery();
if(rs.next()) {
pro = new Product();
item = new CartItem();
pro.setDangPrice(rs.getDouble("dang_price"));
pro.setFixedPrice(rs.getDouble("fixed_price"));
pro.setProductName(rs.getString("product_name"));
pro.setProductPic(rs.getString("product_pic"));
pro.setId(rs.getInt("id"));
item.setPro(pro);
item.setDelete(false);
item.setQty(1);
items.add(item);
}
}
return items;
}
public static void main(String[] args) {
ProductDAO dao = new ProductDAOImpl();
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
try {
List<CartItem> its = dao.findById(ids);
System.out.println(its.get(0).getPro().getProductName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}