1 packagecom.home.utils;2
3 importjava.io.IOException;4 importjava.sql.Connection;5 importjava.sql.DriverManager;6 importjava.sql.ResultSet;7 importjava.sql.SQLException;8 importjava.sql.Statement;9 importjava.util.ArrayList;10 importjava.util.List;11
12 importorg.apache.lucene.analysis.Analyzer;13 importorg.apache.lucene.analysis.standard.StandardAnalyzer;14 importorg.apache.lucene.document.Document;15 importorg.apache.lucene.document.Field;16 importorg.apache.lucene.document.Field.Store;17 importorg.apache.lucene.index.IndexWriter;18 importorg.apache.lucene.index.IndexWriter.MaxFieldLength;19 importorg.apache.lucene.queryParser.QueryParser;20 importorg.apache.lucene.search.IndexSearcher;21 importorg.apache.lucene.search.Query;22 importorg.apache.lucene.search.ScoreDoc;23 importorg.apache.lucene.search.TopDocs;24 importorg.apache.lucene.util.Version;25
26 public classTestLucene {27
28 static{29 try{30 //加载MySql的驱动类
31 Class.forName("com.mysql.jdbc.Driver");32 } catch(ClassNotFoundException e) {33 System.out.println("找不到驱动程序类 ,加载驱动失败!");34 e.printStackTrace();35 }36 }37
38 public staticConnection createConnection() {39
40 String url = "jdbc:mysql://localhost:3306/cool_files";41 String username = "root";42 String password = "123456";43 Connection con = null;44 try{45 con =DriverManager.getConnection(url, username, password);46 } catch(SQLException se) {47 System.out.println("数据库连接失败!");48 se.printStackTrace();49 }50
51 returncon;52 }53
54 public staticResultSet getResult(String sql, Connection conn) {55 try{56 Statement stmt =conn.createStatement();57 ResultSet rs =stmt.executeQuery(sql);58 returnrs;59 } catch(SQLException e) {60 System.out.println(e);61 }62 return null;63 }64
65 public voidclose(ResultSet rs, Statement stmt, Connection conn) {66
67 if (rs != null) { //关闭记录集
68 try{69 rs.close();70 } catch(SQLException e) {71 e.printStackTrace();72 }73 }74 if (stmt != null) { //关闭声明
75 try{76 stmt.close();77 } catch(SQLException e) {78 e.printStackTrace();79 }80 }81 if (conn != null) { //关闭连接对象
82 try{83 conn.close();84 } catch(SQLException e) {85 e.printStackTrace();86 }87 }88 }89
90 public static voidIndex(ResultSet rs) {91 try{92 IndexWriter indexWriter = newIndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);93 while(rs.next()) {94 Document doc = newDocument();95 doc.add(new Field("Au_id", rs.getString(1), Store.YES,96 org.apache.lucene.document.Field.Index.ANALYZED));97 doc.add(new Field("Au_name", rs.getString(2), Store.YES,98 org.apache.lucene.document.Field.Index.ANALYZED));99 doc.add(new Field("Phone", rs.getString(3), Store.YES,100 org.apache.lucene.document.Field.Index.ANALYZED));101 doc.add(new Field("Address", rs.getString(4), Store.YES,102 org.apache.lucene.document.Field.Index.ANALYZED));103 doc.add(new Field("City", rs.getString(5), Store.YES,104 org.apache.lucene.document.Field.Index.ANALYZED));105 doc.add(new Field("State", rs.getString(6), Store.YES,106 org.apache.lucene.document.Field.Index.ANALYZED));107 doc.add(new Field("Zip", rs.getString(7), Store.YES,108 org.apache.lucene.document.Field.Index.ANALYZED));109 doc.add(new Field("contract", rs.getString(8), Store.YES,110 org.apache.lucene.document.Field.Index.ANALYZED));111 indexWriter.addDocument(doc);112 }113 indexWriter.optimize();114 indexWriter.close();115 } catch(IOException e) {116 System.out.println(e);117 } catch(SQLException e) {118 System.out.println(e);119 }120 }121
122 public staticAnalyzer getAnalyzer() {123 return newStandardAnalyzer(Version.LUCENE_30);124 }125
126 public static Listseacher(String queryString) {127 List authorsList = null;128 try{129 IndexSearcher is = newIndexSearcher(LuceneUtils.directory);130 QueryParser parser = new QueryParser(Version.LUCENE_30, "Au_name",131 LuceneUtils.analyzer);132 Query query =parser.parse(queryString);133 TopDocs docs = is.search(query, 10);134 ScoreDoc[] scoreDocs =docs.scoreDocs;135 authorsList = new ArrayList();136
137 for(ScoreDoc scoreDoc : scoreDocs) {138 int num =scoreDoc.doc;139 Document document =is.doc(num);140 Authors article =DocumentUtils.document2Authors(document);141 authorsList.add(article);142 }143
144 } catch(Exception e) {145 System.out.print(e);146 }147 returnauthorsList;148 }149 }