Ejemplo n.º 1
0
  /**
   * FindById Searches the database for a Group object by the object id.
   *
   * @param connection The jdbc connection
   * @param id The id to select by
   */
  public static Post FindByID(Long post_ID) throws SQLException {
    Post post = null;
    PreparedStatement findPostId = null;
    Connection conn = null;

    String sql = String.format("SELECT * from %s where postid = ?", Post.dquote(POST), post_ID);

    if (post_ID != null) {
      try {
        conn = DB.getConnection();
        findPostId = conn.prepareStatement(sql);
        findPostId.setLong(1, post_ID);

        ResultSet rs = findPostId.executeQuery();

        if (rs.next()) {
          post =
              new Post(
                  rs.getString(Post.CONTENT),
                  rs.getString(Post.TYPE),
                  rs.getTimestamp(Post.TIMESTAMP),
                  rs.getLong(Post.USER_ID),
                  rs.getLong(Post.WALL_ID),
                  rs.getLong(Post.POST_ID));
        }

        findPostId.close();
        conn.close();
        return post;
      } catch (SQLException e) {
        Logger.debug("Error retrieving post.", e);
      }
    }
    return post;
  }
Ejemplo n.º 2
0
  /** Persist Saves the post to the database, this function will create a new database connection */
  public void Persist() {

    PreparedStatement pstmt = null;
    Connection conn = null;
    try {
      conn = DB.getConnection();
      String sql = "UPDATE " + User.dquote(Post.POST);
      sql += " set " + Post.CONTENT + " = ?, ";
      sql += Post.TYPE + " = ?, ";
      sql += Post.TIMESTAMP + " = ?, ";
      sql += Post.USER_ID + " = ?, ";
      sql += Post.WALL_ID + " = ?, ";
      sql += "where " + Post.POST_ID + " = ?";

      Logger.debug("Generated update: [%s]", sql);
      pstmt = conn.prepareStatement(sql);
      pstmt.setString(1, this.content);
      pstmt.setString(2, this.type);
      pstmt.setTimestamp(3, this.time_stamp);
      pstmt.setLong(4, this.user_ID);
      pstmt.setLong(5, this.wall_ID);
      pstmt.setLong(6, this.post_ID);

      pstmt.executeUpdate();

      pstmt.close();
      conn.close();
    } catch (SQLException e) {
      // Attempt to close the connection
      Logger.debug("Error while persisting Post");
      if (conn != null) {
        try {
          conn.close();
        } catch (Exception x) {
          Logger.debug("Error while closing connection during exception", x);
        }
      }
    }
  }