package NEWS; import java.sql.*; import java.lang.*; import java.text.*; import java.util.*; import java.io.*; import java.util.regex.*; import DBstep.iDBManager2000;//数据库操作的bean
public class newsSearch { private String filePath=null;//主体新闻存放的目录 private String keyWord=null;//查询关键字 private Vector news = new Vector();//存放符合条件的结果 public newsSearch() { } public void setFilePath(String s) { this.filePath=s; }
public void setKeyWord(String s) { this.keyWord=s; }
public Vector getResult() { return news; }
public void search() { //打开数据库 ResultSet result=null; String mSql=null; PreparedStatement prestmt=null; DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000(); DbaObj.OpenConnection();
try { //检索所有的新闻 mSql="select * from t_news_detail order by release_time desc"; result=DbaObj.ExecuteQuery(mSql); while(result.next()) { String id=result.getString("id"); String title=result.getString("title"); String release_time=result.getString("release_time"); String news_type=result.getString("type"); String content=result.getString("content"); String man_add=result.getString("man_add"); //按行读文件 String trace=filePath+content+".html"; FileReader myFileReader=new FileReader(trace); BufferedReader myBufferedReader=new BufferedReader(myFileReader); String myString=null; String resultString=new String(); while((myString=myBufferedReader.readLine())!=null) { resultString=resultString+myString; } //去掉多余字符
HtmlEncode.HtmlEncode Html=new HtmlEncode.HtmlEncode(); /*这个bean去掉多余的字符,新闻是自己生成的文件,可以尽量多的删除多余字符*/ resultString=Html.TextEncode(resultString); myFileReader.close(); //取出查询关键字 Pattern p=null; Matcher m=null; p = Pattern.compile("\\+"); String[] a=p.split(keyWord);//把关键字用+分开 //全文检索 String searchResult="1";//检索结果 int i; for(i=0;i<a.length;i++)//逐个按关键字查找,如果所有的关键字都符合,则记录结果 { p = Pattern.compile(a[i].toString()); m = p.matcher(resultString); if (!(m.find())) { searchResult="0"; } }
//记录符合条件的新闻 if(searchResult.equals("1")) { News resultNews=new News();//存放结果的类,和数据库的结构基本一致 resultNews.content=content; resultNews.release_time=release_time; resultNews.type=news_type; resultNews.man_add=man_add; resultNews.title=title; news.addElement(resultNews);//最后的结果集,要返回客户端 } }
//关闭数据库 DbaObj.CloseConnection() ; }catch(Exception e){ System.out.println(e.toString()); } }
public class News { //存放结果的类 String content; String release_time; String type; String man_add; String title; public String getContent() { return this.content; } public String getTitle() { return this.title; } public String getTime() { return this.release_time; } public String getType() { return this.type; } public String getMan_add() { return this.man_add; } } }
|