/** * @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; }
/** * @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 }