I’m processing a resultset where the number of returned columns vary and thus I need to know which columns are present. I found that I can extract the returned column names like this:
ResultSetMetaData meta = rs.getMetaData();
ArrayList<String> columns = new ArrayList<String>();
for (int i = 0; i < meta.getColumnCount(); i++) {
columns.add(meta.getColumnLabel(i+1));
}
This however does not give me the full column name defined in my SQL. Ie.
select events.id, events.name from events;
shows up as “id, name” and not “events.id, events.name” which is pretty bad when joining tables and wanting to differ on the column names returned.
The answer is: you can’t retrieve the table alias from the select statement. You can however retrieve the name of the underlying table of a column, so you should be able to retrieve this using getTable(int) from ResultSetMetaData as you are not using table aliases, but are using the actual tables.