简体   繁体   中英

Getting the full column name from jdbc's metadata object

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.

You have to specify an alias while joining tables. There are two methods - getColumnName() - returns a column name and getColumnLabel() - returns a column label (alias). If an alias is not specifed then the getColumnLabel() returns same value as the value returned by the getColumnName() .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM