📄 indexer.java
字号:
import java.util.*;
import java.io.*;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
public class Indexer implements Runnable
{
public static boolean stopFlag=true;
String dirName="c:/search/repository";
File file=new File(dirName);
String fList[]=file.list();
FileReader indCountRead;
StreamTokenizer countTok;
int fileNum;
public static int filePointerb4;
public static int filePointerafter;
// public static int notimes;
public void run()
{
try{
FileReader indCountRead=new FileReader("c:/search/resources/indexcount.txt");
StreamTokenizer countTok=new StreamTokenizer(indCountRead);
countTok.resetSyntax();
countTok.wordChars(33,65535);
countTok.whitespaceChars(0,' ');
countTok.eolIsSignificant(false);
countTok.nextToken();
fileNum=Integer.parseInt(countTok.sval);
Table.notimes=filePointerafter - filePointerb4;
indCountRead.close();
int functioncount=0;
while(stopFlag && fileNum <= fList.length)
{
String s="c:/search/repository/doc"+fileNum+".txt";
FileReader fr;
StreamTokenizer tok;
Table tab;
try
{
fr=new FileReader(s);
tok=new StreamTokenizer(fr);
tok.resetSyntax();
tok.wordChars(33,65535);
tok.whitespaceChars(0,' ');
tok.eolIsSignificant(false);
FrameMain.indFrame.jTextFieldFile.setText("File:"+s);
tab=new Table();
tok.nextToken();
String s1=tok.sval;
while(tok.nextToken()!=tok.TT_EOF)
{
FrameMain.indFrame.jTextFieldToken.setText("Token:"+tok.toString());
if(tab.avoidToken(tok.sval)==false)
{
if(functioncount == 0)
{
if(tab.matchedRecord1(tok.sval,s1)==false)
{
tab.insertRecord1(tok.sval,s1);
}
else tab.updateRecord1(tok.sval,s1);
}
if(functioncount == 1)
{
if(tab.matchedRecord2(tok.sval,s1)==false)
{
tab.insertRecord2(tok.sval,s1);
}
else tab.updateRecord2(tok.sval,s1);
}
if(functioncount == 2)
{
if(tab.matchedRecord3(tok.sval,s1)==false)
{
tab.insertRecord3(tok.sval,s1);
}
else tab.updateRecord3(tok.sval,s1);
}
if(functioncount >= 3)
{
if(tab.matchedRecord4(tok.sval,s1)==false)
{
tab.insertRecord4(tok.sval,s1);
}
else tab.updateRecord4(tok.sval,s1);
}
}
}//while end
}//end of try
catch(IOException e)
{
System.out.println("File Error1"+e.getMessage());
FrameMain.indFrame.jTextFieldFile.setText("ERROR:"+e.getMessage());
}
fileNum++;
functioncount++;
}//end outer while
IndexerFrame.jTextFieldFile.setText("");
FrameMain.indFrame.jTextFieldToken.setText("");
FrameMain.indFrame.jButtonStop.setEnabled(false);
FrameMain.indFrame.jButtonRun.setEnabled(false);
FrameMain.indFrame.jLabelWarning.setText("");
FileWriter indCountWrite=new FileWriter("c:/search/resources/indexcount.txt",false);
Integer count=new Integer(fileNum);
indCountWrite.write(count.toString(),0,count.toString().length());
indCountWrite.close();
if (fileNum>fList.length)
IndexerFrame.jLabelPop.setText("ALL FILES INDEXED !! ");
stopFlag=true;
}//end of outer try
catch(Exception e)
{
FrameMain.indFrame.jTextFieldFile.setText("ERROR:"+e.getMessage());
}
}//end of run
}//end of indexer
class Table
{
String connectionAddress=
"jdbc:odbc:search";
Connection con;
String connectionAddress1=
"jdbc:odbc:search1";
Connection con1;
String connectionAddress2=
"jdbc:odbc:search2";
Connection con2;
String connectionAddress3=
"jdbc:odbc:search3";
Connection con3;
public static int notimes;
Statement stmt;
ResultSet rs;
public boolean avoidToken(String token)
{
if(token.equalsIgnoreCase("is")||
token.equalsIgnoreCase("are")||
token.equalsIgnoreCase("am")||
token.equalsIgnoreCase("was")||
token.equalsIgnoreCase("were")||
token.equalsIgnoreCase("have")||
token.equalsIgnoreCase("has")||
token.equalsIgnoreCase("had")||
token.equalsIgnoreCase("may")||
token.equalsIgnoreCase("might")||
token.equalsIgnoreCase("must")||
token.equalsIgnoreCase("shall")||
token.equalsIgnoreCase("will")||
token.equalsIgnoreCase("would")||
token.equalsIgnoreCase("should")||
token.equalsIgnoreCase("can")||
token.equalsIgnoreCase("could")||
token.equalsIgnoreCase("ought")||
token.equalsIgnoreCase("to")||
token.equalsIgnoreCase("do")||
token.equalsIgnoreCase("did")||
token.equalsIgnoreCase("does")||
token.equalsIgnoreCase("a")||
token.equalsIgnoreCase("an")||
token.equalsIgnoreCase("the")||
token.equalsIgnoreCase("in")||
token.equalsIgnoreCase("of")||
token.equalsIgnoreCase("at")||
token.equalsIgnoreCase("as")||
token.equalsIgnoreCase("into")||
token.equalsIgnoreCase("for")||
token.equalsIgnoreCase("from")||
token.equalsIgnoreCase("while")||
token.equalsIgnoreCase("if")||
token.equalsIgnoreCase("then")||
token.equalsIgnoreCase("."))
return true;
else
return false;
}
public void insertRecord1(String key,String fileAddress)
{
String insertString;
insertString="insert into INDEXTABLE"+
" values('"+key.toLowerCase()+"','"+fileAddress+"',1)";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//myDriver.ClassName");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try
{
con=DriverManager.getConnection(connectionAddress,"",
"");
stmt=con.createStatement();
stmt.executeUpdate(insertString);
stmt.close();
con.close();
}
catch(SQLException ex)
{
System.err.println("SQLException2: "+ex.getMessage());
}
}
public void insertRecord2(String key,String fileAddress)
{
String insertString;
insertString="insert into INDEXTABLE"+
" values('"+key.toLowerCase()+"','"+fileAddress+"',1)";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//myDriver.ClassName");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try
{
con1=DriverManager.getConnection(connectionAddress1,"",
"");
stmt=con1.createStatement();
stmt.executeUpdate(insertString);
stmt.close();
con1.close();
}
catch(SQLException ex)
{
System.err.println("SQLException2: "+ex.getMessage());
}
}
public void insertRecord3(String key,String fileAddress)
{
String insertString;
insertString="insert into INDEXTABLE"+
" values('"+key.toLowerCase()+"','"+fileAddress+"',1)";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//myDriver.ClassName");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try
{
con2=DriverManager.getConnection(connectionAddress2,"",
"");
stmt=con2.createStatement();
stmt.executeUpdate(insertString);
stmt.close();
con2.close();
}
catch(SQLException ex)
{
System.err.println("SQLException2: "+ex.getMessage());
}
}
public void insertRecord4(String key,String fileAddress)
{
String insertString;
insertString="insert into INDEXTABLE"+
" values('"+key.toLowerCase()+"','"+fileAddress+"',1)";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//myDriver.ClassName");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try
{
con3=DriverManager.getConnection(connectionAddress3,"",
"");
stmt=con3.createStatement();
stmt.executeUpdate(insertString);
stmt.close();
con3.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -