1

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
san88
  • 1,286
  • 5
  • 35
  • 60

2 Answers2

4

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • @trashgod : I just wonder wonder how great **SO** and their members are :). But a li'l question as I had a similar scenario, if I update the table model after doing `(DbUtils.resultSetToTableModel(rs));` it gives me an empty resultset. Would you please comment on that? – mustangDC Jun 23 '15 at 18:33
  • Invoking `rs.beforeFirst()` should move the cursor its initial position, before the first row. – trashgod Jun 23 '15 at 19:51
2

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( "..." );
camickr
  • 321,443
  • 19
  • 166
  • 288