/**
   * Retrieves a List of Discussion Posts for a given Thread Id
   *
   * @param threadId The Id of the Thread to query
   * @return A List of Discussion Posts for the Thread
   */
  public List<DiscussionPost> getPosts(int threadId) {
    ArrayList<DiscussionPost> posts = new ArrayList<>();

    try {
      // Create a prepared statement
      PreparedStatement pstmt =
          conn.prepareStatement("SELECT * FROM DiscussionPosts WHERE ThreadId = ?");

      // Set the required parameters adn execute
      pstmt.setInt(1, threadId);
      ResultSet rs = pstmt.executeQuery();

      // Retrieve the results and add to the list
      if (rs.isBeforeFirst()) {
        while (!rs.isAfterLast()) {
          DiscussionPost post = DiscussionPost.fromResultSet(rs);

          if (post != null) posts.add(post);
        }
      }
    } catch (Exception e) {
      logger.log(Level.SEVERE, "SQL Error", e);
    }

    return posts;
  }
  /**
   * Retrieves all of the Discussion Threads associated with a Group Id
   *
   * @param groupId The Group Id to get Threads for
   * @return A List of Discussion Threads that belong to the Group
   */
  public List<DiscussionThread> getThreads(int groupId) {
    ArrayList<DiscussionThread> threads = new ArrayList<>();

    try {
      // Create a prepared statement
      PreparedStatement pstmt =
          conn.prepareStatement("SELECT * FROM DiscussionThreads WHERE GroupId = ?");

      // Set the required parameters and execute
      pstmt.setInt(1, groupId);
      ResultSet rs = pstmt.executeQuery();

      // Get the results and add to the list
      if (rs.isBeforeFirst()) {
        while (!rs.isAfterLast()) {
          DiscussionThread thread = DiscussionThread.fromResultSet(rs);
          if (thread != null) {
            threads.add(thread);
          }
        }
      }
    } catch (Exception e) {
      logger.log(Level.SEVERE, "SQL Error", e);
    }

    return threads;
  }
Esempio n. 3
0
  public void connect() {

    try {
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      Class.forName(driver);

      String dbase = "jdbc:odbc:db1";
      con = DriverManager.getConnection(dbase);
      st = con.createStatement();
      String sql = "select * from Table1";
      rs = st.executeQuery(sql);
      // displays everything in the database

      while (rs.isAfterLast()) {
        String fname = rs.getString("Fname");
        String lname = rs.getString("Lname");
        String book = rs.getString("Book");
        System.out.println(fname + " " + lname + " " + book);
      }

    } catch (Exception ex) {

    }
  }