Ben
Ben

Reputation: 7

JTable not showing the column names set in the AS part of the SQL prepared statement

I know how to manually set JTable column names, but wondering if there was better way because presently I have a prepared sql statement which selects from DB with column names made to show up as different name using the AS 'New Column Name', but the names in the AS part are not showing up, just the standard DB column names... Is that supposed to work that way or is there a better way apart from manually setting column header names using the getColumnModel().getColumn(2).setHeaderValue("NEW NAME") ... ? Thanks

Upvotes: 0

Views: 172

Answers (1)

trashgod
trashgod

Reputation: 205875

The ResultSetMetaData method getColumnLabel() should provide the text from a given SELECT AS label. For example,

PreparedStatement ps = conn.prepareStatement("SELECT name AS moniker, …");
ResultSet rset = ps.executeQuery();
while (rset.next()) {
    String name = rset.getString(1);
    System.out.println(rset.getMetaData().getColumnLabel(1)+ ": " + name …);
}

Upvotes: 1

Related Questions