Question

I'm trying to display more detailed column headers, without using the database column names supplied by DbUtils.resultSetToTableModel(). Here is my code:

ResultSet rs = null;
PreparedStatement pst = null;
String sql = "SELECT * From product";
String col[] = {"Product No", "Name", "Price", "QOH"};
DefaultTableModel dtm = new DefaultTableModel();
try {
    pst = conn.prepareStatement(sql);
    rs = pst.executeQuery();
    dtm.setColumnIdentifiers(col);
    ProductList_tbl.setModel(dtm);
    ProductList_tbl.setModel(DbUtils.resultSetToTableModel(rs));
}

but it still displays the database table column names.

Was it helpful?

Solution

Guessing which DbUtils you're using, a few things come to mind:

  • Use an SQL alias for each desired column, instead of the wildcard, e.g.

    SELECT column_name AS alias_name;
    
  • Update the TableModel after it has been created by DbUtils.resultSetToTableModel() using whatever API is available.

OTHER TIPS

If the DbUtils doesn't provide an API to change the column value then you should be able to use the standard table classes:

TableColumnModel tcm = table.getColumnModel();
TableColumn tc = tcm.getColumn(...);
tc.setHeaderValue( "..." );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top