0

尝试从数据库中获取数据并在数据库中显示时出现问题。我从用户输入中获取并存储为搜索变量。这就是我设置桌子的方式:

//I get the user input to perform search
@FXML
 public void searchResident(ActionEvent event){
     String search=getTb_search().getText();
      if(search.equals("")){
         Dialogs.showErrorDialog(null, "Please enter something", "Blank fields detected", "");
    }else{
          setUpSearchTable(search); 
      }
 }

 //How I set up my table
 public void setUpSearchTable(String search) {
    TableColumn rmNameCol = new TableColumn("Name");
    rmNameCol.setVisible(true);
    rmNameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<SearchNeedyResidentController, String>, ObservableValue<String>>() {
        public ObservableValue<String> call(TableColumn.CellDataFeatures<SearchNeedyResidentController, String> p) {
            return p.getValue().searchNameProperty();
        }
    });

    TableColumn rmNricCol = new TableColumn("NRIC");
    rmNricCol.setCellValueFactory(new PropertyValueFactory<SearchNeedyResidentController, String>("search_nric"));
    rmNricCol.setMinWidth(150);

    TableColumn rmPhNoCol = new TableColumn("Phone Number");
    rmPhNoCol.setCellValueFactory(new PropertyValueFactory<SearchNeedyResidentController,String>("search_phNo"));
    rmPhNoCol.setMinWidth(350);

    TableColumn rmIncomeCol = new TableColumn("Income($)");
    rmIncomeCol.setCellValueFactory(new PropertyValueFactory<SearchNeedyResidentController, String>("search_income"));
    rmIncomeCol.setMinWidth(100);

    ResidentManagement.entity.NeedyResidentEntity searchValue= new ResidentManagement.entity.NeedyResidentEntity();
    //viewProduct.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    table_search.setEditable(false);
    table_search.getColumns().addAll(rmNricCol, rmNameCol, rmIncomeCol, rmPhNoCol);
    table_search.getItems().setAll(searchValue.searchResident(search));
 }
}

 //How I populate the table data
 public List<SearchNeedyResidentController> searchResident(String search){
List ll = new LinkedList();
    try {
        DBController db = new DBController();
        db.getConnection();
        String sql = "SELECT * FROM rm_needyresident WHERE name LIKE '" + search + "%'";
        ResultSet rs = null;
        // Call readRequest to get the result
        rs = db.readRequest(sql);

        while (rs.next()) {
            String nric=rs.getString("nric");
            String name = rs.getString("name");
            double income = rs.getDouble("familyIncome");
            String incomeStr = new DecimalFormat("##.00").format(income);
            String phNo = rs.getString("phNo");

            SearchNeedyResidentController row = new SearchNeedyResidentController();
            row.setSearchNric(nric);
            row.setSearchName(name);
            row.setSearchIncome(incomeStr);
            row.setSearchPhNo(phNo);
            ll.add(row);
        }
        rs.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
        System.out.println("Error SQL!!!");
        System.exit(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ll;
}
}

单击搜索按钮时,将显示表格列。但是,即使有匹配的结果,它也只是显示一个空白表。我已经调试过了,我认为错误在于 searchResident 方法中的检索数据。它不是从数据库中检索数据。有人知道出了什么问题吗?

提前致谢。

4

1 回答 1

0

试试看...

   @FXML private void SearchButton() 
     {      
        Connection c ;
        datamem = FXCollections.observableArrayList();
       try
        {
          c = Dao.getCon();
          String SQL =SELECT * FROM `Member`;

          ResultSet rs = c.createStatement().executeQuery(SQL);
          if(table.getColumns().isEmpty())
          {
            for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++)
            {
                final int j = i;               
                TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
                col.setCellValueFactory(new             Callback<TableColumn.CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){                   
                public ObservableValue<String>    call(TableColumn.CellDataFeatures<ObservableList, String> param) {                                                                                             
                return new SimpleStringProperty(param.getValue().get(j).toString());                       
                    }                   
                });

                table.getColumns().addAll(col);
            }//for
          }//if
         while(rs.next())
          {
              ObservableList<String> row = FXCollections.observableArrayList();
              for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++)
                {
                    row.add(rs.getString(i));
                }// for
               datamem.add(row);
          }//while
          table.setItems(datamem);
        }//try
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Problem in Search Button "+e);
        }
    }//else
}//else

} //搜索方法

于 2014-08-13T07:12:09.833 回答