简体   繁体   中英

checking for multiple checkbox and displays in jtable in java netbeans

Good day

I have created a search engine where the user can checkbox according to what information that the data can displayed on the table according to the following figure 在此处输入图片说明

What I am facing is , the data displays only when checking on a single checkbox How can I display multiples and shows for me on the table ? Example : Major is searched according to High school diploma University : MSU All the results shall be displays according to these two conditions Here is my codes :

if (jCheckBox1.isSelected()){
            try {   
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","MZaa8891@");  
        String Sql="select recName, phoneNo,quali,major,Uni,status,IntDate,interviewer FROM hr.rect  where recName like ?  "; 
        ps= con.prepareStatement(Sql);
        ps.setString(1, jTextField1.getText()+"%");
        rs =ps.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
              }
        catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
         }
         }
     if (jCheckBox2.isSelected()){
            try {   
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","MZaa8891@");  
        String Sql="select recName, phoneNo,quali,major,Uni,status,IntDate,interviewer FROM hr.rect  where phoneNo like ?  "; 
        ps= con.prepareStatement(Sql);
        ps.setString(1, jTextField2.getText()+"%");
        rs =ps.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
              }
        catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
         }
         }
           if (jCheckBox3.isSelected()){
            try {   
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","MZaa8891@");  
        String Sql="select recName, phoneNo,quali,major,Uni,status,IntDate,interviewer FROM hr.rect  where quali like ?  "; 
        ps= con.prepareStatement(Sql);
        ps.setString(1, jTextField3.getText()+"%");
        rs =ps.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
              }
        catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
         }
         }
       if (jCheckBox4.isSelected()){
            try {   
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","MZaa8891@");  
        String Sql="select recName, phoneNo,quali,major,Uni,status,IntDate,interviewer FROM hr.rect  where major like ?  "; 
        ps= con.prepareStatement(Sql);
        ps.setString(1, jTextField4.getText()+"%");
        rs =ps.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
              }
        catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
         }
         }
         if (jCheckBox5.isSelected()){
            try {   
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","MZaa8891@");  
        String Sql="select recName, phoneNo,quali,major,Uni,status,IntDate,interviewer FROM hr.rect  where Uni like ?  "; 
        ps= con.prepareStatement(Sql);
        ps.setString(1, jTextField5.getText()+"%");
        rs =ps.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
              }
        catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
         }
         }
        if (jCheckBox6.isSelected()){
            try {   
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","MZaa8891@");  
        String Sql="select recName, phoneNo,quali,major,Uni,status,IntDate,interviewer FROM hr.rect  where status like ?  "; 
        ps= con.prepareStatement(Sql);
        ps.setString(1, jTextField6.getText()+"%");
        rs =ps.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
              }
        catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
         }
         }
       if (jCheckBox7.isSelected()){
            try {   
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","MZaa8891@");  
        String Sql="select recName, phoneNo,quali,major,Uni,status,IntDate,interviewer FROM hr.rect  where IntDate like ?  "; 
        ps= con.prepareStatement(Sql);
        ps.setString(1, jTextField7.getText()+"%");
        rs =ps.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
              }
        catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
         }
         }
         if (jCheckBox8.isSelected()){
            try {   
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","MZaa8891@");  
        String Sql="select recName, phoneNo,quali,major,Uni,status,IntDate,interviewer FROM hr.rect  where interviewer like ?  "; 
        ps= con.prepareStatement(Sql);
        ps.setString(1, jTextField8.getText()+"%");
        rs =ps.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
              }
        catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
         }
         } 

Any Ideas ?

You need to define two dynamic concepts, "query constraints" and "query values".

While a constraint is bound to a value, they are applied at different points in time. This concept can grow really complex really fast.

Let's start with a very basic idea. A "column" and "value" binding...

public static interface Query {
    public String getColumnName();
    public String getColumnValue();

    public String build();
    public void bind(int index, PreparedStatement ps) throws SQLException;
}

public static class DefaultQuery implements Query {

    private String columnName;
    private String columnValue;

    public DefaultQuery(String columnName, String columnValue) {
        this.columnName = columnName;
        this.columnValue = columnValue;
    }

    @Override
    public String getColumnName() {
        return columnName;
    }

    @Override
    public String getColumnValue() {
        return columnValue;
    }

    public String build() {
        return getColumnName() + " like ?";
    }

    @Override
    public void bind(int index, PreparedStatement ps) throws SQLException {
        ps.setString(index, getColumnValue());
    }

}

All this does is makes it easy to manage the "column" and the "value" we want to query. You could make all sorts of concepts around this, like "and"/"or" properties, but that becomes crazy complex quick.

Next we need a "base" query, which can be executed even without any constraints...

String baseQuery = "select recName, phoneNo,quali,major,Uni,status,IntDate,interviewer FROM hr.rect";

Next we need to build a list of possible constraints to be applied...

List<Query> constraints = new ArrayList<>(25);
if (jCheckBox1.isSelected()) {
    constraints.add(new DefaultQuery("recName", jTextField1.getText() + "%"));
}
if (jCheckBox2.isSelected()) {
    constraints.add(new DefaultQuery("phoneNo", jTextField2.getText() + "%"));
}
if (jCheckBox2.isSelected()) {
    constraints.add(new DefaultQuery("quali", jTextField3.getText() + "%"));
}
//...

Once we have that, we can build the "query" we want to make (including the "base" query and any constraints)

if (!constraints.isEmpty()) {
    StringJoiner whereConstraint = new StringJoiner(" and ", " where ", "");
    for (Query query : constraints) {
        whereConstraint.add(query.build());
    }
    baseQuery += whereConstraint.toString();
}

Next we need to bind the values to the query and execute it...

try (Connection con = ...; PreparedStatement ps = con.prepareStatement(baseQuery)) {
    if (!constraints.isEmpty()) {
        int index = 1;
        for (Query query : constraints) {
            query.bind(index++, ps);
        }
    }
    ResultSet rs = ps.executeQuery();
    //...
} catch (SQLException ex) {
    ex.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM