Beispiel #1
0
  @Override
  public void saveFeedback(Feedback feedback) throws ClassNotFoundException, SQLException {
    PreparedStatement ps = null;
    Connection con = null;

    try {
      con = openConnection();

      StringBuilder query =
          new StringBuilder("insert into ")
              .append(feedbackTableName)
              .append(" (")
              .append(feedbackUserIdAttribute)
              .append(", ")
              .append(feedbackDocumentIdAttribute)
              .append(", ")
              .append(feedbackLabelIdAttribute)
              .append(") values(?,?,?)");

      ps = con.prepareStatement(query.toString());
      ps.setInt(1, feedback.getUserId());
      ps.setInt(2, feedback.getDocument().hashCode());
      ps.setInt(3, feedback.getLabel().hashCode());

      ps.executeUpdate();

    } catch (ClassNotFoundException e) {
      throw e;
    } finally {
      closeStatement(ps);
      closeConnection(con);
    }
  }
Beispiel #2
0
  @Override
  public Set<Feedback> getAllFeedbacks() throws SQLException, ClassNotFoundException {
    PreparedStatement ps = null;
    ResultSet rs = null;
    Connection con = null;
    Feedback feedback = null;

    Set<Feedback> feedbacks = new HashSet<Feedback>();
    try {
      con = openConnection();

      StringBuilder query =
          new StringBuilder("select f.")
              .append(feedbackIdAttribute)
              .append(", f.")
              .append(feedbackUserIdAttribute)
              .append(", d.")
              .append(documentNameAttribute)
              .append(", l.")
              .append(labelNameAttribute)
              .append(",f.")
              .append(feedbackTimestampAttribute)
              .append(" from ")
              .append(documentTableName)
              .append(" as d,")
              .append(feedbackTableName)
              .append(" as f,")
              .append(labelTableName)
              .append(" as l where f.")
              .append(feedbackDocumentIdAttribute)
              .append(" = d.")
              .append(documentIdAttribute)
              .append(" and f.")
              .append(feedbackLabelIdAttribute)
              .append(" = l.")
              .append(labelIdAttribute);

      ps = con.prepareStatement(query.toString());
      rs = ps.executeQuery();

      while (rs.next()) {
        feedback = new Feedback();
        feedback.setId(rs.getInt(feedbackIdAttribute));
        feedback.setUserId(rs.getInt(feedbackUserIdAttribute));
        feedback.setDocument(rs.getString("d." + documentNameAttribute));
        feedback.setLabel(rs.getString("l." + labelNameAttribute));
        feedback.setDate(rs.getTimestamp(feedbackTimestampAttribute));
        feedbacks.add(feedback);
      }

    } catch (ClassNotFoundException e) {
      throw e;
    } finally {
      closeResult(rs);
      closeStatement(ps);
      closeConnection(con);
    }
    return feedbacks;
  }