コード例 #1
0
  /**
   * Creates a Discussion Thread in the database
   *
   * @param discussion The Discussion to insert
   */
  public void createDiscussion(DiscussionThread discussion) {
    try {
      // Create a prepared statement
      PreparedStatement pstmt =
          conn.prepareStatement(
              "INSERT INTO DiscussionThreads (GroupId, ThreadName)" + "VALUES (?, ?)",
              Statement.RETURN_GENERATED_KEYS);

      // Set the required parameters and execute
      pstmt.setInt(1, discussion.getGroupId());
      pstmt.setString(2, discussion.getThreadName());
      pstmt.executeUpdate();

      // Get the generated id
      ResultSet rs = pstmt.getGeneratedKeys();
      if (rs.next()) discussion.setId(rs.getInt(1));
    } catch (Exception e) {
      logger.log(Level.SEVERE, "SQL Error", e);
    }
  }
コード例 #2
0
  /**
   * Creates a Discussion Post in the database
   *
   * @param post The Discussion Post to insert
   */
  public void createPost(DiscussionPost post) {
    try {
      // Create a prepared statement
      PreparedStatement pstmt =
          conn.prepareStatement(
              "INSERT INTO DiscussionPosts (ThreadId, UserId, Message)" + "VALUES (?, ?, ?)",
              Statement.RETURN_GENERATED_KEYS);

      // Set the required parameters and execute
      pstmt.setInt(1, post.getThreadId());
      pstmt.setInt(2, post.getUserId());
      pstmt.setString(3, post.getMessage());
      pstmt.executeUpdate();

      // get the generated id
      ResultSet rs = pstmt.getGeneratedKeys();
      if (rs.next()) post.setId(rs.getInt(1));
    } catch (Exception e) {
      logger.log(Level.SEVERE, "SQL Error", e);
    }
  }