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;
}
}

解决方案 »

  1.   

    addBatch(),executeBatch();是用来执行DML SQL 语句,不是SELECT,LZ弄错了吧
      

  2.   

    掌握两个函数(其实是两类函数)。首先用循环遍历得到的结果集,即while(result.next())。这个函数遍历结果集中的每一条记录。然后在循环体中用result.getString("字段名")(也可以是其他类的get函数,看你各个字段的类型是什么了)获取每条记录相应字段的值,通常是有相应的JavaBean。具体的楼主查查帮助文档应该能懂了
      

  3.   


    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();
    }
    }
    }