I am building a java desktop application that uses lucene 3.6.2 search engine to search for data in a derby database java swing JTable and netbeans A user enters a search query into the testfield and the search result is displayed in a Jtable and the defaulttablemodel.
I have tried my hands on tutorials about displaying database information in a jTable.
But this time I want to have a search field and a search button. a string is entered into the search field after finding the matches , then the result is displayed in the JTable
below are code snippets
public class GNSSJFrame extends javax.swing.JFrame {
String searchField;
DBSearch dbs = new DBSearch();
Vector<String> header = new Vector<>();;
Vector<Vector<String>> tabledata = new Vector<>();;
TableData td = new TableData();
public GNSSJFrame() {
initComponents();
}
searchResultTable.setModel(new javax.swing.table.DefaultTableModel(
tabledata, header
));
searchResultTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
jScrollPane1.setViewportView(searchResultTable);
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel2.setText(" SEARCH RESULTS");
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
searchField = searchTextField.getText();
if(!searchField.isEmpty())
{
tabledata = dbs.searchDatabase(searchField + "*");
td.fillHeader();
header = td.getHeader();
}
} catch (CorruptIndexException ex) {
Logger.getLogger(GNSSJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (LockObtainFailedException ex) {
Logger.getLogger(GNSSJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException | ParseException ex) {
Logger.getLogger(GNSSJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void searchTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
Code for the lucene search engine
public class DBSearch {
//Field Declarations
Version matchVersion = Version.LUCENE_36;
String host = "jdbc:derby://localhost:1527/NSS_DB";
String uName = "staff";
String uPass = "password";
String qString;
Statement stmt;
ResultSet rs;
Connection con;
Document doc;
Vector<Vector<String>> docVector;
Document d;
public DBSearch()
{}
public Vector<Vector<String>> searchDatabase( String qs) throws CorruptIndexException, LockObtainFailedException, IOException, ParseException
{
qString = qs;
// create some index
Directory index = new RAMDirectory();
StandardAnalyzer analyzer = new StandardAnalyzer(matchVersion);
IndexWriterConfig IWConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);
IndexWriter iw = new IndexWriter(index,IWConfig) ;
try {
con = DriverManager.getConnection(host, uName, uPass);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM APP.REGISTRY";
rs = stmt.executeQuery(sql);
rs.beforeFirst();
while(rs.next()) {
doc = new Document();
doc.add(new Field("subject",rs.getString("SUBJECT"),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("letter_from",rs.getString("LETTER_FROM"),Field.Store.YES,Field.Index.ANALYZED));
iw.addDocument(doc);
}
iw.close();
stmt.close();
rs.close();
}
catch(SQLException err)
{System.out.println(err.getMessage());}
//Oen the index
IndexReader ir = IndexReader.open(index);
//create the searcher object
IndexSearcher searcher = new IndexSearcher(ir);
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_36,
new String[]{"subject","letter_from","remarks","office_dispatched_to"}, analyzer);
Query query = queryParser.parse(qString);
//Displaying the search results
TopDocs topDocs = searcher.search(query,10);
ScoreDoc[] hits = topDocs.scoreDocs;
System.out.println("Total hits "+topDocs.totalHits);
for(int q = 0; q<7 ;q++)
{
Vector<String> vn = new Vector<>();
vn.add(d.get("subject"));
vn.add("letter_from");
vn.add("remark");
vn.add("office_dispatched_to");
}
return docVector;
}
public class TableData {
private Vector<Vector<String>> matrix = new Vector<>();
private Vector<String> header = header = new Vector<>();;
public TableData()
{ }
public Vector<Vector<String>> getMatrix() {
return matrix;
}
public void setMatrix(Vector<Vector<String>> matrix) {
this.matrix = matrix;
}
public Vector<String> getHeader() {
return header;
}
public void setHeader(Vector<String> header) {
this.header = header;
}
public void fillHeader(){
header.add(0,"ID");
header.add(1,"SUBJECT");
header.add(2,"LETTER DATE");
header.add(3,"DATE RECEIED");
header.add(4,"REMARKS");
header.add(5,"DATE DISPATCHED");
header.add(6,"DESTINATION OFFICE");
}
}
}