예제 #1
0
  /**
   * Returns the article data for the given identity.
   *
   * @param articleIdentity the article identity
   * @throws IllegalStateException if there is no article with the given identity
   * @throws SQLException if there is a problem with the underlying JDBC connection
   */
  public Article queryArticle(final long articleIdentity) throws SQLException {
    final Article article = new Article();

    synchronized (this.connection) {
      try (PreparedStatement statement = this.connection.prepareStatement(SQL_SELECT_ARTICLE)) {
        statement.setLong(1, articleIdentity);
        try (ResultSet resultSet = statement.executeQuery()) {
          if (!resultSet.next()) throw new IllegalStateException("article doesn't exist.");
          article.setIdentity(resultSet.getLong("identity"));
          article.setDescription(resultSet.getString("description"));
          article.setCount(resultSet.getInt("count"));
          article.setPrice(resultSet.getLong("price"));
        }
      }
    }

    return article;
  }
예제 #2
0
  /**
   * Returns all article data.
   *
   * @throws IllegalStateException if the login data is invalid
   * @throws SQLException if there is a problem with the underlying JDBC connection
   */
  public SortedSet<Article> queryArticles() throws SQLException {
    final SortedSet<Article> articles = new TreeSet<Article>();

    synchronized (this.connection) {
      try (PreparedStatement statement = this.connection.prepareStatement(SQL_SELECT_ARTICLES)) {
        try (ResultSet resultSet = statement.executeQuery()) {
          while (resultSet.next()) {
            final Article article = new Article();
            article.setIdentity(resultSet.getLong("identity"));
            article.setDescription(resultSet.getString("description"));
            article.setCount(resultSet.getInt("count"));
            article.setPrice(resultSet.getLong("price"));
            articles.add(article);
          }
        }
      }
    }

    return articles;
  }