Example #1
0
  public Items getItemsById(int id) {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try {
      conn = DBHelper.getConnection();
      String sql = "select * from items where id =?;";
      stmt = conn.prepareStatement(sql);
      stmt.setInt(1, id);
      rs = stmt.executeQuery();
      while (rs.next()) {
        Items item = new Items();
        item.setId(rs.getInt("id"));
        item.setName(rs.getString("name"));
        item.setCity(rs.getString("city"));
        item.setNumber(rs.getInt("number"));
        item.setPrice(rs.getInt("price"));
        item.setPicture(rs.getString("picture"));
        return item;
      }
    } catch (SQLException ex) {
      ex.printStackTrace();
    } finally {
      try {
        stmt.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
      try {
        rs.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
      rs = null;
      stmt = null;
    }

    return null;
  }
Example #2
0
  // 获得所有的商品信息
  public ArrayList<Items> getAllItems() {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    ArrayList<Items> list = new ArrayList<Items>();

    try {
      conn = DBHelper.getConnection();
      String sql = "select * from items";
      stmt = conn.prepareStatement(sql);
      rs = stmt.executeQuery();
      while (rs.next()) {
        Items item = new Items();
        item.setId(rs.getInt("id"));
        item.setName(rs.getString("name"));
        item.setCity(rs.getString("city"));
        item.setNumber(rs.getInt("number"));
        item.setPrice(rs.getInt("price"));
        item.setPicture(rs.getString("picture"));
        list.add(item);
      }
    } catch (SQLException ex) {
      ex.printStackTrace();
    } finally {
      try {
        stmt.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
      try {
        rs.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
      rs = null;
      stmt = null;
    }

    return list;
  }