Reputation: 181
Given the following query: SELECT fc as A, sc as B, TO_BIGINT(tc) as C from....
I want to grab the column names fc
, sc
, tc
from the result set.
I do the following:
ResultSet rs = statment.executeQuery(query);
ResultSetMetData rsmd = rs.getMetaData();
for(int i = 1; i <= rsmd.getColumnCount(); i++){
System.out.println(rsmd.getColumnName(i) + " " + rsmd.getColumnLabel(i);
}
However, when I print out the values I get the following:
fc a
sc B
TO_BIGINT(tc) c
Is there no way to ignore the cast using jdbc and grab the actual name from the query?
Upvotes: 0
Views: 415
Reputation: 10396
For expressions, the default column label is the full expression.
The example shows an expression (aka derived column*) with a custom label (as C
) and that is what is returned to the client. That's SQL standard behaviour.
In this regard, all expressions are handled the same, and type casts have no special handling that would allow to "peak the original column".
Long story short: you should consider the ColumnLabel as the intended column name.
If you absolutely want/need to get the base column from the result set and you are fine with all the limitations, then you can manually parse the result from getColumnName
and pick out whatever is within the brackets of any casting function.
E.g. if you get TO_BIGINT( abc )
you look for whatever is between (
and )
.
That is the amount of detail you can derive from the result set.
Upvotes: 1