Search code examples
javajtableresultset

How to add boolean column into resultset in java then add result to jtable


let me explain what i trying to achieve here in my result set their are data with 4 columns then i need to add 1 more column manually and it's should contain boolean values (false)

 ResultSet res = DbConnect.tabelDb(sql); // get result set 
List expRptColWise = new ArrayList();

for (int i = 1; i <= res.getMetaData().getColumnCount(); i++) {
    expRxptColWise.add(false);
}

attendence.tblA.setModel(DbUtils.resultSetToTableModel(res))

i have try this code found in stackoverflow.com still this make no changes to my j table it only show 4 columns only


Solution

  • I doubt it can be done using DBUtils.resultSetToTableModel as it's actual TableModel implementation is unknown, instead, you're going to have to get hands dirty, for example...

    try (ResultSet rs = ...) {
        DefaultTableModel model = new DefaultTableModel();
        ResultSetMetaData rsmd = rs.getMetaData();
        for (int col = 0; col < rsmd.getColumnCount(); col++) {
            model.addColumn(rsmd.getColumnName(col + 1));
        }
        model.addColumn("boolean column");
    
        while (rs.next()) {
            Vector data = new Vector();
            for (int col = 0; col < rsmd.getColumnCount(); col++) {
                data.add(rs.getObject(col + 1));
            }
            data.add(Boolean.FALSE);
            model.addRow(data);
        }
    
    } 
    

    Remember, if you open a resource, you should close it, see The try-with-resources Statement for more details