package com.DAO;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import com.utilty.DBHelper;
import entity.Items;
public class ItemsDao {
public ArrayList<Items> getAllItems() {
PreparedStatement ptmt = null;
ResultSet rs = null;
Connection conn = null;
ArrayList<Items> itemList = new ArrayList<Items>();// 商品集合
try {
conn = DBHelper.getConnection();
String sql = "select * from items;";// sql语句
ptmt = conn.prepareStatement(sql);
rs = ptmt.executeQuery();
while (rs.next()) {
Items item = new Items();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setCity(rs.getString("city"));
item.setPrice(rs.getInt("price"));
item.setNumber(rs.getInt("number"));
item.setPicture(rs.getString("picture"));
itemList.add(item);// 把一个商品加入到集合中
}
return itemList;
}
catch (Exception ex) {
ex.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ptmt != null) {
try {
ptmt.close();
ptmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 返回商品集合
}
}