如何从数据库获取所有相同值的数据并在Jtable中显示(通常是过滤器)

时间:2018-10-13 12:50:40

标签: java swing

我有一个名为“ cust_info”的数据库,我正在创建一个过滤器,以便可以获取所有相同的数据。例如,如果我想知道有多少人来自印度,那么在jtextfield中,我只是输入印度,以便它可以显示匹配的所有详细信息或来自印度的人。我正在获取数据,但一次只能获取一个。例如:我在数据库中有两个来自印度的人,如果我在jtextfield中键入印度,我只会得到一个数据(行),它将不会显示所有与'印度'键相关的人。请让我们来解决这个问题。请查看附件中的图片。谢谢

public void filtering(String ValueToSearch) {
   DefaultTableModel model = (DefaultTableModel) customerinfo.getModel();
    DefaultTableModel model2 = (DefaultTableModel) item.getModel();


    String id = "",name = "",address = "",pc = "",phn="",country = "",eml="",objno="",astore="",ornumber="",pfwa="",sd="",ed="",dor="",mop="",pd="",dosm="",dosc="",ss="";
    int rp =0,sp =0,rps =0,sps =0,er =0;
    try {
        Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/customerinfo","root","");
        Statement st  = con.createStatement();
        ResultSet rs;
        String sqlQuery = "SELECT * FROM `cust_info` WHERE CONCAT(`customer id`, `customer name`, `address`, `postal code`, `phone`, `country`, `email`, `object number`, `auctionstore`, `ordernumber`, `pfwa`, `startdate`, `enddate`, `dateoforder`, `retailprice`, `retailpriceshipping`, `sellingprice`, `sellingpriceshipping`, `earnings`, `methododpayment`, `pickdate`, `dosManufacuturer`, `dosCompany`, `status`) LIKE '%"+ValueToSearch+"%'";
        rs = st.executeQuery(sqlQuery);
        while(rs.next()) {
             id = rs.getString("customer id");
             name = rs.getString("customer name");
             address = rs.getString ("address");
             pc = rs.getString("postal code");
             phn = rs.getString("phone");
             country = rs.getString("country");
             eml = rs.getString("email");
             objno = rs.getString("object number");
             astore = rs.getString("auctionstore");
             ornumber = rs.getString("ordernumber");
             pfwa = rs.getString("pfwa");
             sd = rs.getString("startdate");
             ed = rs.getString("enddate");
             dor = rs.getString("dateoforder");
             rp = rs.getInt("retailprice");
             rps = rs.getInt("retailpriceshipping");
             sp = rs.getInt("sellingprice");
             sps = rs.getInt("sellingpriceshipping");
             er = rs.getInt("earnings");
             mop = rs.getString("methododpayment");
             pd = rs.getString("pickdate");
             dosm = rs.getString("dosManufacuturer");
             dosc  = rs.getString("dosCompany");
             ss = rs.getString("status");




        }
        model.setRowCount(0);
        model2.setRowCount(0);
        model.addRow(new Object[] {id,name,address,pc,phn,country,eml});
        model2.addRow(new Object[] {objno,astore,ornumber,pfwa,sd,ed,dor,rp,rps,sp,sps,er,mop,pd,dosm,dosc,ss});
        if(filter.getText().equals("")) {
            model.getDataVector().removeAllElements();
            model.fireTableDataChanged();
        }
        if(filter.getText().equals("")) {
            model2.getDataVector().removeAllElements();
            model2.fireTableDataChanged();
        }




    }catch(Exception e) {
        JOptionPane.showMessageDialog(null,e.getMessage());
    }

}

在jtextfield iam的关键发布事件中编写此代码

    String key = filter.getText();
    filtering(key); 

database image Jframe

1 个答案:

答案 0 :(得分:0)

  

。我正在获取数据,但是一次只能获取一个。

model.setRowCount(0);
model2.setRowCount(0);
model.addRow(new Object[] {id,name,address,pc,phn,country,eml});
model2.addRow(new Object[] {objno,astore,ornumber,pfwa,sd,ed,dor,rp,rps,sp,sps,er,mop,pd,dosm,dosc,ss});

您将继续清除表模型,然后将其添加回一行数据中。

如果您要执行多个查询,则不要继续调用setRowCount(0)。

此外,此循环的意义是什么?

while(rs.next()) {
     id = rs.getString("customer id");
     name = rs.getString("customer name");
     address = rs.getString ("address");

您一直用新数据覆盖变量吗?因此,当您添加数据行时,您将只获得最后一行。

每次循环时都需要添加一行数据。

所以基本逻辑应该是:

while(rs.next()) {
    Vector<Object> row = new Vector<Object>();
    //id = rs.getString("customer id");
    row.add( rs.getString("customer id") );
    name = rs.getString("customer name");
    row.add( rs.getString("customer name") );
    ...
    model.addRow( row );
}

无论您在ResultSet中是一行数据还是多行数据,这都将起作用。

model.fireTableDataChanged();

此外,摆脱该声明。更改模型中的数据后,TableModel的工作就是调用正确的fireXXX(...)方法。