コード例 #1
0
  /**
   * @param String keywords
   * @return Term term 根据查询关键词查找相关的查询扩展信息
   *     返回形式:term:{keyWords:"",eventId:{1,2,3,……},newsId:{1,2,3,……},expansionTerm:{"","","",……}}
   */
  public Expansion getExpansionByKeywords(String keywords) {
    Expansion expansion = new Expansion();
    init();
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      conn = dataSource.getConnection();
      pstmt = conn.prepareStatement(SQL_SELECT_EXPANSION_BY_KEYWORDS);
      pstmt.setString(1, keywords);
      if (pstmt.execute()) {
        rs = pstmt.getResultSet();
        if (rs.next()) {
          expansion.setTerm(keywords);
          if (rs.getString("eventsId") != null) {
            String eventsIdString[] = rs.getString("eventsId").split(",");
            int[] eventsId = new int[eventsIdString.length];
            for (int i = 0; i < eventsIdString.length; i++) {
              eventsId[i] = Integer.parseInt(eventsIdString[i]);
              expansion.addEventId(eventsId[i]);
            } // for
          }

          // 获得所有的news
          String newsIdString[] = rs.getString("newsId").split(",");
          int[] newsId = new int[newsIdString.length];
          for (int i = 0; i < newsIdString.length; i++) {
            newsId[i] = Integer.parseInt(newsIdString[i]);
            expansion.addNewsId(newsId[i]);
          } // for
          // 获得所有的扩展词
          String[] expansionTerms = rs.getString("expansionTerms").split(",");
          for (String expansionTerm : expansionTerms) {
            expansion.addExpansionTerms(expansionTerm);
          }

        } // if
        else {
          expansion = null;
        } // 如果数据库中不存在该扩展,则返回空
      } // try

    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {
        if (rs != null) rs.close();
        if (pstmt != null) pstmt.close();
        if (conn != null) conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    return expansion;
  }
コード例 #2
0
  /** @param Term 将单个的扩展存入数据库 */
  public void insertExpansion(Expansion expansion) {
    init();
    if (expansion == null) return;
    // 如果数据库中已存在,则只进行更新操作
    if (getExpansionByKeywords(expansion.getTerm()) != null) updateExpansion(expansion);
    // 若不存在,则进行插入操作
    else {
      Connection conn = null;
      PreparedStatement pstmt = null;
      ResultSet rs = null;
      try {
        conn = dataSource.getConnection();
        pstmt = conn.prepareStatement(SQL_INSERT_EXPANSION, Statement.RETURN_GENERATED_KEYS);
        pstmt.setString(1, expansion.getTerm());
        /*将与查询词相关的事件id以字符串的形式存储,中间用‘,’隔开,以防止过度冗余,存储形式如"1,2,3",说明与该词相关的事件是事件1、事件2和事件3,
         * 并且对于事件只存id号,如果需要其他详细信息,可以根据id号进行进一步的查询 */
        String EventsIdString = intArrayToString(expansion.getEventId());
        pstmt.setString(2, EventsIdString);

        /*对于新闻的存储方法同事件的存储方式*/
        String NewsIdString = intArrayToString(expansion.getNewsId());
        pstmt.setString(3, NewsIdString);

        String ExpansionTermString = stringArrayToString(expansion.getExpansionTerms());
        pstmt.setString(4, ExpansionTermString);

        pstmt.executeUpdate();

      } catch (SQLException e) {
        e.printStackTrace();
      } finally {
        try {
          if (rs != null) rs.close();
          if (pstmt != null) pstmt.close();
          if (conn != null) conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
      } // finally
    } // else
  }
コード例 #3
0
  /**
   * @param Term term
   *     此方法实现对数据库原有的扩展进行更新,即如果数据库中已经存在关于某个查询关键词的扩展,并且要插入的expansion的关键词与该词相同,则将插入操作改为更新操作
   */
  public void updateExpansion(Expansion expansion) {
    init();
    Expansion existExpansion = getExpansionByKeywords(expansion.getTerm());
    // 将数据库中已有的扩展和该扩展合并
    if (existExpansion.getEventId() == null || existExpansion.getEventId().isEmpty()) {
      existExpansion.addEventId(expansion.getEventId());
    } else {
      for (int eid : expansion.getEventId()) {
        if (!existExpansion.getEventId().contains(eid)) existExpansion.addEventId(eid);
      }
    }

    for (int nid : expansion.getNewsId()) {
      if (!existExpansion.getNewsId().contains(nid)) existExpansion.addNewsId(nid);
    }
    for (String expansionTerm : expansion.getExpansionTerms()) {
      if (!existExpansion.getExpansionTerms().contains(expansionTerm))
        existExpansion.addExpansionTerms(expansionTerm);
    }
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      conn = dataSource.getConnection();
      pstmt = conn.prepareStatement(SQL_UPDATE_EXPANSION);
      pstmt.setString(1, intArrayToString(existExpansion.getEventId()));
      pstmt.setString(2, intArrayToString(existExpansion.getNewsId()));
      pstmt.setString(3, stringArrayToString(existExpansion.getExpansionTerms()));
      pstmt.setString(4, existExpansion.getTerm());
      pstmt.executeUpdate();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {
        if (rs != null) rs.close();
        if (pstmt != null) pstmt.close();
        if (conn != null) conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    } // finally
  }