1

我正在构建一个 java 桌面应用程序,它使用 lucene 3.6.2 搜索引擎在 derby 数据库中搜索数据 java swing JTable 和 netbeans 用户在测试字段中输入搜索查询,搜索结果显示在 Jtable 和 defaulttablemodel 中。

我已经尝试过有关在 jTable 中显示数据库信息的教程。

但是这次我想要一个搜索字段和一个搜索按钮。找到匹配项后在搜索字段中输入一个字符串,然后将结果显示在 JTable 中

下面是代码片段

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");
    }   
}

    } 
4

1 回答 1

1

看起来您正在DefaultTableModel使用合适的 构建匿名Vector,但您随后更新了Vector而不是TableModel自身。更新模型应该会导致表自行刷新。您可以从 获取对您的模型的引用table.getModel(),并且可以使用 更新它model.setDataVector()。扩展AbstractTableModel,如此处所示可能是一种替代方法。

于 2013-03-13T19:15:59.270 回答